android实现多层红点提示的功能

我们在做APP的时候经常会遇到多层级的提示,例如:有一条私信或者消息到了,那么需要在一级页面二级页面三级页面都要出现小红点的形式来提醒用户有新的消息了,那么我们高效的完成呢?

不用广播!不用sharedPreference!因为低效!

方案就是采用:全局的静态数据+页面生命周期

详细方案如下:

1、创建一个消息计数类,在类中声明1个静态整型,用来保存消息的总量。再生命一个静态时间戳,用来存储变更消息总量的时间戳。再在这个类中写一个显隐方法,当数量大于0显示红点,当数量等于0的时候不显示红点,并变更时间戳。
2、当进入每一级页面的时候,在Activity的onResume或者Fragment的onCreateView的方法里调用上述显隐方法来控制显示和隐藏。
3、当在某一个页面的时候操作红点区域,则修改其静态整型,并调用显隐方法。
4、再回到上一个层级的时候重复第二步即可
注意:在变更静态整型变量的时候,需要对比本地存储区该变量的时间戳和服务器的时间戳,以服务器最新的时间戳的变量为最新。当点击该红点处或者页面的时候,也应该更新本地时间戳。或者后台添加index的字段,来代替时间戳

红点一般采用9图或者自定义红点

自定义红点:http://www.jb51.net/article/103994.htm

自定义BadgeView

常用方法:
1.setBadgeCount(int):设置提醒数字
2.setBadgeGravity(Gravity):设置位置布局
3.setTargetView(View):设置提示控件对象
4.setTypeface():设置显示字体
5.setShadowLayer():设置字体阴影
6.setBackgroundColor():设置背景色
7.setBackgroundResource():设置背景图片

package com.jauker.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView.BufferType;

public class BadgeView extends TextView {
    private boolean mHideOnNull;

    public BadgeView(Context context) {
        this(context, (AttributeSet)null);
    }

    public BadgeView(Context context, AttributeSet attrs) {
        this(context, attrs, 16842884);
    }

