简体   繁体   English

RecyclerView适配器:OnBindViewHolder

[英]RecyclerView Adapter :OnBindViewHolder

Why do we use something like this to populate the recyclerview with data? 为什么我们要使用类似的数据来填充recyclerview? Can't we just create a simple array of string like String[] titles and display them, like in ListView? 我们是否只能像String []标题那样创建一个简单的字符串数组并像在ListView中那样显示它们?

List< Information> data

public void onBindViewHolder(MyViewHolder holder, int position) {
    Information current = data.get(position);
    holder.title.setText(current.title);
    holder.icon.setImageResource(current.iconId);
}

I recently watched a video on how to work with recyclerview and I can't understand why we have to create another class of information and then again create its array inside onBindViewHolder to show data. 我最近观看了有关如何使用recyclerview的视频,但我不明白为什么我们必须创建另一类信息,然后再次在onBindViewHolder中创建其数组以显示数据。

Can anybody help me understand this code? 有人可以帮助我理解此代码吗?

If you take a look at this example by google, they used an array dataset to populate data in RecyclerView . 如果您看一下Google的这个示例 ,他们使用了一个数组数据集来填充RecyclerView中的数据。 Take a look at Google's docs: 看一下Google的文档:

Called by RecyclerView to display the data at the specified position. 由RecyclerView调用以在指定位置显示数据。 This method should update the contents of the itemView to reflect the item at the given position. 此方法应更新itemView的内容以反映给定位置的项目。

Note that unlike ListView, RecyclerView will not call this method again if the position of the item changes in the data set unless the item itself is invalidated or the new position cannot be determined. 请注意,与ListView不同,除非项目的位置在数据集中发生更改,否则RecyclerView不会再次调用此方法,除非该项目本身无效或无法确定新位置。 For this reason, you should only use the position parameter while acquiring the related data item inside this method and should not keep a copy of it. 因此,您仅应在获取此方法内的相关数据项时使用position参数,而不应保留其副本。 If you need the position of an item later on (eg in a click listener), use getAdapterPosition() which will have the updated adapter position. 如果以后需要某个项目的位置(例如,在单击侦听器中),请使用getAdapterPosition(),它将具有更新的适配器位置。 Override onBindViewHolder(ViewHolder, int, List) instead if Adapter can handle effcient partial bind. 如果Adapter可以处理有效的部分绑定,则重写onBindViewHolder(ViewHolder,int,List)。

String [] title ; 
int [] imageID;
onBindViewHolder(MyViewHolder holder, int position) { 
 Information current = data.get(position); 
 holder.title.setText(title [position]); holder.icon.setImageResource(imageID     [position]); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase RecyclerView Adapter onBindViewHolder - Firebase RecyclerView Adapter onBindViewHolder Recyclerview 适配器 onBindViewHolder 有效负载不起作用 - Recyclerview adapter onBindViewHolder payload is not working 在 RecyclerView 适配器的 onBindViewHolder 中获取上下文 - Get context in onBindViewHolder in RecyclerView Adapter RecyclerVIew适配器onBIndVIewHolder中的上下文错误 - context error in RecyclerVIew Adapter onBIndVIewHolder 使用RecyclerView.Adapter在onBindViewHolder()内设置onItemClickListener - Set onItemClickListener inside onBindViewHolder() with RecyclerView.Adapter RecyclerView.Adapter onBindViewHolder()获取错误的位置 - RecyclerView.Adapter onBindViewHolder() gets wrong position 如何在RecyclerView适配器的onBindViewHolder中声明overridePendingTransition? - How to declare overridePendingTransition inside onBindViewHolder of a RecyclerView adapter? RecyclerView Adapter notifyDataSetChanged()之后未调用onBindViewHolder - onBindViewHolder not called after RecyclerView Adapter notifyDataSetChanged() 在RecyclerView适配器的onBindViewHolder上的方法setImageResource工作 - Method setImageResource in onBindViewHolder of RecyclerView Adapter dosent work 视图在RecyclerView Adapter中的onBindViewHolder中返回NullPointerException - Views return NullPointerException in onBindViewHolder in RecyclerView Adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM