RecyclerView 适配器:OnBindViewHolder

RecyclerView Adapter :OnBindViewHolder

为什么我们要使用这样的东西来用数据填充 recyclerview?我们不能只创建一个简单的字符串数组,如 String[] titles 并像在 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);
}

我最近看了一个关于如何使用 recyclerview 的视频,我不明白为什么我们必须创建另一个 class 信息,然后再次在 onBindViewHolder[= 中创建它的数组19=] 显示数据。

谁能帮我理解这段代码?

如果你看看this example by google, they used an array dataset to populate data in RecyclerView。 查看 Google 的文档:

Called by RecyclerView to display the data at the specified position. This method should update the contents of the itemView to reflect the item at the given position.

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. 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. If you need the position of an item later on (e.g. in a click listener), use getAdapterPosition() which will have the updated adapter position. Override onBindViewHolder(ViewHolder, int, List) instead if Adapter can handle effcient partial bind.

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