布局完成后,需要通过代码将各个布局串起来
初始化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);
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();
}
}
初始化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();
初始化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;
}
});