Android BottomNavigationView添加消息提示(一)

来源

  • 依赖
implementation 'com.google.android.material:material:1.0.0'
  • 首先要写一个徽章布局notifications_badge.xml,我页面做了适配所以用的pt单位,也可以替换为dp,看项目情况.
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/notifications_badge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:layout_marginLeft="26pt"
        android:layout_marginTop="14pt"
        android:background="@drawable/notification_badge"
        android:gravity="center"
        android:text="1+"
        android:textColor="@color/white"
        android:textSize="16pt" />
</merge>

notification_badge是用shape画的红色圆点

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
        android:width="28pt"
        android:height="28pt" />
    <solid android:color="@color/read" />
</shape>

我导航栏高度是100pt,所以圆点直径是26pt,将近导航栏的三分之一高度.然后导航栏图片是宽是75px,所以android:layout_marginLeft="26pt"可以让小红点尽量靠近ItemView的右边.可以自己调试下就清楚了.

  • 然后stackoverflow中答案封装成简单的工具类,如下.
public class BNVNotifacationBadgeUtil {
    
    
    /**
     * 添加消息徽章
     *
     * @param itemIndex 表示添加到第几个ItemView
     * @param desc      每个徽章上的内容是啥
     */
    public static void addNotificationBadge(BottomNavigationView mBNV, int itemIndex, String desc) {
    
    
        BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
        LayoutInflater.from(mBNV.getContext()).inflate(R.layout.layout_notification_badge, mCurrentItemView, true);
        TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
        if (mBadge != null) {
    
    
            mBadge.setText(desc);
        }
    }
    /**
     * 移除消息徽章
     *
     * @param itemIndex
     */
    public static void removeNotificationBadge(BottomNavigationView mBNV, int itemIndex) {
    
    
        BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
        TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
        if (mBadge != null) {
    
    
            ((ViewGroup) mBadge.getParent()).removeView(mBadge);
        }
    }
    /**
     * 修改消息徽章上面的内容
     *
     * @param itemIndex
     * @param desc
     */
    public static void modifyNotificationBadgeContent(BottomNavigationView mBNV, int itemIndex, String desc) {
    
    
        BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
        TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
        if (mBadge != null) {
    
    
            mBadge.setText(desc);
        }
    }
    /**
     * 判断当前索引对应的ItemView中是否有徽章
     *
     * @param mBNV
     * @param itemIndex
     */
    public static Boolean judgeBadgeExist(BottomNavigationView mBNV, int itemIndex) {
    
    
        BottomNavigationItemView mCurrentItemView = getBottomNavigationItemView(mBNV, itemIndex);
        TextView mBadge = (TextView) mCurrentItemView.findViewById(R.id.notifications_badge);
        if (mBadge == null) {
    
    
            return false;
        } else {
    
    
            return true;
        }
    }
    /**
     * 获取当前索引对应的ItemView对象
     *
     * @param mBNV
     * @param itemIndex
     * @return
     */
    private static BottomNavigationItemView getBottomNavigationItemView(BottomNavigationView mBNV, int itemIndex) {
    
    
        BottomNavigationMenuView bottomNavigationMenuView = (BottomNavigationMenuView) mBNV.getChildAt(0);
        return (BottomNavigationItemView) bottomNavigationMenuView.getChildAt(itemIndex);
    }
}

猜你喜欢

转载自blog.csdn.net/MoLiao2046/article/details/108322163