1. 程式人生 > >Android Studio實現ViewPager實現切換

Android Studio實現ViewPager實現切換

這回寫的是,實現ViewPager底部切換

我寫的這是,點選為true,和為false的兩種顯示,首先在drawable建一個對應的檔案

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/order_active"></item>
    <item android:state_checked="false" android:drawable="@drawable/order"></item>
</selector>

主佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

  
    <androidx.viewpager.widget.ViewPager
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:id="@+id/view_pager"
        />
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:background="#FFFFF0"
        android:orientation="horizontal"
        android:id="@+id/rg"
        >
        <RadioButton
            android:id="@+id/shouye"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="首頁"
            android:padding="5dp"
            android:button="@null"
            android:gravity="center"
            android:checked="true"
            android:drawableTop="@drawable/shouye"/>
        <RadioButton
            android:id="@+id/dingdan"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="訂單"
            android:padding="5dp"
            android:button="@null"
            android:gravity="center"
            android:drawableTop="@drawable/dingdan"/>
        <RadioButton
            android:id="@+id/wode"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="我的"
            android:padding="5dp"
            android:button="@null"
            android:gravity="center"
            android:drawableTop="@drawable/wode"/>
    </RadioGroup>

</LinearLayout>

這個是MainActivity裡的程式碼

package com.example.administrator.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.myapplication.Fragment.DingDanActivity;
import com.example.administrator.myapplication.Fragment.ShouYieActivity;
import com.example.administrator.myapplication.Fragment.WoDeActivity;


import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private ViewPager view_pager;
    private RadioButton shouye;
    private RadioButton dingdan;
    private RadioButton wode;
    private RadioGroup rg;
    private ArrayList<Fragment> list;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        setOnListener();
        list = new ArrayList<>();
        list.add(new ShouYieActivity());
        list.add(new DingDanActivity());
        list.add(new WoDeActivity());
        view_pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return list.get(i);
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
    }

    private void setOnListener() {
        view_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        rg.check(R.id.shouye);
                        break;
                    case 1:
                        rg.check(R.id.dingdan);
                        break;
                    case 2:
                        rg.check(R.id.wode);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.shouye:
                        view_pager.setCurrentItem(0);
                        break;
                    case R.id.dingdan:
                        view_pager.setCurrentItem(1);
                        break;
                    case R.id.wode:
                        view_pager.setCurrentItem(2);
                        break;
                }
            }
        });
    }

    private void init() {
         view_pager = findViewById(R.id.view_pager);
         shouye = findViewById(R.id.shouye);
         dingdan = findViewById(R.id.dingdan);
         wode = findViewById(R.id.wode);
         rg = findViewById(R.id.rg);


    }

}

我也沒標,註解,但是每個方法的意思都很簡單,上網上查一下就好

相關推薦

Android Studio使用ViewPager+Fragment實現仿微信滑動切換介面

前言 微信的滑動切換獲得大家一致好評,在我們開發的過程中我們也經常模仿微信的導航效果。 首先看下效果圖 效果還算不錯,可以滑動切換和點選切換,微信介面用listview展示資料,通訊錄介面用的recyclerview展示資料,在接下來就帶著大家一一

Android Studio實現ViewPager實現切換

這回寫的是,實現ViewPager底部切換 我寫的這是,點選為true,和為false的兩種顯示,首先在drawable建一個對應的檔案 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:a

Android Studio 使用ViewPager + Fragment實現滑動選單Tab效果 --簡易版

描述:         之前有做過一個記賬本APP,拿來練手的,做的很簡單,是用Eclipse開發的;         最近想把這個APP重新完善一下,添加了一些新的功能,並選用Android Studio來開發;         APP已經完善了一部分,現在就想把已經做好的功能整理一下,記錄下來。 效果圖

Android:使用ViewPager實現左右滑動切換圖片 (簡單版)

ViewPager,它是google SDk中自帶的一個附加包的一個類, 可以使檢視滑動。 步驟: 1、引入android-support-v4.jar包,在主佈局里加入 <android.support.v4.view.ViewPager android

Android Studio 使用Intent實現頁面的跳轉(帶參數)

