Android渲染一个列表的过程,并提供动态改变样式
1、index.xml
布局文件,我省略了其他代码,我们需要recyclerview保证在规定范围内,如果列表元素过多可以滑动
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:clipToPadding="false" /></LinearLayout>
2、item_linear_layout.xml
item的布局文件
<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"><LinearLayoutandroid:id="@+id/container"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#FFFFFF"android:backgroundTint="#80FFFFFF"android:outlineProvider="background"android:orientation="vertical"><!-- 在这里可以放一些固定内容 --></LinearLayout></FrameLayout>
3、RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {private List<LinearLayout> linearLayouts = new ArrayList<>();private int[] data;public void updateData(int[] newData) {this.data = newData;Logger.d("updateData");notifyDataSetChanged(); // 通知RecyclerView刷新}@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {LayoutInflater inflater = LayoutInflater.from(parent.getContext());View view = inflater.inflate(R.layout.item_linear_layout, parent, false);return new ViewHolder(view);}@Overridepublic void onBindViewHolder(@NonNull ViewHolder holder, int position) {LinearLayout linearLayout = linearLayouts.get(position);holder.bind(linearLayout);}@Overridepublic int getItemCount() {return linearLayouts.size();}public void addLinearLayout(LinearLayout linearLayout, int i) {linearLayouts.add(linearLayout);notifyItemInserted(linearLayouts.size() - 1);}static class ViewHolder extends RecyclerView.ViewHolder {LinearLayout container;public ViewHolder(@NonNull View itemView) {super(itemView);container = itemView.findViewById(R.id.container);}public void bind(LinearLayout linearLayout) {ViewGroup container = itemView.findViewById(R.id.container);if (linearLayout.getParent() != null) {((ViewGroup) linearLayout.getParent()).removeView(linearLayout);}container.removeAllViews();container.addView(linearLayout);}}
}
3、LayoutHelper
我们可以把可能需要动态改变的元素保存到数组中,如textviews、dotImages、innerLinearLayouts,在我们动态创建时把各个元素存储到对应数组中,写一个updateStatusViews方法用于局部渲染,这样就不用大面积刷新了,当后端websocket推送更新消息时,直接调用就可以了
public static RecyclerViewAdapter adapter;public static TextView []textViews;public static ImageView []dotImages;public static LinearLayout []innerLinearLayouts;public static int len = 22;public static void setupWasherLayout(Context context, RecyclerView recyclerView) {recyclerView.setLayoutManager(new GridLayoutManager(context, 4));adapter = new RecyclerViewAdapter();recyclerView.setAdapter(adapter);int []arr = {1,0,1,0,0,1,2,0,1,0,2,1,0,0,1,0,0,0,1,2,0,0};textViews = new TextView[len];dotImages = new ImageView[len];innerLinearLayouts = new LinearLayout[len];for (int i = 0; i < arr.length; i++) {LinearLayout innerLinearLayout = createWasherItemLayout(context, i,arr[i]);adapter.addLinearLayout(innerLinearLayout, i);}}private static LinearLayout createWasherItemLayout(Context context, final int index, final int status) {// 创建一个外层LinearLayout,垂直布局final LinearLayout innerLinearLayout = new LinearLayout(context);LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(200, // 设置宽度为200dp100 // 设置高度为100dp);layoutParams.setMargins(20, 20, 20, 20); // 设置间距innerLinearLayout.setLayoutParams(layoutParams);innerLinearLayout.setOrientation(LinearLayout.HORIZONTAL); // 垂直布局innerLinearLayout.setBackgroundColor(Color.parseColor("#987988"));// 设置背景颜色并包含透明度int backgroundColorWithAlpha = Color.argb(50, 152, 121, 136); // 128 表示透明度innerLinearLayout.setBackgroundColor(backgroundColorWithAlpha);// 添加一个ImageView来显示本地图片final ImageView imageView = new ImageView(context);imageView.setLayoutParams(new LinearLayout.LayoutParams(60, // 设置宽度为60dp80 // 设置高度为80dp));imageView.setImageResource(R.mipmap.washer_item); // 替换为你的图片资源// 添加一个TextView来显示洗衣机编号final TextView textView = new TextView(context);textView.setText((index + 1) + "号洗衣机");textView.setTypeface(null, Typeface.BOLD); // 设置文本加粗LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, // 宽度包裹内容LinearLayout.LayoutParams.WRAP_CONTENT // 高度包裹内容);textLayoutParams.setMargins(10, 0, 0, 0); // 设置左边边距textView.setLayoutParams(textLayoutParams);// 创建一个垂直的LinearLayout来包含“故障”文本和圆点图片final LinearLayout infoLayout = new LinearLayout(context);infoLayout.setOrientation(LinearLayout.VERTICAL);// 创建一个水平的LinearLayout来包含“故障”文本和圆点图片final LinearLayout statusLayout = new LinearLayout(context);statusLayout.setOrientation(LinearLayout.HORIZONTAL);// 添加圆点图片final ImageView dotImage = new ImageView(context);LinearLayout.LayoutParams dotImageParams = new LinearLayout.LayoutParams(10, // 设置宽度为10dp10 // 设置高度为10dp);dotImageParams.setMargins(0, 0, 5, 0);dotImage.setLayoutParams(dotImageParams);// 添加圆点图片到statusLayoutstatusLayout.addView(dotImage);// 添加文本final TextView statusText = new TextView(context);// 添加文本到statusLayoutstatusLayout.addView(statusText);textViews[index] = statusText;dotImages[index] = dotImage;// 添加ImageView和TextView到innerLinearLayoutinnerLinearLayout.addView(imageView);infoLayout.addView(textView);infoLayout.addView(statusLayout);innerLinearLayout.addView(infoLayout);// 设置innerLinearLayout水平和垂直居中innerLinearLayout.setGravity(Gravity.CENTER);statusLayout.setGravity(Gravity.CENTER);innerLinearLayouts[index] = innerLinearLayout;// 在这里更新图片和文本updateStatusViews(status, index,context);// 为innerLinearLayout添加点击事件监听器return innerLinearLayout;}// todo 更新状态视图private static void updateStatusViews(int status,int index,Context context) {switch (status){case 0:dotImages[index].setImageResource(R.mipmap.grey_dot);textViews[index].setText("空闲");break;case 1:dotImages[index].setImageResource(R.mipmap.green_dot);textViews[index].setText("使用中");break;case 2:dotImages[index].setImageResource(R.mipmap.red_dot);textViews[index].setText("设备故障");break;default:}innerLinearLayouts[index].setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View view) {switch (status){case 0:Intent intent = new Intent();intent.setClass(context, WasherActivity.class);context.startActivity(intent);break;case 1:Toast.makeText(context, "被人用啦~", Toast.LENGTH_SHORT).show();break;case 2:Toast.makeText(context, (index + 1) + " 号洗衣机连接错误", Toast.LENGTH_SHORT).show();break;default:Toast.makeText(context, (index + 1) + " 号洗衣机连接错误", Toast.LENGTH_SHORT).show();}}});}
相关文章:
Android渲染一个列表的过程,并提供动态改变样式
1、index.xml 布局文件,我省略了其他代码,我们需要recyclerview保证在规定范围内,如果列表元素过多可以滑动 <LinearLayoutandroid:layout_width"match_parent"android:layout_height"match_parent"android:layout_…...
Leetcode—260.只出现一次的数字III【中等】
2023每日刷题(三) Leetcode—260.只出现一次的数字III 借助lowbit的解题思想 参考的灵茶山艾府大神的题解 实现代码 /*** Note: The returned array must be malloced, assume caller calls free().*/ int* singleNumber(int* nums, int numsSize, in…...
Mysql 约束,基本查询,复合查询与函数
文章目录 约束空属性约束默认值约束zerofill主键约束自增长约束唯一键约束外键约束 查询select的执行顺序单表查询排序 updatedelete整张表的拷贝复合语句group by分组查询 函数日期函数字符串函数数学函数其他函数 复合查询合并查询union 约束 空属性约束 两个值:…...
web前端基础CSS------美化页面“footer”部分
一,实验代码 <!DOCTYPE html> <html><head><meta charset"utf-8"><title>关于我们</title><style type"text/css">#footer{margin: 10px 0px;background: #f5f5f5;border: top 1px solid #eee ;}#f…...
在中国,技术到底有多有用?
🙌秋名山码民的主页 😂oi退役选手,Java、大数据、单片机、IoT均有所涉猎,热爱技术,技术无罪 🎉欢迎关注🔎点赞👍收藏⭐️留言📝 获取源码,添加WX 目录 前言1.…...
《动手学深度学习 Pytorch版》 9.2 长短期记忆网络(LSTM)
解决隐变量模型长期信息保存和短期输入缺失问题的最早方法之一是长短期存储器(long short-term memory,LSTM)。它与门控循环单元有许多一样的属性。长短期记忆网络的设计比门控循环单元稍微复杂一些,却比门控循环单元早诞生了近 2…...
计算机操作系统-第十一天
目录 1、进程的状态 创建态与就绪态 运行态 终止态 新建态 结束态 进程状态的转换 进程的组织方式 链接方式(常见) 索引方式(少见) 本节思维导图 1、进程的状态 创建态与就绪态 1、进程正在被创建时,处于…...
Flutter视图原理之StatefulWidget,InheritedWidget
目录 StatefulElement1. 构造函数2. build3. _firstBuild3. didChangeDependencies4. setState InheritedElement1. Element类2. _updateInheritance3. InheritedWidget数据向下传递3.1 dependOnInheritedWidgetOfExactType 4. InheritedWidget的状态绑定4.1. ProxyElement 在f…...
观察者模式-对象间的联动
有个商城小程序,用户希望当有新品上市的时候能通知他们。这样用户就可以不要时刻盯着小程序了。在这个场景中,用户向小程序订阅了一个服务——发送新品短信。小程序在有新品上线时负责向订阅客户发出这个消息。 这就是发布-订阅模式,也称观察…...
Webpack十大缺点:当过度工程化遇上简单的静态页面
🤍 前端开发工程师(主业)、技术博主(副业)、已过CET6 🍨 阿珊和她的猫_CSDN个人主页 🕠 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 🍚 蓝桥云课签约作者、已在蓝桥云…...
新手指南|如何快速参与Moonbeam Ignite
Moonbeam Ignite是社区熟悉的有奖链上交互活动,将有300万枚GLMR作为生态激励注入Moonbeam生态系统,体验MoonbeamMoonbeam生态的应用即可获取相应Token奖励。Beamex/Beamswap、Moonwell和Gamma作为首批参与Moonbeam Ignite的三家项目方,将在活…...
VR航天科普主题公园模拟太空舱体验馆vr航天模拟体验设备
VR航天航空体验馆巡展是一项非常受欢迎的展览活动,可以让公众在现场体验到航天飞行的乐趣。 普乐蛙VR展览组织者会设计一个航天航空主题的VR体验馆,并在馆内设置各种航天航空相关的展示内容,如太空舱、火箭发射、星际航行等。 其次࿰…...
Spring Boot OAuth 2.0整合详解
目录 一、Spring Boot 2.x 示例 1、初始化设置 2、设置重定向URI 3、配置 application.yml 4、启动应用程序 二、Spring Boot 2.x 属性映射 二、CommonOAuth2Provider 三、配置自定义提供者(Provider)属性 四、覆盖 Spring Boot 2.x 的自动配置…...
安装visual studio报错“无法安装msodbcsql“
在安装visual studio2022时安装完成后提示无法安装msodbcsql, 查看日志文件详细信息提示:指定账户已存在。 未能安装包“msodbcsql,version17.2.30929.1,chipx64,languagezh-CN”。 搜索 URL https://aka.ms/VSSetupErrorReports?qPackageIdmsodbcsql;PackageActi…...
webGL编程指南 第三章 矩阵平移三角形.translatedTriangle_Matrix
我会持续更新关于wegl的编程指南中的代码。 当前的代码不会使用书中的缩写,每一步都是会展开写。希望能给后来学习的一些帮助 git代码地址 :git 接着 上一节 中 我们使用矩阵进行旋转,这次我们使用矩阵实现位移 <!DOCTYPE html> <…...
修改echarts的tooltip样式 折线图如何配置阴影并实现渐变色和自适应
图片展示 一、引入echarts 这里不用多解释 vue里使用 import echarts from “echarts”; html页面引用js文件或用script标签引用 二、定义一个具有宽高的dom div <div id"echart-broken" style"width:400px;height: 200px;"></div>三、定义…...
[论文笔记] SurroundOcc: Multi-Camera 3D Occupancy Prediction for Autonomous Driving
Wei, Yi, et al. “Surroundocc: Multi-camera 3d occupancy prediction for autonomous driving.” Proceedings of the IEEE/CVF International Conference on Computer Vision. 2023. 重点记录 将占用网格应用到多个相机构成的3D空间中; 使用BEVFormer中的方法获取3D特征, …...
辅助驾驶功能开发-功能对标篇(16)-NOA 城市辅助系统-毫末智行
1.横向对标参数 厂商毫末智行车型魏牌摩卡DHT-PHEV上市时间发布:2022年8月30日 上市:2022年底前方案12V5R2L+1DMS摄像头前视摄像头*3【800W】侧视摄像头*4后视摄像头*1【800W】环视摄像头*4DMS摄像头*1雷达毫米波雷达*54D毫米波雷达/超声波雷达*12激光雷达*2【速腾聚创 M1,1…...
H3C的IRF堆叠互联关系说明
H3C IRF堆叠互联说明48口交换机连接方式IRF Port 两台设备第一台的51口 第二台的51口irf-port 1/2 port group interface ten-gigabitethernet 1/0/51 port group interface ten-gigabitethernet 1/0/52第一台的52口第二台的52口irf-port 2/1 port group interface ten-gigabi…...
货物摆放(蓝桥杯)
货物摆放 题目描述 小蓝有一个超大的仓库,可以摆放很多货物。 现在,小蓝有 n 箱货物要摆放在仓库,每箱货物都是规则的正方体。小蓝规定了长、宽、高三个互相垂直的方向,每箱货物的边都必须严格平行于长、宽、高。 小蓝希望所有的…...
Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility
Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility 1. 实验室环境1.1 实验室环境1.2 小测试 2. The Endor System2.1 部署应用2.2 检查现有策略 3. Cilium 策略实体3.1 创建 allow-all 网络策略3.2 在 Hubble CLI 中验证网络策略源3.3 …...
对WWDC 2025 Keynote 内容的预测
借助我们以往对苹果公司发展路径的深入研究经验,以及大语言模型的分析能力,我们系统梳理了多年来苹果 WWDC 主题演讲的规律。在 WWDC 2025 即将揭幕之际,我们让 ChatGPT 对今年的 Keynote 内容进行了一个初步预测,聊作存档。等到明…...
DIY|Mac 搭建 ESP-IDF 开发环境及编译小智 AI
前一阵子在百度 AI 开发者大会上,看到基于小智 AI DIY 玩具的演示,感觉有点意思,想着自己也来试试。 如果只是想烧录现成的固件,乐鑫官方除了提供了 Windows 版本的 Flash 下载工具 之外,还提供了基于网页版的 ESP LA…...
python爬虫:Newspaper3k 的详细使用(好用的新闻网站文章抓取和解析的Python库)
更多内容请见: 爬虫和逆向教程-专栏介绍和目录 文章目录 一、Newspaper3k 概述1.1 Newspaper3k 介绍1.2 主要功能1.3 典型应用场景1.4 安装二、基本用法2.2 提取单篇文章的内容2.2 处理多篇文档三、高级选项3.1 自定义配置3.2 分析文章情感四、实战案例4.1 构建新闻摘要聚合器…...
Springcloud:Eureka 高可用集群搭建实战(服务注册与发现的底层原理与避坑指南)
引言:为什么 Eureka 依然是存量系统的核心? 尽管 Nacos 等新注册中心崛起,但金融、电力等保守行业仍有大量系统运行在 Eureka 上。理解其高可用设计与自我保护机制,是保障分布式系统稳定的必修课。本文将手把手带你搭建生产级 Eur…...
零基础在实践中学习网络安全-皮卡丘靶场(第九期-Unsafe Fileupload模块)(yakit方式)
本期内容并不是很难,相信大家会学的很愉快,当然对于有后端基础的朋友来说,本期内容更加容易了解,当然没有基础的也别担心,本期内容会详细解释有关内容 本期用到的软件:yakit(因为经过之前好多期…...
论文笔记——相干体技术在裂缝预测中的应用研究
目录 相关地震知识补充地震数据的认识地震几何属性 相干体算法定义基本原理第一代相干体技术:基于互相关的相干体技术(Correlation)第二代相干体技术:基于相似的相干体技术(Semblance)基于多道相似的相干体…...
20个超级好用的 CSS 动画库
分享 20 个最佳 CSS 动画库。 它们中的大多数将生成纯 CSS 代码,而不需要任何外部库。 1.Animate.css 一个开箱即用型的跨浏览器动画库,可供你在项目中使用。 2.Magic Animations CSS3 一组简单的动画,可以包含在你的网页或应用项目中。 3.An…...
C++.OpenGL (20/64)混合(Blending)
混合(Blending) 透明效果核心原理 #mermaid-svg-SWG0UzVfJms7Sm3e {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-icon{fill:#552222;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-text{fill…...
站群服务器的应用场景都有哪些?
站群服务器主要是为了多个网站的托管和管理所设计的,可以通过集中管理和高效资源的分配,来支持多个独立的网站同时运行,让每一个网站都可以分配到独立的IP地址,避免出现IP关联的风险,用户还可以通过控制面板进行管理功…...
