Android RecyclerView使用

1.导入依赖

implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.35'

2.写一个Layout布局装载RecyclerView

<?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"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="#F2F1F6"style="@style/xxx"><!-- 标题栏 --><RelativeLayout android:id="@+id/title_bar"android:layout_width="match_parent"android:layout_height="@dimen/dimen_titltbar_height"android:layout_gravity="center_vertical"android:background="@drawable/xxx"><ImageViewandroid:id="@+id/backhome"style="@style/xxx" /><TextViewandroid:id="@+id/nfctitle"style="@style/aj_new_title_bar_title_text"android:text="@string/xxx" /></RelativeLayout><Buttonandroid:id="@+id/test_btn"android:text="测试"android:layout_width="wrap_content"android:layout_height="wrap_content"/><!-- 正文 --><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/product_rv"android:padding="10dp"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>

3.Acitivity

public class AllActivity extends AppCompatActivity {private Disposable subscribe;RecyclerView product_rv;@AllArgsConstructorpublic static class Args {int[] viewIdArray;String[] moduleNames;AutoDto[][] autoDtos;}private final static String TAG = "AllActivity";ImageView backhome;Button button;private Unbinder bind;@SuppressLint("CheckResult")@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity);backhome = findViewById(R.id.backhome);product_rv = findViewById(R.id.product_rv);button = findViewById(R.id.test_btn);subscribe = Observable.fromCallable(() -> {List<ProductCateDto> productList = new LinkedList<>();return productList;}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<List<ProductCateDto>>() {@Overridepublic void accept(List<ProductCateDto> productCateDtos) throws Exception {GridRecyclerAdapter adapter = new GridRecyclerAdapter(AllActivity.this, productCateDtos);GridSpacingItemDecoration gridSpacingItemDecoration = new GridSpacingItemDecoration( 13, true);//Grid布局GridLayoutManager gridLayoutManager = new GridLayoutManager(AllActivity.this, 4);product_rv.setLayoutManager(gridLayoutManager);//这里用线性宫格显示 类似于grid viewproduct_rv.setAdapter(adapter);product_rv.addItemDecoration(gridSpacingItemDecoration);product_rv.setClipToPadding(false);DefaultItemAnimator defaultItemAnimator = new DefaultItemAnimator();defaultItemAnimator.setAddDuration(1000);defaultItemAnimator.setRemoveDuration(1000);product_rv.setItemAnimator(defaultItemAnimator);/*** 设置item所占的格子*/gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {@Overridepublic int getSpanSize(int position) {return adapter.isHeader(position) ? gridLayoutManager.getSpanCount() : 1;}});button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {//自己随便设一个随机集合List<ProductCateDto> list = Arrays.asList();Random random = new Random();int i = random.nextInt(2);ProductCateDto productCateDto1 = list.get(i);LinkedList<ProductCateDto> productCateDtos2 = new LinkedList<>(productCateDtos);productCateDtos2.addFirst(productCateDto1);adapter.updateList(productCateDtos2);}});}}, new Consumer<Throwable>() {@Overridepublic void accept(Throwable throwable) throws Exception {Log.e("TAG", "accept: " + throwable.getMessage());}});init();}public void init() {backhome.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent = new Intent(AllActivity.this, Activity_other.class);startActivity(intent);startAini(true);finish();}});}/*** Intent Switching animation** @param flag*/public void startAini(boolean flag) {if (flag) {overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);} else {overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);}}@Overrideprotected void onDestroy() {super.onDestroy();System.out.println("onDestroy");if (bind != null) {//解绑视图bind.unbind();}if (subscribe != null) {subscribe.dispose();}}
}

4.RecyclerView适配器

public class GridRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {public static final Integer HEADER = 0;public static final Integer BODY = 1;private Activity context;List<ProductCateDto> productCateDtoList;public boolean isHeader(int position) {return productCateDtoList.get(position).getType().equals(HEADER);}public void updateList(List<ProductCateDto> newList) {MyDiffCallback<ProductCateDto> diffCallback = new MyDiffCallback<>(this.productCateDtoList, newList);DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);this.productCateDtoList.clear();this.productCateDtoList.addAll(newList);diffResult.dispatchUpdatesTo(this);}@Overridepublic int getItemViewType(int position) {ProductCateDto productCateDto = productCateDtoList.get(position);return productCateDto.getType();}public GridRecyclerAdapter(Activity context, List<ProductCateDto> productCateDtoList) {this.context = context;this.productCateDtoList = productCateDtoList;}@NonNull@Overridepublic RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View inflate;if (viewType == HEADER) {inflate = LayoutInflater.from(context).inflate(R.layout.layout_product_category_header, parent, false);return new HeaderViewHolder(inflate);} else {inflate = LayoutInflater.from(context).inflate(R.layout.jmy_grid_item, parent, false);return new BodyViewHolder(inflate);}}@Overridepublic void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {ProductCateDto productCateDto = productCateDtoList.get(position);Integer type = productCateDto.getType();if (type.equals(HEADER)) {HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;headerViewHolder.title_tv.setText(productCateDto.getHeaderName());} else {BodyViewHolder bodyViewHolder = (BodyViewHolder) holder;bodyViewHolder.iv.setImageResource(productCateDto.getImage());bodyViewHolder.tv.setText(productCateDto.getName());bodyViewHolder.itemView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {// Handle item clickIntent intent = new Intent(context, productCateDto.getAClass());context.startActivity(intent);startAini(false);context.finish();}});}}/*** Intent Switching animation** @param flag*/public void startAini(boolean flag) {if (flag) {context.overridePendingTransition(R.anim.push_right_in,R.anim.push_right_out);} else {context.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);}}@Overridepublic int getItemCount() {return productCateDtoList.size();}public static class HeaderViewHolder extends RecyclerView.ViewHolder {TextView title_tv;public HeaderViewHolder(@NonNull View itemView) {super(itemView);title_tv = itemView.findViewById(R.id.title_tv);}}public static class BodyViewHolder extends RecyclerView.ViewHolder {ImageView iv;TextView tv;public BodyViewHolder(@NonNull View itemView) {super(itemView);iv = itemView.findViewById(R.id.iv_item);tv = itemView.findViewById(R.id.tv_item);}}}

5.自动调整item间距

public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {private int spanCount;private int spacing;private boolean includeEdge;public GridSpacingItemDecoration(int spacing, boolean includeEdge) {this.spacing = spacing;this.includeEdge = includeEdge;}@Overridepublic void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {int position = parent.getChildAdapterPosition(view); // item positionRecyclerView.Adapter adapter = parent.getAdapter();int itemViewType = adapter.getItemViewType(position);if (itemViewType==GridRecyclerAdapter.HEADER){spanCount=4;}else {spanCount=1;}int column = position % spanCount; // item columnif (includeEdge) {outRect.left = spacing - column * spacing / spanCount;outRect.right = (column + 1) * spacing / spanCount;} else {outRect.left = column * spacing / spanCount;outRect.right = spacing - (column + 1) * spacing / spanCount;}}
}

6.BaseEntity


import lombok.Getter;
import lombok.Setter;public class BaseEntity {@Getter@Setterprivate String id;
}

都是代码,不解释了

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/27046.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

mysql_ssl_rsa_setup使用详解

mysql_ssl_rsa_setup 是一个MySQL附带的工具&#xff0c;用于自动创建SSL证书和密钥文件&#xff0c;以便在MySQL服务器与客户端之间启用安全的SSL/TLS连接。这对于确保数据传输的安全性是非常重要的&#xff0c;尤其是在不安全的网络环境中。下面是对mysql_ssl_rsa_setup使用的…

AI绘画SD下载安装教程,学习AI绘画软件必看(SD怎么安装,SD安装教程,安装stable diffusion软件必看)

大家好&#xff0c;我是设计师阿威 最近很火很有趋势的便是AI人工智能了&#xff0c;提到AI大家肯定都不陌生&#xff08;AIGC&#xff09;大家也很熟知&#xff0c;但是要问应用的工具有哪些肯定很多人说不出来几个&#xff0c;但是比较厉害的就是大众所认识的SD-stable diff…

Kafka使用教程和案例详解

Kafka 使用教程和案例详解 Kafka 使用教程和案例详解1. Kafka 基本概念1.1 Kafka 是什么?1.2 核心组件2. Kafka 安装与配置2.1 安装 Kafka使用包管理器(如 yum)安装使用 Docker 安装2.2 配置 Kafka2.3 启动 Kafka3. Kafka 使用教程3.1 创建主题3.2 生产消息3.3 消费消息3.4 …

Java 异常处理 -- Java 语言的异常、异常链与断言

大家好,我是栗筝i,这篇文章是我的 “栗筝i 的 Java 技术栈” 专栏的第 009 篇文章,在 “栗筝i 的 Java 技术栈” 这个专栏中我会持续为大家更新 Java 技术相关全套技术栈内容。专栏的主要目标是已经有一定 Java 开发经验,并希望进一步完善自己对整个 Java 技术体系来充实自…

Android 汉字转拼音(两行就够了)

在Android中&#xff0c;我们可以使用Android自带的Transliterator类来实现汉字转拼音的功能。下面是使用Transliterator类的示例代码&#xff1a; 在你的Activity或者工具类中&#xff0c;使用以下代码来实现汉字转拼音的功能&#xff1a; import android.support.v7.app.Ap…

力扣每日一题 6/14 动态规划+数组

博客主页&#xff1a;誓则盟约系列专栏&#xff1a;IT竞赛 专栏关注博主&#xff0c;后期持续更新系列文章如果有错误感谢请大家批评指出&#xff0c;及时修改感谢大家点赞&#x1f44d;收藏⭐评论✍ 2786.访问数组中的位置使分数最大【中等】 题目&#xff1a; 给你一个下标…

JavaSE---类和对象(上)

1. 面向对象的初步认知 1.1 什么是面向对象 Java是一门纯面向对象的语言(Object Oriented Program&#xff0c;简称OOP)&#xff0c;在面向对象的世界里&#xff0c;一切皆为对象。 面向对象是解决问题的一种思想&#xff0c;主要依靠对象之间的交互完成一件事情。用面向对象…

如何用R语言ggplot2画高水平期刊散点图

文章目录 前言一、数据集二、ggplot2画图1、全部代码2、细节拆分1&#xff09;导包2&#xff09;创建图形对象3&#xff09;主题设置4&#xff09;轴设置5&#xff09;图例设置6&#xff09;散点颜色7&#xff09;保存图片 前言 一、数据集 数据下载链接见文章顶部 处理前的数据…

搭建 Tomcat 集群【Nginx 负载均衡】

当我们想要提高后端服务器的并发性能&#xff0c;可以通过分配更多的资源给 Tomcat 服务器&#xff0c;但是这只能提高一部分的性能。因为每台 Tomcat 的服务器是有最大连接数为 200.所以即可拥有无穷无尽的内存&#xff0c;也会因为单台 Tomcat 的原因而无法发挥这些资源的最大…

grpc代理服务的实现(二)

目录 grpc service 的实现grpc服务通过unix域监听请求建立与代理服务的tcp连接请求转发到 unix 上代码地址 grpc service 的实现 假设 grpc service 的服务名是 Bar grpc服务通过unix域监听请求 go svr : grpc.NewServer() messages.RegisterBarServer(svr, bar.New()) reflec…

免杀笔记 ----> 后续更新安排

前一段时间&#xff0c;我疯狂更新了内网&#xff0c;本来想把NTLM-Relay给更上的&#xff0c;但是计划安排不允许了&#xff0c;之后后续再给大家进行深入的内网更新了&#xff01;&#xff01; &#xff1a;&#xff1a; 真不是我托更 嘻嘻嘻~~~ 说回正题&#xff0c;接下来…

算法体系-22 第二十二节:暴力递归到动态规划(四)

一 最小距离累加和 1.1 描述 给定一个二维数组matrix&#xff0c;一个人必须从左上角出发&#xff0c;最后到达右下角 沿途只可以向下或者向右走&#xff0c;沿途的数字都累加就是距离累加和 返回最小距离累加和 1.2 分析

把服务器上的镜像传到到公司内部私有harbor上,提高下载速度

一、登录 docker login https://harbor.cqxyy.net/ -u 账号 -p 密码 二、转移镜像 minio 2024.05版 # 指定tag docker tag minio/minio:RELEASE.2024-05-10T01-41-38Z harbor.cqxyy.net/customer-software/minio:RELEASE.2024-05-10T01-41-38Z# 推送镜像 docker push harbo…

陕西印刷平台的元宇宙之旅:打造创意印刷新纪元

在数字化时代的浪潮中&#xff0c;陕西印刷平台不断追求创新与卓越&#xff0c;将目光投向了新兴的元宇宙领域。作为传统印刷业的先行者&#xff0c;陕西印刷平台致力于将先进的元宇宙技术融入传统印刷之中&#xff0c;为品牌和个人提供独一无二的创意印刷解决方案。此刻&#…

学习笔记——交通安全分析04

目录 前言 当天学习笔记整理 交通行为、心理与安全 结束语 前言 #随着上一轮SPSS学习完成之后&#xff0c;本人又开始了新教材《交通安全分析》的学习 #整理过程不易&#xff0c;喜欢UP就点个免费的关注趴 #本期内容接上一期03笔记 #最近感觉有点懒&#xff0c;接受各位…

【AI应用探讨】— GPT-4o模型应用场景

目录 1. 自然语言处理&#xff08;NLP&#xff09;任务 文本生成 机器翻译 问答系统 2. 聊天机器人与虚拟助手 智能聊天机器人 虚拟助手与陪伴 3. 内容创作与辅助 创意写作 代码生成 4. 教育辅助 学习工具 5. 客户服务与支持 客户服务聊天机器人 技术支持 6. 研…

GitLab教程(六):通过rebase来合并commit

文章目录 1.理解和操作rebase&#xff08;1&#xff09;rebase的逻辑&#xff08;2&#xff09;实践演示 2.rebase的优缺点 1.理解和操作rebase &#xff08;1&#xff09;rebase的逻辑 Git Rebase的基本逻辑是将一个分支的更改移到另一个分支上&#xff0c;同时看起来好像这…

流批一体计算引擎-9-[Flink]中的数量窗与时间窗

1 数量窗 1.1 数量滚动窗口 0基础学习PyFlink——个数滚动窗口(Tumbling Count Windows) 1.1.1 代码分析 Tumbling Count Windows是指按元素个数计数的滚动窗口。 滚动窗口是指没有元素重叠的窗口。 (1)构造了一个KeyedStream&#xff0c;用于存储word_count_data中的数据。…

英伟达算法岗面试,问的贼专业。。。

节前&#xff0c;我们星球组织了一场算法岗技术&面试讨论会&#xff0c;邀请了一些互联网大厂朋友、参加社招和校招面试的同学。 针对算法岗技术趋势、大模型落地项目经验分享、新手如何入门算法岗、该如何准备、面试常考点分享等热门话题进行了深入的讨论。 合集&#x…

接口返回的文字段落换行问题

接口返回的文字段落换行问题 解决方案&#xff1a;文本中包含\r\n, 使用v-html显示文本&#xff0c;并给便签添加&#xff1a;white-space: pre-wrap;