Невозможно отметить Нижнюю навигацию Посмотреть объект - PullRequest
0 голосов
/ 31 мая 2018

У меня есть два вида деятельности: «MainActivity» и «LibraryActivity».Эти два действия используют один и тот же «вид снизу навигации».В этом случае мне удалось выбрать правильный элемент (выделенный другим цветом), когда намерение вызывает «LibraryActivity».Проблема возвращается к «MainActivity» с помощью «onBackPressed ()» из «LibraryActivity», нижний элемент «View Navigation» не выделен другим цветом.

Ниже мой код:

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);       

        context = this;

        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

 // used to highlight the correct item

        Menu bottomMenu = bottomNavigationView.getMenu();
        for (int i=0; i<bottomMenu.size(); i++)
        {
            MenuItem bottomMenuItem = bottomMenu.getItem(0);
            bottomMenuItem.setChecked(true);
        }

// item click listener

        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.action_library:
                                Intent intent = new Intent(getApplicationContext(), LibraryActivity.class);
                                intent.putExtra("NUM", "0");
                                startActivity(intent);

                                break;
                        }
                        return true;
                    }
                });

    }

LibraryActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_library);

        context = this;

        bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

// used to highlight the correct item

        Menu bottomMenu = bottomNavigationView.getMenu();
        for (int i=0; i<bottomMenu.size(); i++)
        {
            MenuItem bottomMenuItem = bottomMenu.getItem(1);
            bottomMenuItem.setChecked(true);
        }

// item click listener

        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem item) {
                        switch (item.getItemId()){
                            case R.id.action_for_you:
                                onBackPressed();

                                break;

                        return true;
                    }
                });

    }

@Override
    public void onBackPressed() {
        super.onBackPressed();
    }

1 Ответ

0 голосов
/ 31 мая 2018

Возьмите этот код,

   Menu bottomMenu = bottomNavigationView.getMenu();
    for (int i=0; i<bottomMenu.size(); i++)
    {
        MenuItem bottomMenuItem = bottomMenu.getItem(0);
        bottomMenuItem.setChecked(true);
    }

и поместите его здесь,

@Override
protected void onResume() {
    super.onResume();
    Menu bottomMenu = bottomNavigationView.getMenu();
    for (int i=0; i<bottomMenu.size(); i++)
    {
        MenuItem bottomMenuItem = bottomMenu.getItem(0);
        bottomMenuItem.setChecked(true);
    }
}

, чтобы он обновлялся после возобновления некоторых других действий и возобновления текущего действия.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...