1. 程式人生 > >第16天ViewPager案例4:ViewPager+Fragment巢狀

第16天ViewPager案例4:ViewPager+Fragment巢狀

第16天ViewPager案例4:ViewPager+Fragment巢狀

ViewPager

一.效果

在這裡插入圖片描述

二.思路:

(1)父Viewpager:RadioGroup+Viewpager+5個Fragment (Fragment管理者:getSupportFragmentManager)
(2)首頁Fragment:ViewPager+5個Fragment+tablayout(Fragment管理者:getChildFragmentManager)

三.父Viewpager程式碼

(1)匯入依賴:implementation ‘com.android.support:design:25.2.0’
(2)建立底部導航的5個Fragment
(3)佈局activity_main.xml:ViewPager+RadioButton

<?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"> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_weight=
"10" android:layout_width="match_parent" android:layout_height="0dp"> </android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/rg" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal"> <RadioButton android:id="@+id/rb1" android:textAlignment="center" android:drawableTop="@drawable/selector" android:button="@null" android:text="首頁" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/rb2" android:textAlignment="center" android:drawableTop="@drawable/selector" android:button="@null" android:text="點播" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/rb3" android:textAlignment="center" android:drawableTop="@drawable/selector" android:button="@null" android:text="視訊" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/rb4" android:textAlignment="center" android:drawableTop="@drawable/selector" android:button="@null" android:text="我的" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </RadioGroup> </LinearLayout>

(4)Java程式碼:MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;
    private RadioGroup rg;
    private RadioButton rb1,rb2,rb3,rb4;
    //資料
    private ArrayList<Fragment> fragment_list=new ArrayList<>();
    //介面卡
    private MyFragementPagerAdapter myFragementPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initview();
        initdata();
        initadpater();
        initlistener();
    }

    private void initlistener() {
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //checkedId:選中的rb的id
                if(rb1.getId()==checkedId){
                    vp.setCurrentItem(0);
                }
                if(rb2.getId()==checkedId){
                    vp.setCurrentItem(1);
                }
                if(rb3.getId()==checkedId){
                    vp.setCurrentItem(2);
                }
                if(rb4.getId()==checkedId){
                    vp.setCurrentItem(3);
                }
            }
        });
    }

    private void initadpater() {
        myFragementPagerAdapter=new MyFragementPagerAdapter(getSupportFragmentManager(),fragment_list);
        vp.setAdapter(myFragementPagerAdapter);
    }

    private void initdata() {
        fragment_list.add(new Fragment1());
        fragment_list.add(new Fragment2());
        fragment_list.add(new Fragment3());
        fragment_list.add(new Fragment4());

    }

    private void initview() {
        vp=findViewById(R.id.vp);
        rg=findViewById(R.id.rg);
        rb1=findViewById(R.id.rb1);
        rb2=findViewById(R.id.rb2);
        rb3=findViewById(R.id.rb3);
        rb4=findViewById(R.id.rb4);
    }
}

(4)介面卡程式碼:MyFragmentPagerAdapter.java

public class MyFragementPagerAdapter extends FragmentPagerAdapter {
    private ArrayList<Fragment> list;
    public MyFragementPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
        super(fm);
        this.list=list;
    }

    @Override
    public Fragment getItem(int i) {
        return list.get(i);
    }

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

四.首頁Fragment程式碼

(1)匯入依賴:implementation ‘com.android.support:design:25.2.0’
(2)建立tablayout的5個fragment
(3)佈局fragment_fragment1.xml:ViewPager+tabLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragment1"
    android:orientation="vertical">
    <android.support.design.widget.TabLayout
        android:id="@+id/tb"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/vp1"
        android:layout_weight="10"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </android.support.v4.view.ViewPager>

</LinearLayout>

(4)Java程式碼:Fragment1 .java

public class Fragment1 extends Fragment {
    private TabLayout tb;
    private ViewPager vp1;

    private ArrayList<Fragment> fragment_list=new ArrayList<>();
    private ArrayList<String> title_list=new ArrayList<>();
    private MyFragmentPagerAdapter2 myFragmentPagerAdapter2;

    public Fragment1() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.i("aaa", "onCreateView: ");
        View view=inflater.inflate(R.layout.fragment_fragment1, container, false);
        //TODO 檢視
        tb=view.findViewById(R.id.tb);
        vp1=view.findViewById(R.id.vp1);

        //TODO 介面卡
        initadapter();
        //TODO 關聯
        tb.setupWithViewPager(vp1);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //TODO 資料:不要在onCreateView裡面新增資料,不然會重複的
        initdata();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i("aaa", "onDestroyView: ");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("aaa", "onDestroy: ");
    }

    private void initadapter() {
    //使用getChildFragmentManager管理者進行管理
        myFragmentPagerAdapter2=new MyFragmentPagerAdapter2(getChildFragmentManager(),fragment_list,title_list);
        vp1.setAdapter(myFragmentPagerAdapter2);
    }

    private void initdata() {
        fragment_list.add(new Fragment11());
        fragment_list.add(new Fragment12());
        fragment_list.add(new Fragment13());
        fragment_list.add(new Fragment14());
        title_list.add("111");
        title_list.add("222");
        title_list.add("333");
        title_list.add("444");
    }

}

(4)介面卡程式碼:MyFragmentPagerAdapter2 .java

public class MyFragmentPagerAdapter2 extends FragmentPagerAdapter{
    private ArrayList<Fragment> list1;
    private ArrayList<String> list2;

    public MyFragmentPagerAdapter2(FragmentManager fm, ArrayList<Fragment> list1,ArrayList<String> list2) {
        super(fm);
        this.list1=list1;
        this.list2=list2;
    }

    @Override
    public Fragment getItem(int i) {
        return list1.get(i);
    }

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

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return list2.get(position);
    }
}

總結:程式碼結構:

在這裡插入圖片描述
原始碼地址:
https://pan.baidu.com/s/1a1AG6mcWj1kkp670uXmfZw