Android RecyclerView中的ViewHolder

在使用android提供的组件以列表的格式显示数据时,使用过ListView组件和RecyclerView组件。目前一般推荐使用RecyclerView,因为RecyclerView本身的缓存和效率比ListView高,且支持灵活的布局方式,所以会被大家采用。相信大家在使用ListView时,如果要显示的数据多,肯定多会想到优化Adaper的getView()方法,下面给出一个例子:

public class UsersAdapter extends ArrayAdapter<User> {

    private static class ViewHolder {
        TextView name;
        TextView home;
    }

    public UsersAdapter(Context context, ArrayList<User> users) {
       super(context, R.layout.item_user, users);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       User user = getItem(position);    
       ViewHolder viewHolder;
       if (convertView == null) {
          viewHolder = new ViewHolder();
          LayoutInflater inflater = LayoutInflater.from(getContext());
          convertView = inflater.inflate(R.layout.item_user, parent, false);
          viewHolder.name = (TextView) convertView.findViewById(R.id.tvName);
          viewHolder.home = (TextView)convertView.findViewById(R.id.tvHome);
          convertView.setTag(viewHolder);
       } else {
           viewHolder = (ViewHolder) convertView.getTag();
       }
       viewHolder.name.setText(user.name);
       viewHolder.home.setText(user.hometown);
       return convertView;
   }
}

在这边,我们在ListView需要用到的Adapter中定义了一个内部类ViewHolder,它存储了我们要加载的view的所有子view结构,如果这个view已经被加载过只是暂时被回收, 当需要再次展示的话我们就不需要重新加载整个view,也不需要通过findViewById()来寻找要加载的view的子view,可以直接找到这个view,将要展示的数据设置即可返回显示。

但是在RecyclerView中,我们并不需要做这么多,我们先看一个RecyclerView的简单使用步骤:

  • 定义一个RecyclerView的布局文件以及要展示的item的布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
   
    <android.support.v7.widget.RecyclerView
        android:id="@+id/t_classList"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp">

<TextView
    android:id="@+id/t_class_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:textSize="19sp" />

<View
    android:layout_width="match_parent"
    android:layout_height="0.5px"
    android:background="@color/whiteGray"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="6dp"
    android:typeface="serif"/>
</LinearLayout>
  • 定义一个adapter

public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.ClassViewHolder> {

private List<ClassVO> classVOs;

static class ClassViewHolder extends RecyclerView.ViewHolder {

    View classView;
    TextView className;

    public ClassViewHolder(View itemView) {
        super(itemView);
        classView = itemView;
        className = (TextView) itemView.findViewById(R.id.t_class_name);
    }
}

public ClassAdapter(List<ClassVO> classVOs){
    this.classVOs = classVOs;
}

@Override
public ClassViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragement_class_item, null, false);
    final ClassViewHolder holder = new ClassViewHolder(view);
    holder.classView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int position = holder.getAdapterPosition();
            ClassVO classVO = classVOs.get(position);
            Toast.makeText(view.getContext(), "you click class "+classVO.getId(), Toast.LENGTH_SHORT).show();
        }
    });
    return holder;
}

@Override
public void onBindViewHolder(ClassViewHolder holder, int position) {
    ClassVO classVO = classVOs.get(position);
    holder.className.setText(classVO.getName());
}

@Override
public int getItemCount() {
    return classVOs.size();
}

}

  • 设置RecyclerView的布局和adapter

recyclerView = (RecyclerView) classListView.findViewById(R.id.t_classList);
layoutManager = new LinearLayoutManager(this.getContext());
recyclerView.setLayoutManager(layoutManager);
if (classVOs!=null){
    adapter = new ClassAdapter(classVOs);
}
recyclerView.setAdapter(adapter);

既然我们要说的是RecyclerView中的ViewHolder,但是我们的使用步骤中并没有单独提出ViewHolder,因为在我们上面说的使用步骤的第二步———“定义一个adapter”,就涉及到ViewHolder。
当我们自定义一个adapter时,继承了RecyclerView的一个内部类:

 /**
     * Base class for an Adapter
     *
     * <p>Adapters provide a binding from an app-specific data set to views that are displayed
     * within a {@link RecyclerView}.</p>
     */
    public static abstract class Adapter<VH extends ViewHolder> {
        private final AdapterDataObservable mObservable = new AdapterDataObservable();
        private boolean mHasStableIds = false;

        /**
         * Called when RecyclerView needs a new {@link ViewHolder} of the given type to represent
         * an item.
         * <p>
         * This new ViewHolder should be constructed with a new View that can represent the items
         * of the given type. You can either create a new View manually or inflate it from an XML
         * layout file.
         * <p>
         * The new ViewHolder will be used to display items of the adapter using
         * {@link #onBindViewHolder(ViewHolder, int, List)}. Since it will be re-used to display
         * different items in the data set, it is a good idea to cache references to sub views of
         * the View to avoid unnecessary {@link View#findViewById(int)} calls.
         *
         * @param parent The ViewGroup into which the new View will be added after it is bound to
         *               an adapter position.
         * @param viewType The view type of the new View.
         *
         * @return A new ViewHolder that holds a View of the given view type.
         * @see #getItemViewType(int)
         * @see #onBindViewHolder(ViewHolder, int)
         */
        public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);

        /**
         * Called by RecyclerView to display the data at the specified position. This method should
         * update the contents of the {@link ViewHolder#itemView} to reflect the item at the given
         * position.
         * <p>
         * Note that unlike {@link android.widget.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 <code>position</code> 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 {@link ViewHolder#getAdapterPosition()} which will
         * have the updated adapter position.
         *
         * Override {@link #onBindViewHolder(ViewHolder, int, List)} instead if Adapter can
         * handle efficient partial bind.
         *
         * @param holder The ViewHolder which should be updated to represent the contents of the
         *        item at the given position in the data set.
         * @param position The position of the item within the adapter's data set.
         */
        public abstract void onBindViewHolder(VH holder, int position);

        /**
         * Called by RecyclerView to display the data at the specified position. This method
         * should update the contents of the {@link ViewHolder#itemView} to reflect the item at
         * the given position.
         * <p>
         * Note that unlike {@link android.widget.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 <code>position</code> 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 {@link ViewHolder#getAdapterPosition()} which will
         * have the updated adapter position.
         * <p>
         * Partial bind vs full bind:
         * <p>
         * The payloads parameter is a merge list from {@link #notifyItemChanged(int, Object)} or
         * {@link #notifyItemRangeChanged(int, int, Object)}.  If the payloads list is not empty,
         * the ViewHolder is currently bound to old data and Adapter may run an efficient partial
         * update using the payload info.  If the payload is empty,  Adapter must run a full bind.
         * Adapter should not assume that the payload passed in notify methods will be received by
         * onBindViewHolder().  For example when the view is not attached to the screen, the
         * payload in notifyItemChange() will be simply dropped.
         *
         * @param holder The ViewHolder which should be updated to represent the contents of the
         *               item at the given position in the data set.
         * @param position The position of the item within the adapter's data set.
         * @param payloads A non-null list of merged payloads. Can be empty list if requires full
         *                 update.
         */

