Android RecyclerView

news/2024/9/23 4:32:31/

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/news/1458808.html

相关文章

LeetCode763:划分字母区间

题目描述 给你一个字符串 s 。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。 注意,划分结果需要满足:将所有划分结果按顺序连接,得到的字符串仍然是 s 。 返回一个表示每个字符串片段的长度的列表。 …

Paddle 基于ANN(全连接神经网络)的GAN(生成对抗网络)实现

什么是GAN GAN是生成对抗网络,将会根据一个随机向量,实现数据的生成(如生成手写数字、生成文本等)。 GAN的训练过程中,需要有一个生成器G和一个鉴别器D. 生成器用于生成数据,鉴定器用于鉴定数据的准确性&…

Dijkstra算法(C/C++简明注释详解版: 代码实现 测试数据)

算法思想简述(From C知道): Dijkstra算法是一种求解最短路径的贪心算法,它能够找到两点之间的最短路径。C语言实现Dijkstra算法需要以下步骤: 1. 创建一个数组用于记录起始点到其他节点的距离,初始化为无…

2024面试自动化测试面试题【含答案】

🔥 交流讨论:欢迎加入我们一起学习! 🔥 资源分享:耗时200小时精选的「软件测试」资料包 🔥 教程推荐:火遍全网的《软件测试》教程 📢欢迎点赞 👍 收藏 ⭐留言 &#x1…

GORM数据库连接池对接Prometheus

一、背景与介绍 Golang的database/sql包定了关于操作数据库的相关接口,但是没有去做对应数据库的实现。这些实现是预留给开发者或者对应厂商进行实现的。 其中让我比较关注的是Golang的sql包有没有实现连接池pool的机制呢? 毕竟Golang是静态语言,类似J…

多线程学习Day07

共享模型之不可变 从一个日期转换的问题开始 Slf4j(topic "c.Test1") public class Test1 {public static void main(String[] args) {SimpleDateFormat sdf new SimpleDateFormat("yyyy-MM-dd");for (int i 0; i < 10; i) {new Thread(() -> {…

Leetcode—1396. 设计地铁系统【中等】

2024每日刷题&#xff08;127&#xff09; Leetcode—1396. 设计地铁系统 实现代码 class UndergroundSystem { public:typedef struct Checkin {string startStation;int time;} Checkin;typedef struct Checkout{int tripNum;int totalTime;} Checkout;UndergroundSystem()…

新火种AI|AI让大家都变“土”了!

作者&#xff1a;一号 编辑&#xff1a;美美 AI不仅要把人变“土”&#xff0c;还要把人变多样。 这个世界&#xff0c;终究是变“土”了。 今年五一假期&#xff0c;一个名为“Remini”的AI修图APP火遍了全网。注意&#xff0c;是Remini&#xff0c;而不是Redmi&#xff0…