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

Android RecyclerView

RecyclerView核心实现官方文档

Once you determine your layout, you need to implement your Adapter and ViewHolder. These two classes work together to define how your data is displayed. The ViewHolder is a wrapper around a View that contains the layout for an individual item in the list. The Adapter creates ViewHolder objects as needed and also sets the data for those views. The process of associating views to their data is called binding.

  1. onCreateViewHolder(): RecyclerView calls this method whenever it needs to create a new ViewHolder. The method creates and initializes the ViewHolder and its associated View, but does not fill in the view’s contents—the ViewHolder has not yet been bound to specific data.
    /*** Provide a reference to the type of views that you are using* (custom ViewHolder)*/public static class ViewHolder extends RecyclerView.ViewHolder {private final TextView textView;public ViewHolder(View view) {super(view);// Define click listener for the ViewHolder's ViewtextView = (TextView) view.findViewById(R.id.textView);}public TextView getTextView() {return textView;}}/*** Initialize the dataset of the Adapter** @param dataSet String[] containing the data to populate views to be used* by RecyclerView*/public CustomAdapter(String[] dataSet) {localDataSet = dataSet;}// Create new views (invoked by the layout manager)@Overridepublic ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {// Create a new view, which defines the UI of the list itemView view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.text_row_item, viewGroup, false);return new ViewHolder(view);}
  1. onBindViewHolder(): RecyclerView calls this method to associate a ViewHolder with data. The method fetches the appropriate data and uses the data to fill in the view holder’s layout. For example, if the RecyclerView displays a list of names, the method might find the appropriate name in the list and fill in the view holder’s TextView widget.
    // Replace the contents of a view (invoked by the layout manager)@Overridepublic void onBindViewHolder(ViewHolder viewHolder, final int position) {// Get element from your dataset at this position and replace the// contents of the view with that elementviewHolder.getTextView().setText(localDataSet[position]);}
  1. getItemCount(): RecyclerView calls this method to get the size of the dataset. For example, in an address book app, this might be the total number of addresses. RecyclerView uses this to determine when there are no more items that can be displayed.
    // Return the size of your dataset (invoked by the layout manager)@Overridepublic int getItemCount() {return localDataSet.length;}
http://www.lryc.cn/news/345694.html

相关文章:

  • 【QT教程】QT6命令行工具开发 QT命令行
  • 基于Springboot的微乐校园管理系统(有报告)。Javaee项目,springboot项目。
  • 【第6节课笔记】LagentAgentLego
  • 【ARMv8/v9 系统寄存器 4 -- ARMv8 通用寄存器详细介绍】
  • 一个月速刷leetcodeHOT100 day 01
  • Cargo - 构建 rust项目、管理依赖包
  • 内网安全-代理Socks协议路由不出网后渗透通讯CS-MSF控制上线简单总结
  • NSSCTF | [SWPUCTF 2021 新生赛]jicao
  • Redis 支持的 Java 客户端都有哪些?
  • 【JavaEE 初阶(四)】多线程进阶
  • ZOC8 for Mac v8.08.1激活版:卓越性能的SSH客户端
  • 指针(4)有点难
  • 初步了解json文件
  • 赶紧收藏!2024 年最常见 100道 Java 基础面试题(四十)
  • 初步了解Kubernetes
  • 前端工程化的基本介绍
  • linux上Redis安装使用
  • prometheus+grafana的安装与部署及优点
  • JWK和JWT 学习
  • Go 使用mqtt
  • C++ primer plus习题及解析第十二章(类和动态内存分配)
  • gdb调试功能描述
  • 使用Simulink Test进行单元测试
  • 深度学习中超参数设置
  • Docker nsenter 命令使用
  • 十二种网络威胁防护方案
  • C++ sort()排序详解
  • 移动机器人系统与技术:自动驾驶、移动机器人、旋翼无人机
  • zTasker v1.88.1一键定时自动化任务
  • 时序医疗数据集---adfecgdb