如何将超过5个项目添加到bottomNavigationBar?

时间:2018-10-06 10:27:59

标签: java android bottomnavigationview

大家好,我正在开发android新闻应用,当我添加了第五个菜单项时,可以将项目增加到5个。它给出了此异常java.lang.RuntimeException:无法启动活动ComponentInfo {yodgobekkomilov.edgar.com.worldnews / yodgobekkomilov.edgar.com.worldnews.MainActivity}:java.lang.RuntimeException:NavigationPage列表必须包含4个成员。

在MainActivity的代码下面

public class MainActivity extends BottomBarHolderActivity implements AllJazeeraFragment.OnFragmentInteractionListener, BBCFragment.OnFragmentInteractionListener, CNNFragment.OnFragmentInteractionListener, CBCNewsFragment.OnFragmentInteractionListener {

//    private ApiService apiService;
public static final String url_key = "urlKey";
public ArrayList<Article> articleList = new ArrayList<>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

    Article article = new Article(Parcel.obtain());
    article.setUrl(article.getUrl());
    Intent i = new Intent(this, DetailActivity.class);
    i.putExtra("urlKey", article);
// // using the (String name, Parcelable value) overload!
// startActivity(i);

    NavigationPage page1 = new NavigationPage("AllJazeera", ContextCompat.getDrawable(this, R.drawable.alljazeera), AllJazeeraFragment.newInstance());
    NavigationPage page2 = new NavigationPage("BBC", ContextCompat.getDrawable(this, R.drawable.bbc_icon), CNNFragment.newInstance());
    NavigationPage page3 = new NavigationPage("CNN", ContextCompat.getDrawable(this, R.drawable.cnn_icon), AllJazeeraFragment.newInstance());
    NavigationPage page4 = new NavigationPage("CBC", ContextCompat.getDrawable(this, R.drawable.cbc_icon), CBCNewsFragment.newInstance());
    NavigationPage page5 = new NavigationPage("Menu", ContextCompat.getDrawable(this, R.drawable.icon_menu), Menu.newInstance());
    List<NavigationPage> navigationPages = new ArrayList<>();
    navigationPages.add(page1);
    navigationPages.add(page2);
    navigationPages.add(page3);
    navigationPages.add(page4);
    navigationPages.add(page5);
    super.setupBottomBarHolderActivity(navigationPages);
}


public void onClicked() {
    Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
}

我正在使用此but not all

我知道它只容纳4件物品,但其他选项应该在那里容纳第五件物品。

1 个答案:

答案 0 :(得分:1)

尝试使用

BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomBar);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);

班级是

class BottomNavigationViewHelper {
    static void removeShiftMode(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);
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("ERROR NO SUCH FIELD", "Unable to get shift mode field");
        } catch (IllegalAccessException e) {
            Log.e("ERROR ILLEGAL ALG", "Unable to change value of shift mode");
        }
    }
}