关于RecyclerView.ViewHolder和RecyclerView.Adapter [英] About RecyclerView.ViewHolder and RecyclerView.Adapter
问题描述
-
RecyclerView.ViewHolder
类的字段为public final View itemView
.它说onBindViewHolder
方法应该更新itemView
的内容以反映给定位置的项目.final
修饰符是否表示此字段的值不能更改?
The
RecyclerView.ViewHolder
class has a field that ispublic final View itemView
. It says that theonBindViewHolder
method should update the contents of theitemView
to reflect the item at the given position . Doesn’t thefinal
modifier indicate that the value of this field cannot change ?
下面的代码来自教科书:
The code below is from the textbook :
public class ViewHolder extends RecyclerView.ViewHolder {
...
@Override
public int getItemCount() {
...
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
...
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
...
}
}
为什么这里的这些方法可以覆盖从RecyclerView.ViewHolder
类派生的RecyclerView.Adapter
类中的方法?
Why do these methods here can override the methods in the RecyclerView.Adapter
class which is derived from the RecyclerView.ViewHolder
class ?
https://developer.android.com/参考/android/support/v7/widget/RecyclerView.Adapter.html
https://developer.android.com/参考/android/support/v7/widget/RecyclerView.ViewHolder.html
有人可以解释吗?
谢谢.
推荐答案
最终修饰符是否表示该字段的值 不能改变?
Doesn’t the final modifier indicate that the value of this field cannot change ?
视图上的最终修饰符表示您只能仅启动一次视图(通过创建新的视图(上下文)或从xml文件中填充视图). 但是您仍然可以修改view属性. (即您的视图包含TextView,您可以设置文本)
The final modifier on a View indicate that you can only initiate the view once (by creating a new View(context) or inflate a view from an xml file). But you can still modify the view property. (i.e. your view contains a TextView, you can set the text)
对于第二个问题,教科书中关于如何使用视图保持器实现适配器的定义不是很精确.这是带有自定义视图持有人的适配器的简单实现.
For your second question, the text book is not very precise about how to implement the adapter with a view holder. Here is a simple implementation of an adapter with a custom view holder.
public class Adapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
private List<String> titles;
public Adapter(List<String> titles) {
this.titles = titles;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
return new MyViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_view, viewGroup, false));
}
@Override
public void onBindViewHolder(MyViewHolder myViewHolder, int i) {
String title = titles.get(i);
myViewHolder.title.setText(title);
}
@Override
public int getItemCount() {
return titles.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView title;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title_TV);
}
}
}
以及它的xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="@+id/title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
您可以看到,如果扩展RecyclerView.Adapter,则必须重写这3种方法.
You can see that if you extend RecyclerView.Adapter, you will have to override these 3 methods.
希望这将帮助您更多地了解RecyclerView.
Hope this will help you to understand more the RecyclerView.
这篇关于关于RecyclerView.ViewHolder和RecyclerView.Adapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!