col xtra ima alt main 都是 img mage android 不管是在APP,還是在網站中,頁面之間的跳轉都是很常見的,本文主要講一下在APP中,如何通過Intent實現頁面的跳轉。 不帶參數: 寫在MainActivity頁面的代碼: 1 In

android studio 使用zxing實現掃碼功能

1、新增依賴 在專案的build.gradle新增:maven { url 'https://jitpack.io' } allprojects { repositories { google() jcenter() maven { u

android studio中ToolBar實現標題欄

首先注意將values中的style類中改成NoActionBar <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light**.NoActionBar**

android studio抽獎轉盤實現

首先建立一個類,繼承view實現三個方法 package com.zhuanpan.turntable; import android.content.Context; import android.graphics.Canvas; import android.graphics.C

Android中Fragment+Viewpager實現左右滑動和點選

一.佈局檔案 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.ViewPager android:id="@+id/viewpager" andr

Android TV利用viewPager實現輪播圖,並通過handler進行邏輯控制

    公司要求實現一個輪播圖,滾動圖片及其對應文字。共有五張圖,包含小圓點。     最初的實現是參考了https://blog.csdn.net/zhaoxiaojian1213/article/details/78280132,使用ViewPager實現,新開一個執行

Android Studio-個推-實現簡單聊天(二)

實現聊天,首先要知道聊天雙方的CID,這裡可以獲取到,將其存起來。 這裡用模擬器測試一下,準備倆臺模擬器,分別獲得其CID;準備好聊天介面;注備好聊天介面,就可以測試了。 一、web後臺Contraller package com.smxy.office.con

Android Studio-個推-實現簡單聊天(三)

因為之前的是寫的主要程式碼,現在貼出全部程式碼,有些瑕疵,哈哈哈哈。那我現在開始貼程式碼吧 1.聊天實體類 (1)聊天實體類 ChatUser.java package com.smxy.lj.chat; import java.io.Serializable

Android Studio中TextView實現跑馬燈效果

自建一個MarqueeText 類 繼承自AppCompatTextView並重載父類的三個構造方法,新增一個isFocused方法 public class MarqueeText extends AppCompatTextView { public Marque

Android Studio使用Gradle實現自動打包,簽名,自定義apk檔名,多渠道打包,整合系統簽名證書【附效果圖附原始碼】

        接觸Android Stuidio有一陣子了,之前用的時候有很多小問題,不過現在的版本感覺已經很好用了,所以準備徹底從Eclipse轉戰Android Stuidio,這段時間把以前經常使用的公用庫都從Eclipse移植過來了,今天研究了一下在Andro

Android Studio使用AIDL 實現程序間通訊例項

參考:部落格:http://www.cnblogs.com/BeyondAnyTime/p/3204119.html, http://www.cnblogs.com/linlf03/p/3192025.html     視訊教程:http://www.imooc.com/l

Android Studio 表格佈局實現登入介面

在xml檔案中新增如下程式碼: <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout1"

Android 巢狀ViewPager實現連貫雙滑動

ViewPager巢狀ViewPager後,滑動事件沒法在子ViewPager裡面響應。 解決辦法是自定義子ViewPager。 通知他的父ViewPager現在進行的是本控制元件的操作,不要對我的操作進行干擾 getParent().requestDisallow

Android Studio 登入功能實現

LoginActivity.java 包 com.hyx.example.mymap; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Activity; imp

Android Studio-個推-實現簡單聊天(一)

官方文件 一、註冊賬號,建立個推應用 註冊好賬號之後,看開發文件步驟建立應用。建立好的應用列表如下 點選應用配置,會看到後續程式碼會用到的內容,注意[應用標識],它是填專案的包名。 二、專案開發 1.添加個推SDK及相關配置 在主專案build.gradle

Android Studio——layout_weight體驗(實現按比例顯示)

在android開發中LinearLayout很常用,LinearLayout的內控制元件的android:layout_weight在某些場景顯得非常重要,比如我們需要按比例顯示。android並沒用提供table這樣的控制元件,雖然有TableLayout,但是它並非