1.ViewPager+TabLayout
1.1xml
<androidx.viewpager.widget.ViewPager android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="10" android:id="@+id/vp_main" /> <com.google.android.material.tabs.TabLayout android:id="@+id/tab_main" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" />
1.2创建对应的 Fragment
1.3 编写Adapter
public class NavigationAdapter extends FragmentPagerAdapter { private List<Fragment> mFragments; private List<String> mtitle; public NavigationAdapter(@NonNull FragmentManager fm,List<Fragment> mFragments,List<String> mtitle) { super(fm); this.mFragments = mFragments; this.mtitle = mtitle; } @NonNull @Override public Fragment getItem(int position) { return mFragments.get(position); } @Override public int getCount() { return mFragments.size(); } @Nullable @Override //获取分页标题 public CharSequence getPageTitle(int position) { return mtitle.get(position); } }
1.4 activity里初始化
private void initView() { mFragments=new ArrayList<>(); mFragments.add(new MsgFragment()); mFragments.add(new FriendsFragment()); mFragments.add(new NewThingsFragment()); mFragments.add(new MyFragment()); mtitle=new ArrayList<String>(); mtitle.add("信息"); mtitle.add("联系人"); mtitle.add("发现"); mtitle.add("我的"); //实例化适配器 NavigationAdapter adapt = new NavigationAdapter(getSupportFragmentManager(), mFragments, mtitle); mMainBinding.vpMain.setAdapter(adapt); mMainBinding.tabMain.setupWithViewPager(mMainBinding.vpMain);//给tab设置一个viewpager //viewpager的监听 mMainBinding.vpMain.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override //选中 public void onPageSelected(int position) { switch (position){ case 0: break; case 1: break; case 2: break; case 3: break; } } @Override // //state的状态有三个,0表示什么都没做,1正在滑动,2滑动完毕 public void onPageScrollStateChanged(int state) { } }); }
2.RadioGroup + ViewPager
2.1xml
<androidx.viewpager.widget.ViewPager android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="10" android:id="@+id/vp_main" /> <RadioGroup android:id="@+id/rg_main" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2"> <RadioButton android:checked="true" android:textColor="@drawable/textcolor" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:button="@null" android:text="消息" android:drawableTop="@drawable/msg"/> <RadioButton android:textColor="@drawable/textcolor" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="联系人" android:button="@null" 去掉radiobutton的样式 android:drawableTop="@drawable/friends"/> <RadioButton android:textColor="@drawable/textcolor" android:layout_width="0dp" android:gravity="center" android:layout_height="match_parent" android:layout_weight="1" android:text="动态" android:button="@null" android:drawableTop="@drawable/newthing"/> <RadioButton android:textColor="@drawable/textcolor" android:layout_width="0dp" android:gravity="center" android:layout_height="match_parent" android:layout_weight="1" android:text="我的" android:button="@null" android:drawableTop="@drawable/my"/> </RadioGroup>
2.2.编写selector
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/friend" android:state_checked="false"/> <item android:drawable="@mipmap/friend1" android:state_checked="true"/> </selector>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" tools:ignore="MissingDefaultResource"> <item android:color="@color/black" android:state_checked="false"/> <item android:color="@color/bule" android:state_checked="true"/> </selector>
2.3.mainActivity
private void initViewByRG() { mFragments = new ArrayList<>(4); mFragments.add(new MsgFragment()); mFragments.add(new FriendsFragment()); mFragments.add(new NewThingsFragment()); mFragments.add(new MyFragment()); // init view pager NavigationAdapter navigationAdapter = new NavigationAdapter(getSupportFragmentManager(), mFragments); mMainBinding.vpMain.setAdapter(navigationAdapter); // register listener mMainBinding.vpMain.addOnPageChangeListener(mPageChangeListener); mMainBinding.rgMain.setOnCheckedChangeListener(mOnCheckedChangeListener); } private ViewPager.OnPageChangeListener mPageChangeListener = new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if (position!=0){ RadioButton first = (RadioButton) mMainBinding.rgMain.getChildAt(0); first.setChecked(false); } RadioButton radioButton = (RadioButton) mMainBinding.rgMain.getChildAt(position); radioButton.setChecked(true); } @Override public void onPageScrollStateChanged(int state) { } }; private RadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { for (int i = 0; i < group.getChildCount(); i++) { if (group.getChildAt(i).getId() == checkedId) { mMainBinding.vpMain.setCurrentItem(i); return; } } } };
3.BottomNavigationView
这个按照官方的操作,创建然后改一下就行了,这里不进行任何说明