所以我开始了一个新项目并实现了BottomNavigationView,我创建了一个类帮助器,BottomNavigationViewHelper使按钮保持原样,除了导航视图中的按钮必须两次点击工作之外,一切都工作正常单击一下。如果我单击按钮,它只会突出显示它,然后我必须再次单击它才能访问class / xml。
这是Helper Class的代码
public class BottomNavigationViewHelper {
@SuppressLint("RestrictedApi")
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);
}
}
public static void enableNavigation(final Context context, BottomNavigationView view){
view.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
@Override
public void onNavigationItemReselected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.ic_house:
Intent intent1=new Intent(context, HomeActivity.class); //ACTIVITY_NUM=0
context.startActivity(intent1);
break;
case R.id.ic_search:
Intent intent2=new Intent(context, SearchActivity.class);//ACTIVITY_NUM=1
context.startActivity(intent2);
break;
case R.id.ic_circle:
Intent intent3=new Intent(context, PicActivity.class);//ACTIVITY_NUM=2
context.startActivity(intent3);
break;
case R.id.ic_alert:
Intent intent4=new Intent(context, AlertActivity.class);//ACTIVITY_NUM=3
context.startActivity(intent4);
break;
case R.id.ic_android:
Intent intent5=new Intent(context, ProfileActivity.class);//ACTIVITY_NUM=4
context.startActivity(intent5);
break;
}
}
});
}
}
这是个人资料类
public class ProfileActivity extends AppCompatActivity {
private Context mContext= ProfileActivity.this;
private static final int ACTIVITY_NUM=4;
private static final String TAG ="ProfileActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
setupToolbar();
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationView);
Menu menu=bottomNavigationView.getMenu();
MenuItem menuItem=menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
Log.d(TAG, "onCreate: started.");
}
private void setupToolbar(){
Toolbar toolbar=(Toolbar) findViewById(R.id.profileToolBar);
setSupportActionBar(toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Log.d(TAG,"onMenuItemClick:clicked menu item: "+item);
switch (item.getItemId()){
case R.id.profileMenu:
Log.d(TAG,"onMenuItemClick: Navigating to profile preferences.");
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.profile_menu, menu);
return true;
}
}
最后是xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true">
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottomNavViewBar"
android:background="@drawable/white_grey_border_top"
app:menu="@menu/bottom_navigation_menu"
app:itemIconTint="@color/teal"
android:clickable="true"
android:focusable="true">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
</merge>
非常感谢任何帮助。
答案 0 :(得分:2)
要注册SelectListener,而不是重新 selectListener!
public static void enableNavigation(final Context context, BottomNavigationView view){
view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public void onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){