Android RecyclerView

devtools/2024/9/23 17:26:56/

RecyclerView核心实现android.com/develop/ui/views/layout/recyclerview#java" rel="nofollow">官方文档

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.ppmy.cn/devtools/39511.html

相关文章

idea使用前的全局配置,一次配置,多次使用

前提:每次导入一个新的项目,就需要重新设置编码、maven、jdk、git版本等信息。实际每个项目所用到的配置信息是一致的,除非换一家公司,不然不会改动到这些内容。 idea版本:2024.1.1 1.1、全局Maven配置 IDEA启动页面…

使用nginx部署Vue项目

前提是后端已经跨域! 下载nginx,在路径下使用cmd打开nginx,关闭nginx使用任务管理器details end task 把dist中的文件都放到html文件夹中 打开conf,找到nginx.conf,编辑以下内容 location就是刚才放dist文件的那个文…

7-zip的介绍

7-Zip 是一款开源的文件压缩工具,它支持多种压缩格式,包括其自己的.7z格式,以及.zip、.tar、.gz、.bzip2等。7-Zip 以其高压缩比和强大的功能而闻名,尤其是在处理.7z格式时,它提供了比传统.zip格式更高的压缩率。 以下…

uniapp开发微信小程序,选择地理位置uni.chooseLocation

<view click"toCommunity">点击选择位置</view>toCommunity() {const that thisuni.getSetting({success: (res) > {const status res.authSetting// 如果当前设置是&#xff1a;不允许&#xff0c;则需要弹框提醒客户&#xff0c;需要前往设置页面…

ios实现拍摄视频与显示在界面上

1、添加录音和拍摄权限 NSMicrophoneUsageDescription Privacy - Camera Usage Description 2、代码 #import "ViewController.h" #import <AVFoundation/AVFoundation.h> #import <MobileCoreServices/MobileCoreServices.h>// 接下来是你的 ViewCont…

部署达梦数据库主从配置详细操作DM8

服务器配置 主库 192.168.81.128 实例名 dm-1 从库 192.168.81.129 实例名 dm-2 以下安装部署主从服务器都操作 关闭防火墙 systemctl stop firewalld && systemctl disable firewalld 注意安装前必须创建 dmdba 用户&#xff0c;禁止使用 root 用户安装数据库。…

【QA】Java集合常用的函数

文章目录 前言Collection接口通用函数 | Collections工具类通用函数 | List接口 Set接口List接口ArrayListLinkedList Set接口TreeSetHashSetLinkedHashSet Map接口通用函数TreeMapHashMapLinkedHashMap 前言 本文介绍Java集合中常用的函数。 Collection接口 通用函数 | Co…

普通组件的注册-局部注册和全局注册

目录 一、局部注册和全局注册-概述 二、局部注册的使用示例 三、全局注册的使用示例 一、局部注册和全局注册-概述 组件注册有两种方式&#xff1a; 局部注册&#xff1a;只能在注册的组件内使用。使用方法&#xff1a;创建.vue文件&#xff0c;在使用的组件内导入并注册。…