当前位置: 首页 > news >正文

Android RecyclerView展示List<View> Adapter的数据源使用View

Android不推荐使用View作为 Recyclerview.Adapter的数据源;弊端像回收或复用就不多说了,要展示List<View>肯定是有原由的!

方式一、FrameLayout.addView:
public class ViewListAdapter extends RecyclerView.Adapter<ViewListAdapter.ViewHolder> {private final List<View> viewList;public ViewListAdapter(List<View> views) {this.viewList = views;}@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {// 使用 FrameLayout 作为容器,避免布局嵌套问题FrameLayout container = new FrameLayout(parent.getContext());container.setLayoutParams(new RecyclerView.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT));return new ViewHolder(container);}@Overridepublic void onBindViewHolder(@NonNull ViewHolder holder, int position) {View currentView = viewList.get(position);// 移除容器中已有的 View(避免复用冲突)holder.container.removeAllViews();if(currentView.getParent() != null){// 移除需要添加View的父布局(避免复用冲突)((ViewGroup)currentView.getParent()).removeView(currentView);}// 确保 View 有布局参数if (currentView.getLayoutParams() == null) {FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);currentView.setLayoutParams(params);}holder.container.addView(currentView);}@Overridepublic int getItemCount() {return viewList.size();}public static class ViewHolder extends RecyclerView.ViewHolder {FrameLayout container;public ViewHolder(View itemView) {super(itemView);container = (FrameLayout) itemView;}}
}
方式二、XML文件addView:
public class ViewAdapter extends RecyclerView.Adapter<ViewAdapter.ViewHolder> {private final List<View> viewList;public ViewAdapter(List<View> views) {this.viewList = views;}@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_rv, parent, false);return new ViewHolder(view);}@Overridepublic void onBindViewHolder(@NonNull ViewHolder holder, int position) {View currentView = viewList.get(position);// 移除容器中已有的 View(避免复用冲突)holder.container.removeAllViews();if(currentView.getParent() != null){// 移除需要添加View的父布局(避免复用冲突)((ViewGroup)currentView.getParent()).removeView(currentView);}holder.container.addView(currentView);}@Overridepublic int getItemCount() {return viewList.size();}public static class ViewHolder extends RecyclerView.ViewHolder {FrameLayout container;public ViewHolder(View itemView) {super(itemView);container = (FrameLayout) itemView;}}
}

R.layout.layout_rv

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="300dp"android:padding="10dp"android:layout_height="300dp">
</FrameLayout>
方式三、viewType代替position:
public class ViewTypeListAdapter extends RecyclerView.Adapter<ViewTypeListAdapter.ViewHolder> {private final List<View> viewList;public ViewTypeListAdapter(List<View> views) {this.viewList = views;}@Overridepublic int getItemViewType(int position) {return position;}@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {return new ViewHolder(viewList.get(viewType));}@Overridepublic void onBindViewHolder(@NonNull ViewHolder holder, int position) {}@Overridepublic int getItemCount() {return viewList.size();}public static class ViewHolder extends RecyclerView.ViewHolder {public ViewHolder(View itemView) {super(itemView);}}
}

方式三不推荐,notifyDataSetChanged等刷新会有问题;

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling androidx.recyclerview.widget.RecyclerView

IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

没必要使用RecyclerView时,可以使用 ScrollView + LinearLayout + addView 


文章转载自:

http://C6u2WQYu.snnfn.cn
http://jA6K8XTr.snnfn.cn
http://Qd5czrVz.snnfn.cn
http://15dreRMp.snnfn.cn
http://FUmPJyS1.snnfn.cn
http://SOJJHuw8.snnfn.cn
http://TgYdaBlD.snnfn.cn
http://JAdCOfdU.snnfn.cn
http://hAqsaZeQ.snnfn.cn
http://zBnwFhJy.snnfn.cn
http://g612gqXt.snnfn.cn
http://h5D72SAA.snnfn.cn
http://BY8wgMgI.snnfn.cn
http://duQHnHPO.snnfn.cn
http://8dIj9ynU.snnfn.cn
http://Bl2uaGR3.snnfn.cn
http://ob4o2QEo.snnfn.cn
http://8LoYIeY1.snnfn.cn
http://wpUlLN8v.snnfn.cn
http://iBr9MteJ.snnfn.cn
http://vJ5J4knu.snnfn.cn
http://Jgp5PKVD.snnfn.cn
http://getPn488.snnfn.cn
http://kzdSKv3l.snnfn.cn
http://8KYWIM9P.snnfn.cn
http://3Zh61mgv.snnfn.cn
http://85VrK5AY.snnfn.cn
http://qX0m7CAV.snnfn.cn
http://unTbrdb7.snnfn.cn
http://4qEiYU66.snnfn.cn
http://www.dtcms.com/a/384239.html

相关文章:

  • 深圳比斯特|电池组PACK自动化生产线厂家概述
  • 查看iOS App 性能监控全流程 如何监控CPU内存GPU帧率、电池能耗与网络延迟(uni-app iOS开发与调试优化指南)
  • AI渗透测试工具“Villager“整合Kali Linux工具与DeepSeek AI实现自动化攻击
  • uniAPP安装 uni-popup,弹窗提示
  • 无人机图传系统的功能解析和技术实现原理
  • Linux笔记---HTTPS的原理
  • 如何抓包?iOS 抓包方法、HTTPS 抓包工具选择与手机网络调试全攻略
  • 第22课:DevOps与CI、CD
  • JDK 8调用HTTPS POST接口的SSL配置
  • HTTPS 的加密
  • 基于 EPGF 架构理念的 FaceFusion 3.4.1 本地 .venv 部署教程(非 Conda 环境部署优化版)
  • RabbitMQ 高级功能与优化篇
  • Node.js 高级应用:负载均衡与流量限制
  • Capistrano 让Ruby应用部署变得优雅又简单!
  • [计算机毕业设计]基于深度学习的噪声过滤音频优化系统研究
  • 02-Media-8-uvc_with_csc.py 使用硬件解码的USB摄像头(UVC)捕获视频并显示的程序
  • 【Java】P2 Java 学习路线与JVM、注释方法
  • 【论文阅读—智能驾驶】Diving Deeper Into Pedestrian Behavior Understanding
  • 【论文阅读】LG-VQ: Language-Guided Codebook Learning
  • AI摘桃记:精准率(P-Precision)、召回率(R-Recall)、F1-Score之争
  • 分布式专题——12 RabbitMQ之应用开发
  • 软件可靠性设计:高可用性架构实战——双机热备与集群技术
  • Mac 真正多显示器支持:TESmart USB-C KVM(搭载 DisplayLink 技术)如何实现
  • 鼠标光标消失、触摸板失灵?仅用键盘 3 步救回
  • 漏洞无效化学习
  • 蓝牙鼠标频繁卡顿?一招解决 Win10/11 的 USB 省电机制干扰问题
  • 吱吱企业即时通讯保障企业通讯安全,提升企业部门协作效率
  • 中宇联云计算SD-WAN的售后服务怎么样
  • 【矩阵局部最大值】2022-11-16
  • 矩阵的特征值与特征向量:定义、几何意义与在信号处理中的应用