【Android -- 开源库】BRVAH 的基本使用

在这里插入图片描述

一、简介

BRVAH 是一个强大的 RecyclerAdapter 框架,它能节约开发者大量的开发时间,集成了大部分列表常用需求解决方案。
在这里插入图片描述

二、框架引入

1. 在 build.gradle(Project:XXXX) 的 repositories 添加:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io"}
    }
}

2. 在 build.gradle(Module:app) 的 dependencies 添加:

dependencies {
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
}

三、使用 Adapter

1. 效果图
6.jpg

2. HomeBean.java

/**
 * Created on 2019/11/28 9:30
 *
 * @author Gong Youqiang
 */
public class HomeBean {
    private String name;
    private int iconId;

    public HomeBean(String name, int iconId) {
        this.name = name;
        this.iconId = iconId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getIconId() {
        return iconId;
    }

    public void setIconId(int iconId) {
        this.iconId = iconId;
    }
}

2. HomeAdapter.java

/**
 * Created on 2019/11/28 9:31
 *
 * @author Gong Youqiang
 */
public class HomeAdapter extends BaseQuickAdapter<HomeBean, BaseViewHolder> {

    public HomeAdapter(int layoutResId, @Nullable List<HomeBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeBean item) {
        helper.setText(R.id.tv_name,item.getName());
        helper.setImageResource(R.id.iv_img,item.getIconId());

        CardView cardView = helper.getView(R.id.card_view);

        helper.addOnClickListener(R.id.card_view);
    }
}

3. item_home.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:layout_gravity="center"
    android:foreground="?android:attr/selectableItemBackground"
    card_view:cardBackgroundColor="@color/white"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="2dp"
    card_view:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/iv_img"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:src="@mipmap/ic_launcher"/>

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:text="android"
            android:textColor="@color/black"/>
    </LinearLayout>
</androidx.cardview.widget.CardView>

4. activity_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

5. top_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_home_bg" />
</LinearLayout>

6. Activity

public class LayoutActivity extends AppCompatActivity {
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    HomeAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);

        ButterKnife.bind(this);

        initAdapter();

    }

    private void initAdapter() {
        mAdapter = new HomeAdapter(R.layout.item_home,getHomeItem());
        mAdapter.openLoadAnimation();
        View top = getLayoutInflater().inflate(R.layout.top_view, (ViewGroup) mRecyclerView.getParent(), false);
        mAdapter.addHeaderView(top);

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(LayoutActivity.this,"click"+position,Toast.LENGTH_SHORT).show();
            }
        });

        mRecyclerView.setLayoutManager(new GridLayoutManager(LayoutActivity.this,3));
        mRecyclerView.setAdapter(mAdapter);
    }

    private List<HomeBean> getHomeItem() {
        return Arrays.asList(
                new HomeBean("android",R.mipmap.ic_launcher),
                new HomeBean("ios",R.mipmap.ic_launcher),
                new HomeBean("haha",R.mipmap.ic_launcher),
                new HomeBean("andnnroid",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher),
                new HomeBean("dddd",R.mipmap.ic_launcher)
        );
    }

}

四、常见问题

为什么有数据不显示?
请检查一下你的RecyclerView是否设置了LayoutManager。

为什么有10条数据,只显示1条?
请检查一下item的布局最外层的Layout是不是layout_height设置了match_parent.

数据状态错乱
这个问题无论是RecyclerView还是ListView不做处理都会出现问题,这个本质上是由于布局重用机制导致的,解决办法是通过数据状态来控制控件的状态,一定要设置状态无论什么状态,if和else是少不了的,如下代码:

  if(entity.isCheck){
    checkBox.isChecked(true);
  } else {
    checkBox.isChecked(false);
  }

推荐阅读

BRVAH官方使用指南

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kevin-Dev

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值