    public BadgeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mHideOnNull = true;
        this.init();
    }

    private void init() {
        if(!(this.getLayoutParams() instanceof LayoutParams)) {
            LayoutParams layoutParams = new LayoutParams(-2, -2, 53);
            this.setLayoutParams(layoutParams);
        }

        this.setTextColor(-1);
        this.setTypeface(Typeface.DEFAULT_BOLD);
        this.setTextSize(2, 11.0F);
        this.setPadding(this.dip2Px(5.0F), this.dip2Px(1.0F), this.dip2Px(5.0F), this.dip2Px(1.0F));
        this.setBackground(9, Color.parseColor("#d3321b"));
        this.setGravity(17);
        this.setHideOnNull(true);
        this.setBadgeCount(0);
    }

    public void setBackground(int dipRadius, int badgeColor) {
        int radius = this.dip2Px((float)dipRadius);
        float[] radiusArray = new float[]{(float)radius, (float)radius, (float)radius, (float)radius, (float)radius, (float)radius, (float)radius, (float)radius};
        RoundRectShape roundRect = new RoundRectShape(radiusArray, (RectF)null, (float[])null);
        ShapeDrawable bgDrawable = new ShapeDrawable(roundRect);
        bgDrawable.getPaint().setColor(badgeColor);
        this.setBackgroundDrawable(bgDrawable);
    }

    public boolean isHideOnNull() {
        return this.mHideOnNull;
    }

    public void setHideOnNull(boolean hideOnNull) {
        this.mHideOnNull = hideOnNull;
        this.setText(this.getText());
    }

    public void setText(CharSequence text, BufferType type) {
        if(!this.isHideOnNull() || text != null && !text.toString().equalsIgnoreCase("0")) {
            this.setVisibility(0);
        } else {
            this.setVisibility(8);
        }

        super.setText(text, type);
    }

    public void setBadgeCount(int count) {
        this.setText(String.valueOf(count));
    }

    public Integer getBadgeCount() {
        if(this.getText() == null) {
            return null;
        } else {
            String text = this.getText().toString();

            try {
                return Integer.valueOf(Integer.parseInt(text));
            } catch (NumberFormatException var3) {
                return null;
            }
        }
    }

    public void setBadgeGravity(int gravity) {
        LayoutParams params = (LayoutParams)this.getLayoutParams();
        params.gravity = gravity;
        this.setLayoutParams(params);
    }

    public int getBadgeGravity() {
        LayoutParams params = (LayoutParams)this.getLayoutParams();
        return params.gravity;
    }

    public void setBadgeMargin(int dipMargin) {
        this.setBadgeMargin(dipMargin, dipMargin, dipMargin, dipMargin);
    }

    public void setBadgeMargin(int leftDipMargin, int topDipMargin, int rightDipMargin, int bottomDipMargin) {
        LayoutParams params = (LayoutParams)this.getLayoutParams();
        params.leftMargin = this.dip2Px((float)leftDipMargin);
        params.topMargin = this.dip2Px((float)topDipMargin);
        params.rightMargin = this.dip2Px((float)rightDipMargin);
        params.bottomMargin = this.dip2Px((float)bottomDipMargin);
        this.setLayoutParams(params);
    }

    public int[] getBadgeMargin() {
        LayoutParams params = (LayoutParams)this.getLayoutParams();
        return new int[]{params.leftMargin, params.topMargin, params.rightMargin, params.bottomMargin};
    }

    public void incrementBadgeCount(int increment) {
        Integer count = this.getBadgeCount();
        if(count == null) {
            this.setBadgeCount(increment);
        } else {
            this.setBadgeCount(increment + count.intValue());
        }

    }

    public void decrementBadgeCount(int decrement) {
        this.incrementBadgeCount(-decrement);
    }

    public void setTargetView(TabWidget target, int tabIndex) {
        View tabView = target.getChildTabViewAt(tabIndex);
        this.setTargetView(tabView);
    }

    public void setTargetView(View target) {
        if(this.getParent() != null) {
            ((ViewGroup)this.getParent()).removeView(this);
        }

        if(target != null) {
            if(target.getParent() instanceof FrameLayout) {
                ((FrameLayout)target.getParent()).addView(this);
            } else if(target.getParent() instanceof ViewGroup) {
                ViewGroup parentContainer = (ViewGroup)target.getParent();
                int groupIndex = parentContainer.indexOfChild(target);
                parentContainer.removeView(target);
                FrameLayout badgeContainer = new FrameLayout(this.getContext());
                android.view.ViewGroup.LayoutParams parentlayoutParams = target.getLayoutParams();
                badgeContainer.setLayoutParams(parentlayoutParams);
                target.setLayoutParams(new android.view.ViewGroup.LayoutParams(-1, -1));
                parentContainer.addView(badgeContainer, groupIndex, parentlayoutParams);
                badgeContainer.addView(target);
                badgeContainer.addView(this);
            } else if(target.getParent() == null) {
                Log.e(this.getClass().getSimpleName(), "ParentView is needed");
            }

        }
    }

    private int dip2Px(float dip) {
        return (int)(dip * this.getContext().getResources().getDisplayMetrics().density + 0.5F);
    }
}

调用的地方


mTipText2 = (TextView) findViewById(R.id.id_activity_info_tip_tv2);

BadgeView badgeView3 = new BadgeView(this); 
badgeView3.setTargetView(mTipText3); 
badgeView3.setBadgeGravity(Gravity.TOP | Gravity.LEFT); 
badgeView3.setTypeface(Typeface.create(Typeface.SANS_SERIF, 
  Typeface.ITALIC)); 
badgeView3.setShadowLayer(2, -1, -1, Color.GREEN); 
badgeView3.setBadgeCount(2);

其他参考链接:http://www.cnblogs.com/Sweet-Candy/p/5669815.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值