1. 程式人生 > >TabLayout與ViewPager結合使用

TabLayout與ViewPager結合使用

:TabLayout是安卓6.0推出的,可以替代 ViewPagerIndicator 的一個控制元件,存放在 design 包下,繼承自 HorizontalScrollView 。使用TabLayout需要匯入依賴

implementation 'com.android.support:design:27.1.1'

在這裡插入圖片描述

整體效果就是外部是一個幀佈局切換fragment(訊息1.2.3),在訊息1的fragment中實現的TabLayout+ViewPage

MainActivity實現fragment(訊息1.2.3)的切換
MainActivity佈局

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/frameLt"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8">


    </FrameLayout>

    <RadioGroup
        android:layout_marginTop="20dp"
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/btn01"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:drawableTop="@drawable/radiobtn_selector"
            android:text="訊息(1)"/>

        <RadioButton
            android:id="@+id/btn02"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:drawableTop="@drawable/radiobtn_selector"
            android:text="訊息(2)"/>

        <RadioButton
            android:id="@+id/btn03"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:drawableTop="@drawable/radiobtn_selector"
            android:text="訊息(3)"/>

    </RadioGroup>

</LinearLayout>

MainActivity程式碼

public class MainActivity extends AppCompatActivity {

    private Toolbar tool;
    private FragmentManager manager;
    private RadioGroup radioGroup;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //載入元件

        radioGroup = findViewById(R.id.radioGroup);

        //獲取事務管理
        manager = getSupportFragmentManager();
        //開啟事務
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        //新增事務
        fragmentTransaction.add(R.id.frameLt,new Fragment01());
        //提交事務
        fragmentTransaction.commit();

        //第一個Radiobutton預設選中
        radioGroup.check(radioGroup.getChildAt(0).getId());

        //radioGroup的監聽
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                //開啟新事務,上一個已經提交
                FragmentTransaction fragmentTransaction2 = manager.beginTransaction();

                switch (i){
                    case R.id.btn01:
                        //替換fragment
                        fragmentTransaction2.replace(R.id.frameLt,new Fragment01());
                        break;
                    case R.id.btn02:
                        fragmentTransaction2.replace(R.id.frameLt,new Fragment02());
                        break;
                    case R.id.btn03:
                        fragmentTransaction2.replace(R.id.frameLt,new Fragment03());
                        break;

                }
                fragmentTransaction2.commit();
            }
        });

    }

fragment(訊息1)佈局

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

   <android.support.design.widget.TabLayout
       android:id="@+id/tabLt"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="#ffbb">


   </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">


    </android.support.v4.view.ViewPager>


</LinearLayout>

fragment(訊息1)java程式碼
fragment(訊息1)實現今天的重點,剩下的fragment程式碼就不發了

public class Fragment01 extends Fragment {

    private TabLayout tabLt;
    private ViewPager pager;

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment01,container,false);
        //載入元件
        tabLt = view.findViewById(R.id.tabLt);
        pager = view.findViewById(R.id.pager);

        //tab與pager繫結
        tabLt.setupWithViewPager(pager);
        //tablayout的顯示模式;
        tabLt.setTabMode(TabLayout.MODE_FIXED);
        //pager輪播集合
        List<Fragment> list = new ArrayList<Fragment>();
        list.add(new Fragment04());
        list.add(new Fragment05());
        list.add(new Fragment06());

        //tab要展示的文字集合
        List<String> slist = new ArrayList<String>();
        for (int i=0;i<list.size();i++){

            slist.add("標題"+i);
        }

        //介面卡
        //getFragmentManager()所得到的是所在fragment 的父容器的管理器,
        //getChildFragmentManager()所得到的是在fragment  裡面子容器的管理器。
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getChildFragmentManager(),list,slist);
        pager.setAdapter(myPagerAdapter);



        return view;
    }

}

介面卡

public class MyPagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> list;
    private List<String> slist;


    public MyPagerAdapter(FragmentManager fm,List<Fragment> list,List<String> slist) {
        super(fm);
        this.list =list;
        this.slist = slist;

    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = list.get(position);

        return fragment;
    }

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

    @Nullable
    @Override
    //返回標題,返回給tab
    public CharSequence getPageTitle(int position) {
        return slist.get(position);
    }
}

ps:本人組織語言很差,望大家海涵

在這裡插入圖片描述