【发布时间】:2018-07-18 23:09:47
【问题描述】:
我有用于底部导航视图的类 BottomNaviationViewHelper。 我需要在这个活动中加载片段。但是当我运行我的应用程序时,什么也没有发生。
BottomNaviationViewHelper.class
public void setupBottomNavigationView(BottomNavigationViewEx bottomNavigationViewEx){
Log.d(TAG, "setupBottomNavigationView: Setting up NavigationView1");
bottomNavigationViewEx.enableAnimation(false);
bottomNavigationViewEx.enableItemShiftingMode(false);
bottomNavigationViewEx.enableShiftingMode(false);
bottomNavigationViewEx.setTextVisibility(false);
}
public void enableNavigation(Context context, final BottomNavigationViewEx view, final FragmentManager supportFragmentManager){
view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment;
switch (item.getItemId()){
case R.id.nav_home:
fragment = new FragmentMarker();
loadFragment(fragment);
return true;
case R.id.nav_bookmark:
fragment = new FragmentBookmark();
loadFragment(fragment);
return true;
case R.id.nav_blog:
fragment = new FragmentBlog();
loadFragment(fragment);
return true;
case R.id.nav_notification:
fragment = new FragmentNotification();
loadFragment(fragment);
return true;
case R.id.nav_account:
fragment = new FragmentAccount();
loadFragment(fragment);
return true;
}
return false;
}
private void loadFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = supportFragmentManager.beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
这是我的示例片段之一FragmentsBlog.class
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blog, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setupBottomNavigationView();
}
private void setupBottomNavigationView(){
Log.d(TAG, "setupBottomNavigationView: Setting up BottomNavigationView");
BottomNavigationViewEx bottomNavigationViewEx = getView().findViewById(R.id.bottom_navigation);
((BottomNavigationViewHelper)getActivity()).setupBottomNavigationView(bottomNavigationViewEx);
((BottomNavigationViewHelper)getActivity()).enableNavigation(getActivity(), bottomNavigationViewEx, getFragmentManager());
Menu menu = bottomNavigationViewEx.getMenu();
MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
希望有人能帮我解决这个问题。谢谢!
【问题讨论】:
标签: android android-fragments android-activity bottomnavigationview