单击android中底部导航菜单的一个位置时如何隐藏/清除徽章?

时间:2017-09-15 06:10:09

标签: android broadcastreceiver badge bottomnavigationview

我在NavigationView隐藏徽章时遇到困难。当我收到任何通知时添加了BottomNavigationNiew位置的徽章,但在点击该位置时无法刷新该批次。

摇篮:

compile 'com.github.ittianyu:BottomNavigationViewEx:1.1.7'
compile 'q.rorbin:badgeview:1.1.0'

MainActivity:

private Badge addBadgeAt(int position, int number) {
        return new QBadgeView(this)
                .setBadgeNumber(number)
                .setGravityOffset(42, 2, true)
                .bindTarget(bottomNavigationView.getBottomNavigationItemView(position)); 
}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    switch (item.getItemId()) {
        case R.id.menu_conection:

            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, ConnectionFragment.newInstance("connection"));
            fragmentTransaction.commit();

            break;
        case R.id.menu_me:
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, MyProfile.newInstance(ARG_TYPE));
            fragmentTransaction.commit();

            break;

        case R.id.menu_notification:

            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, NotificationsFragment.newInstance());
            fragmentTransaction.commit();

            break;
    }

    return true;
}

我只想在点击menu_notification

时删除第二个位置的徽章

2 个答案:

答案 0 :(得分:3)

public static void removeBadge(int position, BottomNavigationViewEx bottomNavigationViewEx, QBadgeView badgeView) {
badgeView.bindTarget(bottomNavigationViewEx.getBottomNavigationItemView(position))
                .hide(true);
    }

答案 1 :(得分:0)

调用addBadgeAt函数时,需要将返回的Badge对象赋值给变量:

mBadge = addBadgeAt(2, 1);

然后,当您想删除徽章时,只需拨打hide方法:

即可
mBadge.hide(true);

注意:似乎调用两次隐藏方法会导致徽章再次显示。

您可以在此处查看文档:{​​{3}}