如何在点击backpress时在BottomNavigationView中突出显示相应的片段项目图标?

时间:2019-01-09 11:09:04

标签: android android-fragments fragment bottomnavigationview

我有一个 BottomNavigationView ,在屏幕底部有 3 item 标签。每个项目标签都有两个片段。我单击此流程, Item1Fragment-> Item2Fragment-> Item3Fragment-> SubItem3Fragment1-> SubItem3Fragment2 。因此,当我从 Item3Fragment backpress 时,突出显示了Item2Fragment Item1Fragment的核心响应项目图标。

我指的是这篇帖子Change BottomNavigationView Icons on Back Button clicked

我需要的是当我从 Item3Fragment 反压时,流程应为 SubItem3Fragment1(突出显示第3个项目图标)-> Item3Fragment(突出显示第3个项目图标)-> Item2Fragment(突出显示第2个项目图标) )-> Item1Fragment(突出显示第一个项目图标)

我正在用SubItemFragments调用    transaction.addToBackStack(“ subfrag”);

Deque<Integer> mStack = new ArrayDeque<>();
boolean isBackPressed  = false;

private void setBottomNavigationView() {
    mBottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.ic_home:
                     if(!isBackPressed) {
                       pushFragmentIntoStack(R.id.ic_home);
                   }
                    isBackPressed = false
                    setFragment(HomeFragment.newInstance(), HOME_FRAGMENT);
                    return true;
                case R.id.ic_search:
                    if(!isBackPressed) {
                       pushFragmentIntoStack(R.id.ic_search);
                   }
                    isBackPressed = false
                    setFragment(SearchFragment.newInstance(), SEARCH_FRAGMENT);
                    return true;
                case R.id.ic_circle:
                    if(!isBackPressed) {
                       pushFragmentIntoStack(R.id.ic_circle);
                   }
                    isBackPressed = false
                    setFragment(ShareFragment.newInstance(), SHARE_FRAGMENT);
                    return true;

                default:
                    return false;
            }
        }
    });
    mBottomNavigationView.setOnNavigationItemReselectedListener(new 
          BottomNavigationView.OnNavigationItemReselectedListener() {
        @Override
        public void onNavigationItemReselected(@NonNull MenuItem item) {

        }
    });
 mBottomNavigationView.setSelectedItemId(R.id.ic_home);
 pushFragmentIntoStack(R.id.ic_home);
}

private void pushFragmentIntoStack(int id)
{
    if(mStack.size() < 3)
    {
        mStack.push(id);
    }
    else
    {
        mStack.removeLast();
        mStack.push(id);
    }
}

private void setFragment(Fragment fragment, String tag) {
    FragmentTransaction transaction = mFragmentManager.beginTransaction();
    transaction.replace(R.id.container, fragment, tag);
    transaction.commit();
}

@Override
public void onBackPressed() {
    if(mStack.size() > 1)
    {
        isBackPressed = true;
        mStack.pop();
        mBottomNavigationView.setSelectedItemId(mStack.peek());
    }
    else 
    {
        super.onBackPressed();
    }
}

1 个答案:

答案 0 :(得分:0)

我通过使用我为 tabItem 分配了 id 的片段名称获得了片段名称,解决了这种情况,然后使用< strong> tabItem ID

 Menu menu = bottomNavigationView.getMenu();
 MenuItem menuItem = menu.getItem(tabId);
 menuItem.setChecked(true);