您当前的位置: 首页 >  android

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

发布时间:2019-12-06 09:55:35 ,浏览量:5

在这里插入图片描述

一、简介

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

 <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 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 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" />  @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官方使用指南

关注
打赏
1688896170
查看更多评论

暂无认证

  • 5浏览

    0关注

    115984博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0452s