| |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| -> 移动开发 -> Android RecyclerView简单又万能适配器BaseRecyclerViewAdapterHelper+SuperTextView -> 正文阅读 |
|
|
[移动开发]Android RecyclerView简单又万能适配器BaseRecyclerViewAdapterHelper+SuperTextView |
|
Android开发过程中大家一定或多或少都碰到过RecyclerView写适配器的麻烦,重复写,代码篇幅长,等等一系列的问题。今天就说说BaseRecyclerViewAdapterHelper这个帮助 节省代码量的万能适配器了,最简单的使用,能改减少适配器中一半以上的代码量。但是只是这样还是不够简单便捷,再加上一个万能的控件SuperTextView,那才可以叫做真!万能适配器!先给大家上这两个库的GitHub地址: 然后开始我们的RecyclerView万能适配器的使用步骤: 首先先在项目中集成 在app下的build.gradle文件中的dependencies加入 ?implementation 'com.github.lygttpod:SuperTextView:2.4.6'? 超级TextVie万能控件 implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.7'? 万能适配器 记得保证根目录的 build.gradle下的repositories已经加入了 ?allprojects { ???????? repositories { ??????????? ... ??????????? maven { url "https:jitpack.io" } ??????? } ??? } 正式使用前我们得先了解一下SuperTextView, 下图是SuperTextView的作者给的一些效果图,其实大家理解SuperTextView就是一个能帮我们生成好我们常用的一些TextView、ImageView、checkBox等等一些控件并且帮我们摆放好,甚至做好了我们常用的某些效果的的工具。
为了方便大家理解,我使用SuperTextView部分属性简单写了一个效果,相信大家都能一眼就明白这些意思。
? ?SuperTextView还有很多很多的属性设置,这些属性通通都可以在代码中以及xml中去使用。
如果这些属性还不能完全满足你的要求,可以直接获取你想要修改的地方,再加工做到你需要的效果,例如你想要获取左上的文字只需要: SuperTextView.getLeftTopTextView(); 这样即可得到左上角文字返回的TextView,其他的地方也一样以此类推即可得到相对应的控件。 在了解SuperTextView的使用以后就是BaseRecyclerViewAdapterHelper的使用了。通常我们在创建适配器的时候都需要一个相对应的layout文件来写适配器的Item,但是基于但是基于SuperTextView,我们的layout文件就能变得很简单, ?只需要一个SuperTextView就能满足百分之八十左右的Item需求了。 <?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="wrap_content">
<com.allen.library.SuperTextView
android:id="@+id/stv"
android:padding="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
有了Item的layout文件以后我们就可以开始写适配器了。 新建一个class需要继承BaseQuickAdapter<>里面有两个泛型, 第一个是实体类,第二个直接写BaseViewHolder, 是为了支持扩展ViewHolder, 因为我们的目的是做一个万能的适配器我们需要支持所有数据类型,所以第一个实体类我们直接用Object,之后需要操作数据的时候我们再通过类型判断来强制转换成我们需要的类型即可。然后就能生成一个构造函数,以及一个convert方法,主要操作是在convert,我们这里通过构造函数传进来一个类型值,判断当前适配器需要的数据类型,以及SuperTextView需要实现的样式。 我们在convert方法中获取我们的SuperTextView控件,然后根据类型值判断需要转换的数据类型。再根据自己的需求去完成SuperTextView需要的样式,并且赋值,就完成了我们的万能适配器了。看了一大推说明,相信大家都看烦了。 小二!上代码! 首先是两个数据类型。 //一个商品相关Bean
public class MainBean {
private String name; //商品名称
private String num; //商品数量
private String price; //商品价格
public MainBean(String name, String num, String price) {
this.name = name;
this.num = num;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
另一个数据类型 //一个学生成绩相关Bean
public class SubBean {
private String name; //学生姓名
private String curriculum; //科目
private String number; //学号
private String fraction; //分数
public SubBean(String name, String curriculum, String number, String fraction) {
this.name = name;
this.curriculum = curriculum;
this.number = number;
this.fraction = fraction;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCurriculum() {
return curriculum;
}
public void setCurriculum(String curriculum) {
this.curriculum = curriculum;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getFraction() {
return fraction;
}
public void setFraction(String fraction) {
this.fraction = fraction;
}
}
接下来我在Activity中写两个RecyclerView, 通过一个适配器实现使用两个不同的数据类型实现两个效果,首先看看xml。 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".AdapterActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="效果1,商品列表"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="效果2,学生成绩列表"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_sub"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
很简单,就两个RecycleView 和两个TextView
接下来就是适配器代码: public class Adapter extends BaseQuickAdapter<Object, BaseViewHolder> {
private int type;
//type为0是商品, 为1是成绩
public Adapter(int type) {
super(R.layout.item_adapter);
this.type = type;
}
@Override
protected void convert(BaseViewHolder baseViewHolder, Object o) {
SuperTextView stv = baseViewHolder.getView(R.id.stv);
//根据type区分使用的数据类型以及样式
switch (type) {
case 0:
//数据类型强制转换商品列表数据
MainBean main = (MainBean) o;
//左边文字
stv.setLeftString("商品名:" + main.getName());
//中间文字
stv.setCenterString("商品价格:" + main.getPrice());
//右边文字
stv.setRightString("商品数量:" + main.getNum());
break;
case 1:
//数据类型强制转换学生成绩列表数据
SubBean sub = (SubBean) o;
//左上文字
stv.setLeftTopString(sub.getName());
//左下文字
stv.setLeftBottomString(sub.getNumber());
//右上文字
stv.setRightTopString(sub.getCurriculum());
//右下文字
stv.setRightBottomString(sub.getFraction());
break;
}
}
}
最后就是使用,Activity代码 public class AdapterActivity extends AppCompatActivity {
//商品列表控件
private RecyclerView rv_main;
//成绩列表控件
private RecyclerView rv_sub;
//商品数据
private List<MainBean> mainList = new ArrayList<>();
//成绩数据
private List<SubBean> subList = new ArrayList<>();
//万能适配器
private Adapter universalAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adapter);
//实例化
rv_main = findViewById(R.id.rv_main);
rv_sub = findViewById(R.id.rv_sub);
//给数据上值
mainList.add(new MainBean("冬瓜","50","0.98"));
mainList.add(new MainBean("西瓜","30","0.77"));
mainList.add(new MainBean("哈密瓜","70","0.88"));
mainList.add(new MainBean("傻瓜","1","250"));
subList.add(new SubBean("张三","语文","000012","90"));
subList.add(new SubBean("张三","数学","000012","88"));
subList.add(new SubBean("张三","英语","000012","92"));
subList.add(new SubBean("张三","计算机","000012","0"));
setMainAdapter();
setSubAdapter();
}
private void setMainAdapter() {
//实例化适配器
universalAdapter = new Adapter(0);
//配置RecyclerView
rv_main.setLayoutManager(new LinearLayoutManager(this));
rv_main.setAdapter(universalAdapter);
//给适配器设置数据
universalAdapter.setList(mainList);
//给Item点击事件
universalAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
@Override
public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
//这样可以获取到点击行的数据 如何使用我就不赘述了
adapter.getData().get(position);
}
});
}
private void setSubAdapter() {
//实例化适配器
universalAdapter = new Adapter(1);
//配置RecyclerView
rv_sub.setLayoutManager(new LinearLayoutManager(this));
rv_sub.setAdapter(universalAdapter);
//给适配器设置数据
universalAdapter.setList(subList);
}
}
最后我们看看跑起来的样子
就这样,万能适配器的代码就讲完了,实际代码其实很少,主要是为了方便新手们理解我讲得稍微有点啰嗦了。 其实BaseRecyclerViewAdapterHelper还有很多功能大家可以自己去拓展,比如下拉菜单啊,刷新加载更多之类,SuperTextView和BaseRecyclerViewAdapterHelper远远比我介绍的强大很多很多,大家可以去github上看看源码,掌握更多的用法。 另外本人创建了个微信公众号? 啊哈Android? 打算专门用来和大家一起讨论分享自己开发工作过程中遇到的有趣方便的工具,如果能帮到或者你也有兴趣就来关注关注吧? 谢谢谢谢? 之后也还会在公众号发视频类教程,大家一起多多讨论吧 , 在公众号中回复“适配器”就能看到这篇文章咯~~~~~
|
|
|
| 移动开发 最新文章 |
| Vue3装载axios和element-ui |
| android adb cmd |
| 【xcode】Xcode常用快捷键与技巧 |
| Android开发中的线程池使用 |
| Java 和 Android 的 Base64 |
| Android 测试文字编码格式 |
| 微信小程序支付 |
| 安卓权限记录 |
| 知乎之自动养号 |
| 【Android Jetpack】DataStore |
|
|
| 上一篇文章 下一篇文章 查看所有文章 |
|
|
开发:
C++知识库
Java知识库
JavaScript
Python
PHP知识库
人工智能
区块链
大数据
移动开发
嵌入式
开发工具
数据结构与算法
开发测试
游戏开发
网络协议
系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程 数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁 |
| 360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年11日历 | -2025/11/29 2:08:10- |
|
| 网站联系: qq:121756557 email:121756557@qq.com IT数码 |