Android开发记录(三)ViewPager2和TabLayout绑定

布局完成后,需要通过代码将各个布局串起来

  1. 初始化ViewPager

private List<Fragment> fragmentList = new ArrayList<Fragment>();
private MainViewPagerAdapter mainViewPagerAdapter;
  
fragmentList.add(HomeFragment.newInstance());
fragmentList.add(NavFragment.newInstance());
fragmentList.add(ProjectFragment.newInstance());
fragmentList.add(CollectFragment.newInstance());
mainViewPagerAdapter = new MainViewPagerAdapter(this,fragmentList);
binding.appBarMain.contentMain.viewPagerMain.setAdapter(mainViewPagerAdapter);
  1. MainViewPagerAdapter

public class MainViewPagerAdapter extends FragmentStateAdapter {
    private List<Fragment> mFragmentList;

    public MainViewPagerAdapter(@NonNull @NotNull FragmentActivity fragmentActivity, List<Fragment> fragmentList) {
        super(fragmentActivity);
        this.mFragmentList = fragmentList;
    }

    @NotNull
    @Override
    public Fragment createFragment(int position) {
        return  mFragmentList.get(position);
    }

    @Override
    public int getItemCount() {
        return  mFragmentList.size();
    }
}
  1. 初始化TabLayout,绑定ViewPager2

 LayoutInflater layoutInflater = LayoutInflater.from(this);
        TabLayoutMediator mediator = new TabLayoutMediator(binding.appBarMain.contentMain.tabLayoutBottom,binding.appBarMain.contentMain.viewPagerMain,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull @NotNull TabLayout.Tab tab, int position) {
                        View view = layoutInflater.inflate(R.layout.main_tablayout_view,null);
                        TextView tabtitle = (TextView) view.findViewById(R.id.tabtitle);
                        tabtitle.setText(tabTitles[position]);
                        ImageView tabicon = (ImageView) view.findViewById(R.id.tabicon);
                        tabicon.setImageResource(tabIcons[position]);
                        tab.setCustomView(view);
                    }
                });
        mediator.attach();
        //禁止ViewPager滑动
        binding.appBarMain.contentMain.viewPagerMain.setUserInputEnabled(false);
        binding.appBarMain.contentMain.tabLayoutBottom.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                binding.toolbar.setTitle(tabTitles[tab.getPosition()]);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        binding.appBarMain.contentMain.tabLayoutBottom.getTabAt(0).select();
  1. 初始化ToolBar

        //用toolbar代替actionbar
        setSupportActionBar(binding.toolbar);
        //使返回按钮可点击
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        //隐藏标配
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        binding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.drawerLayout.isOpen()) {
                    binding.drawerLayout.close();
                } else {
                    binding.drawerLayout.open();
                }
            }
        });
        binding.toolbar.setTitle(tabTitles[0]);
        binding.toolbar.setTitleTextColor(getResources().getColor(R.color.white));
binding.navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_center:
                        Log.i(TAG, "个人中心");
                        break;
                    case R.id.nav_clean:
                        Log.i(TAG, "清除缓存");
                        break;
                }
                return false;
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值