Android中的BGABadgeView未读消息提示小红点

当用户收到未读消息的时候需要一个徽章来提示,也就是控件上面的一个小红点,下面就来描述一下小红点的实现方法。

首先贴出BGABadgeView的Github地址
https://github.com/bingoogolapple/BGARefreshLayout-Android

我们在项目中首先要导入三个依赖
compile 'cn.bingoogolapple:bga-badgeview:1.1.3@aar'
compile 'cn.bingoogolapple:bga-adapter:1.1.5@aar'
compile 'com.android.support:recyclerview-v7:25.3.1'

一个是BGA布局的依赖,一个是BGA适配器的依赖,最后一个就是项目中要用到的RecyclerView布局依赖。下面就介绍一个简单的例子来使用BGA自定义的控件。
首先我们创建activity_main.xml布局文件,在里面定义一个RecyclerView布局。

"http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    .support.v7.widget.RecyclerView
        android:id="@+id/general_rcview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    .support.v7.widget.RecyclerView>

接下来我们给RecyclerView添加item布局,需要注意的是,如果控件中需要添加小红点的功能,就必须使用BGABadgeView中的控件。
app:badge_gravity=”rightTop”设置小红点在宿主控件中的位置,此时为右上。

.bingoogolapple.badgeview.BGABadgeLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:badge_gravity="rightTop"
    android:id="@+id/bga_linear"
    android:padding="5dp">
    .bingoogolapple.badgeview.BGABadgeTextView
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:text="title"
        android:textSize="20sp"
        android:textStyle="bold"
        android:id="@+id/bga_title"/>
    .bingoogolapple.badgeview.BGABadgeTextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="content"
        android:textSize="15sp"
        android:id="@+id/bga_content"/>
.bingoogolapple.badgeview.BGABadgeLinearLayout>

布局文件既然都写完了,那么我们就要给RecyclerView自定义一个适配器了,这里的我们自定义的适配器也要继承于BGARecyclerViewAdapter。

public class BGAAdapter extends BGARecyclerViewAdapter<Data> {

    public BGAAdapter(RecyclerView recyclerView) {
        //加载RecyclerView中的子布局item
        super(recyclerView,R.layout.item);
    }

    @Override
    protected void fillData(BGAViewHolderHelper helper, int position, Data model) {
        //helper为BGAViewHolderHelper中的一个对象,作者把一些方法封装在了这个类中
        //通过封装的helper对象给item布局中的控件适配数据
        //model为我们定义的数据类Data的一个对象
        helper.setText(R.id.bga_title,model.title);
        helper.setText(R.id.bga_content,model.content);
        //找到我们需要添加小红点的控件,并且设置未读消息的数目
        BGABadgeLinearLayout badge=helper.getView(R.id.bga_linear);
        badge.showTextBadge(""+model.count);
    }
}
public class Data {
    public String title;
    public String content;
    public int count;
    public Data(String title,String content,int count){
        this.content=content;
        this.title=title;
        this.count=count;
    }
    public static List getData(){
        List messages = new ArrayList<>();
        Random random = new Random();
        //通过循环静态给数组添加数据
        for (int i = 0; i < 30; i++) {
            messages.add(new Data("title"+i,"content"+i,random.nextInt(20)));
        }
        return messages;
    }
}

好的,既然我们布局文件写完了,适配器也写完了,那么最后一步就是在MainActivity中将我们的布局和适配器建立连接了。

public class MainActivity extends AppCompatActivity {

    public BGAAdapter adapter;
    public RecyclerView recyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
    }
    public void initData(){
        //找到RecyclerView布局,然后设置布局的样式
        recyclerView=(RecyclerView)findViewById(R.id.general_rcview);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));
        adapter=new BGAAdapter(recyclerView);
        //设置item的点击事件
        adapter.setOnRVItemClickListener(new BGAOnRVItemClickListener() {
            @Override
            public void onRVItemClick(ViewGroup parent, View itemView, int position) {
                Toast.makeText(MainActivity.this, "点击item"+position, Toast.LENGTH_SHORT).show();
            }
        });
        //setData()方法为作者封装的一个方法,我们给适配器传数据的时候
        // 只需要在setData方法中给定一个数组即可,是不是很方便
        adapter.setData(Data.getData());
        recyclerView.setAdapter(adapter);
    }
}

这篇文章只是简单介绍了小红点在RecyclerView中的用法,除此以外小红点在很多控件中都可以使用,大家可以去尝试着用一用,下面图片是此次demo的效果图,需要的话还可以添加很多自定义的控件。

Android中的BGABadgeView未读消息提示小红点_第1张图片

你可能感兴趣的:(一个程序员的爬坑之路)