上文讲了如何简单的在RecycleView中加入底部View,
本文将在之前的基础了继续讲解如何改造RecycleView
Section1
我们知道,RecycleView.Adapter有三个抽象方法必须要实现
public class TestViewHolder extends RecyclerView.Adapter { @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return null; } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { } @Override public int getItemCount() { return 0; } }
其中,
getItemCount()类似传统ListView的adapter里面的getCount()
而onBindViewHolder方法传入了两个参数,hold以及postion,其实就是把数据绑定到到holder中,也比较好理解,
稍微要看下的onCreateViewHolder,返回一个ViewHolder,
你可能会疑问,View在哪?怎么没有返回View的地方,其实秘密就藏在这个ViewHolder,当你继承
RecyclerView.ViewHolder的时候你会发现,你必须要有一个传入参数为view的构造方法,并且这个
构造方法还要必须调用父类的super(itemView)方法,我们的itemView就通过这种方式传给系统了
class MyHolder extends RecyclerView.ViewHolder{ public MyHolder(View itemView) { super(itemView); } }所以,onCreateViewHolder这个方法,一石二鸟,既创建了View,又创建了ViewHolder,并且把两者绑定,然后传给了系统使用。@Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { return null; }
Section2
那么,onCreateViewHolder方法还有一个参数,int viewType,这个参数是做什么的,我们知道,通常情况下,RecycleView,listView的item都是一种类型,但是有些时候,我们的item可能会是完全不同的item,这里的viewType就是在这种情况下使用的当然,哪个view是哪种viewType是由我们所指定的,所以这个时候我们需要OverridegetItemViewType(int position)方法
如下@Override public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { TextView v = (TextView) LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false); ViewHolder vh = new ViewHolder(v); Log.i("onCreateViewHolder",viewType+""); return vh; }@Override public int getItemViewType(int position) { return position+100; }打印结果09-13 13:21:37.704 23985-23985/com.example.superdy.test I/onCreateViewHolder: 100 09-13 13:21:37.706 23985-23985/com.example.superdy.test I/onCreateViewHolder: 101 09-13 13:21:37.707 23985-23985/com.example.superdy.test I/onCreateViewHolder: 102 09-13 13:21:37.709 23985-23985/com.example.superdy.test I/onCreateViewHolder: 103 09-13 13:21:37.710 23985-23985/com.example.superdy.test I/onCreateViewHolder: 104 09-13 13:21:37.711 23985-23985/com.example.superdy.test I/onCreateViewHolder: 105可以看到,onCreateViewHolder(ViewGroup parent, int viewType) 传入的viewType,正是我们再getItemViewType中返回的int值如果不Override呢我们看下父类的实现/** * Return the view type of the item at <code>position</code> for the purposes * of view recycling. * * <p>The default implementation of this method returns 0, making the assumption of * a single view type for the adapter. Unlike ListView adapters, types need not * be contiguous. Consider using id resources to uniquely identify item view types. * * @param position position to query * @return integer value identifying the type of the view needed to represent the item at * <code>position</code>. Type codes need not be contiguous. */ public int getItemViewType(int position) { return 0; }把getItemViewType的重写去掉,再run一次。打印如下09-13 13:26:10.396 1482-1482/com.example.superdy.test I/onCreateViewHolder: 0 09-13 13:26:10.398 1482-1482/com.example.superdy.test I/onCreateViewHolder: 0 09-13 13:26:10.399 1482-1482/com.example.superdy.test I/onCreateViewHolder: 0 09-13 13:26:10.400 1482-1482/com.example.superdy.test I/onCreateViewHolder: 0 09-13 13:26:10.401 1482-1482/com.example.superdy.test I/onCreateViewHolder: 0 09-13 13:26:10.402 1482-1482/com.example.superdy.test I/onCreateViewHolder: 0Section3
到现在,我们已经介绍了RecyclerView.Adapter
中可以重写的四种方法onCreateViewHolderonBindViewHoldergetItemCountgetItemViewType其实RecyclerView.Adapter
中还有两个方法也是经常会重写的分别是/** * Called when a view created by this adapter has been attached to a window. * * <p>This can be used as a reasonable signal that the view is about to be seen * by the user. If the adapter previously freed any resources in * {@link #onViewDetachedFromWindow(RecyclerView.ViewHolder) onViewDetachedFromWindow} * those resources should be restored here.</p> * * @param holder Holder of the view being attached */ public void onViewAttachedToWindow(VH holder) { }和/** * Called when a view created by this adapter has been detached from its window. * * <p>Becoming detached from the window is not necessarily a permanent condition; * the consumer of an Adapter's views may choose to cache views offscreen while they * are not visible, attaching an detaching them as appropriate.</p> * * @param holder Holder of the view being detached */ public void onViewDetachedFromWindow(VH holder) { }onViewAttachedToWindow源码里的注释里有这么一句This can be used as a reasonable signal that the view is about to be seen by the user也就是说,我们可以通过这个方法来判断itemView是否对于用户可见,这样我们可以根据这个来做各种判断。