可以看到,RecyclerView内部已经帮我们定义好了ViewHolder抽象类,当我们自定义Adapter时,需要定义好要继承ViewHolder的类(Adapter<VH extends ViewHolder>)。

ViewHolder的作用我们在上文介绍使使用ListView时已经说到,简而言之就是提高消息,下面我们看一看源码中对ViewHolder的介绍:

/**
     * A ViewHolder describes an item view and metadata about its place within the RecyclerView.
     *
     * <p>{@link Adapter} implementations should subclass ViewHolder and add fields for caching
     * potentially expensive {@link View#findViewById(int)} results.</p>
     *
     * <p>While {@link LayoutParams} belong to the {@link LayoutManager},
     * {@link ViewHolder ViewHolders} belong to the adapter. Adapters should feel free to use
     * their own custom ViewHolder implementations to store data that makes binding view contents
     * easier. Implementations should assume that individual item views will hold strong references
     * to <code>ViewHolder</code> objects and that <code>RecyclerView</code> instances may hold
     * strong references to extra off-screen item views for caching purposes</p>
     */
    public static abstract class ViewHolder {
        ......
    }

可以看出两点重要的地方:

  • Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive {@link View#findViewById(int)} results(adapter应当拥有ViewHolder的子类,并且ViewHolder内部应当存储一些子view,避免时间代价很大的findViewById操作)

  • Adapters should feel free to use their own custom ViewHolder implementations to store data that makes binding view content easier

其RecyclerView内部定义的ViewHolder类包含很多复杂的属性,内部使用场景也有很多,而我们经常使用的也就是onCreateViewHolder()方法和onBindViewHolder()方法,onCreateViewHolder()方法在RecyclerView需要一个新类型。item的ViewHolder时调用来创建一个ViewHolder,而onBindViewHolder()方法则当RecyclerView需要在特定位置的item展示数据时调用。

参考文章:

RecyclerViewAndroid提供的一个非常灵活的控件,用于显示大量数据的列表,它的核心是ViewHolder和Adapter。 ViewHolder是RecyclerView的一个子类,它主要用于缓存item的视图,以便能够快速地滚动和重新使用这些视图。ViewHolder的实现非常简单,只需要继承RecyclerView.ViewHolder,然后在构造函数初始化视图,最后将视图绑定到ViewHolder。 Adapter是RecyclerView的另一个子类,它主要用于管理数据和视图之间的关系。Adapter必须实现三个方法:onCreateViewHolder、onBindViewHolder和getItemCount。其onCreateViewHolder方法用于创建ViewHolder,onBindViewHolder方法用于将数据绑定到ViewHolder,getItemCount方法用于获取数据的数量。 以下是一个简单的RecyclerViewViewHolder和Adapter的实现示例: ViewHolder的实现: ``` public class MyViewHolder extends RecyclerView.ViewHolder { private TextView mTextView; public MyViewHolder(View itemView) { super(itemView); mTextView = itemView.findViewById(R.id.text_view); } public void bind(String text) { mTextView.setText(text); } } ``` Adapter的实现: ``` public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> { private List<String> mData; public MyAdapter(List<String> data) { mData = data; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item_view, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { holder.bind(mData.get(position)); } @Override public int getItemCount() { return mData.size(); } } ``` 在这个示例,我们创建了一个ViewHolder,它包含一个TextView,用于显示列表项的文本。我们还创建了一个Adapter,它管理一个字符串列表,并将每个字符串绑定到ViewHolder的TextView。 最后,我们需要将Adapter设置到RecyclerView: ``` RecyclerView recyclerView = findViewById(R.id.recycler_view); MyAdapter adapter = new MyAdapter(data); recyclerView.setAdapter(adapter); ``` 这样就可以显示一个简单的RecyclerView了。当我们滚动列表时,RecyclerView会自动回收不再需要的视图,并将它们重新用于新的列表项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值