TabLayout与ViewPager结合使用

:TabLayout是安卓6.0推出的,可以替代 ViewPagerIndicator 的一个控件,存放在 design 包下,继承自 HorizontalScrollView 。使用TabLayout需要导入依赖

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

TabLayout与ViewPager结合使用_第1张图片

整体效果就是外部是一个帧布局切换fragment(消息1.2.3),在消息1的fragment中实现的TabLayout+ViewPage

MainActivity实现fragment(消息1.2.3)的切换
MainActivity布局




    


    

    

        

        

        

    


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)布局




   


   

    


    




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 list = new ArrayList();
        list.add(new Fragment04());
        list.add(new Fragment05());
        list.add(new Fragment06());

        //tab要展示的文字集合
        List slist = new ArrayList();
        for (int i=0;i

适配器

public class MyPagerAdapter extends FragmentPagerAdapter {

    private List list;
    private List slist;


    public MyPagerAdapter(FragmentManager fm,List list,List 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:本人组织语言很差,望大家海涵

TabLayout与ViewPager结合使用_第2张图片

你可能感兴趣的:(TabLayout与ViewPager结合使用)