BottomNavigationView+ViewPager
2018-08-04 本文已影响0人
大灰狼zz
效果图:
GIF.gif布局文件中使用BottomNavigationView
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
app:itemIconTint="@drawable/home_nav_item_color_state"
app:itemTextColor="@drawable/home_nav_item_color_state"
app:menu="@menu/home_navigation">
</android.support.design.widget.BottomNavigationView>
在menu文件夹(没有的话在res下新建一个)下新建home_navigation.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@mipmap/icon1"
android:title="首页" />
<item
android:id="@+id/navigation_list"
android:icon="@mipmap/icon2"
android:title="列表" />
<item
android:id="@+id/navigation_Three"
android:icon="@mipmap/icon3"
android:title="三方" />
<item
android:id="@+id/navigation_person"
android:icon="@mipmap/icon4"
android:title="个人" />
</menu>
在需要使用的页面添加如下代码,与viewpager联动
private MenuItem menuItem;
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (menuItem != null) {
menuItem.setChecked(false);
} else {
bottomNavigationView.getMenu().getItem(0).setChecked(false);
}
menuItem = bottomNavigationView.getMenu().getItem(position);
menuItem.setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);//取消位移动画
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
menuItem = item;
int i = item.getItemId();
if (i == R.id.navigation_home) {
viewPager.setCurrentItem(0);
return true;
} else if (i == R.id.navigation_list) {
viewPager.setCurrentItem(1);
return true;
} else if (i == R.id.navigation_Three) {
viewPager.setCurrentItem(2);
return true;
} else if (i == R.id.navigation_person) {
viewPager.setCurrentItem(3);
return true;
}
return false;
}
});
BottomNavigationViewHelper.java
public class BottomNavigationViewHelper {
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
}