Android底部导航栏实现
TabLayout+ViewPager+Fragment
https://blog.csdn.net/chengmuzhe2690/article/details/89406085
布局文件:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/viewpager"></android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabIndicatorHeight="0dp"
app:tabSelectedTextColor="@android:color/holo_red_dark"
app:tabTextColor="@android:color/black"
android:id="@+id/tablayout">
Activity
private TabLayout tabLayout;
private List<Fragment> fragments;
private ViewPager viewPager;
private FragmentPagerAdapter adapter;
private String[] strings ={"Home","DashBoard","Notification","Email","Alert"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tablayout_test);
initView();
}
private void initView() {
tabLayout = findViewById(R.id.tablayout);
fragments = new ArrayList<Fragment>();
fragments.add(HomeFragment.newInstance());
fragments.add(DashboardFragment.newInstance());
fragments.add(NotificationFragment.newInstance());
fragments.add(EmailFragment.newInstance());
fragments.add(AlertFragment.newInstance());
viewPager = findViewById(R.id.viewpager);
adapter = new TitleFragmentPagerAdapter(getSupportFragmentManager(),fragments,strings);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
onTabItemSelected();
}
private void onTabItemSelected() {
for (int i =0;i<tabLayout.getTabCount();i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
Drawable drawable = null;
switch (i){
case 0:
drawable = getResources().getDrawable(R.drawable.ic_home_black_24dp);
break;
case 1:
drawable = getResources().getDrawable(R.drawable.ic_dashboard_black_24dp);
break;
case 2:
drawable = getResources().getDrawable(R.drawable.ic_notifications_black_24dp);
break;
case 3:
drawable = getResources().getDrawable(R.drawable.email);
break;
case 4:
drawable = getResources().getDrawable(R.drawable.dialog_alert_icon);
break;
}
tab.setIcon(drawable);
}
}
private class TitleFragmentPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragmentList = null;
private String[] titles;
public TitleFragmentPagerAdapter(FragmentManager mFragmentManager,
List<Fragment> fragmentList) {
super(mFragmentManager);
mFragmentList = fragmentList;
}
public TitleFragmentPagerAdapter(FragmentManager mFragmentManager,
List<Fragment> fragmentList, String[] titles) {
super(mFragmentManager);
mFragmentList = fragmentList;
this.titles = titles;
}
@Override
public int getCount() {
return mFragmentList.size();
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
if (position < mFragmentList.size()) {
fragment = mFragmentList.get(position);
} else {
fragment = mFragmentList.get(0);
}
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
if (titles != null && titles.length > 0)
return titles[position];
return null;
}
}
Androdx控件:
BottomNavigationView+ViewPager+fragment
https://blog.csdn.net/qq_15396257/article/details/116498355
https://www.cnblogs.com/sunxun/p/9241180.html
https://blog.csdn.net/xiaoyangsavvy/article/details/70213537
https://github.com/brucevanfdm/BottomNavigationView