RecycleView加入底部loadmore(三)

之前的文章我们介绍了

onCreateViewHolder
onBindViewHolder
getItemCount
getItemViewType
onViewAttachedToWindow
onViewDetachedFromWindow

共六个方法,那么,你可能会想他们执行的顺序是什么样的?

为了方便观察,我们先看只有一个item的情况,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    String [] myDataset = {"11"};
    mAdapter = new MyAdapter(myDataset);
    mRecyclerView.setAdapter(mAdapter);
}
 
  
public class MyAdapter extends RecyclerView.Adapter {
    private String[] mDataset;
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }
    @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("MyAdapter","onCreateViewHolder");
        return vh;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.i("MyAdapter","onBindViewHolder");
    }
    @Override
    public int getItemCount() {
        Log.i("MyAdapter","getItemCount");
        return  mDataset.length;
    }

    @Override
    public int getItemViewType(int position) {
        Log.i("MyAdapter","getItemViewType");
        return super.getItemViewType(position);
    }
    @Override
    public void onViewAttachedToWindow(ViewHolder holder) {
        Log.i("MyAdapter","onViewAttachedToWindow");
        super.onViewAttachedToWindow(holder);
    }

    @Override
    public void onViewDetachedFromWindow(ViewHolder holder) {
        Log.i("MyAdapter","onViewDetachedFromWindow");
        super.onViewDetachedFromWindow(holder);
    }
}
打印结果如下

09-13 14:25:21.919 12484-12484/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:25:21.954 12484-12484/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:25:21.962 12484-12484/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:25:21.962 12484-12484/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:25:21.963 12484-12484/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:25:21.963 12484-12484/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:25:21.963 12484-12484/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:25:21.963 12484-12484/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:25:21.967 12484-12484/com.example.superdy.test I/MyAdapter: onCreateViewHolder
09-13 14:25:21.970 12484-12484/com.example.superdy.test I/MyAdapter: onBindViewHolder
09-13 14:25:21.971 12484-12484/com.example.superdy.test I/MyAdapter: onViewAttachedToWindow


可以看到,在只有一个item的情况下,getItemCount调用了5次,getItemType调用了三次

09-13 14:25:21.967 12484-12484/com.example.superdy.test I/MyAdapter: onCreateViewHolder
09-13 14:25:21.970 12484-12484/com.example.superdy.test I/MyAdapter: onBindViewHolder
09-13 14:25:21.971 12484-12484/com.example.superdy.test I/MyAdapter: onViewAttachedToWindow

分别调用了一次


我们看看两个item的时候,情况如何,

09-13 14:31:01.371 24304-24304/? I/MyAdapter: getItemCount
09-13 14:31:01.397 24304-24304/? I/MyAdapter: getItemCount
09-13 14:31:01.398 24304-24304/? I/MyAdapter: getItemCount
09-13 14:31:01.398 24304-24304/? I/MyAdapter: getItemCount
09-13 14:31:01.399 24304-24304/? I/MyAdapter: getItemCount
09-13 14:31:01.399 24304-24304/? I/MyAdapter: getItemViewType
09-13 14:31:01.400 24304-24304/? I/MyAdapter: getItemViewType
09-13 14:31:01.400 24304-24304/? I/MyAdapter: getItemViewType
09-13 14:31:01.404 24304-24304/? I/MyAdapter: onCreateViewHolder
09-13 14:31:01.404 24304-24304/? I/MyAdapter: onBindViewHolder
09-13 14:31:01.404 24304-24304/? I/MyAdapter: onViewAttachedToWindow

09-13 14:31:01.405 24304-24304/? I/MyAdapter: getItemCount
09-13 14:31:01.405 24304-24304/? I/MyAdapter: getItemViewType
09-13 14:31:01.405 24304-24304/? I/MyAdapter: getItemViewType
09-13 14:31:01.405 24304-24304/? I/MyAdapter: getItemViewType
09-13 14:31:01.406 24304-24304/? I/MyAdapter: onCreateViewHolder
09-13 14:31:01.406 24304-24304/? I/MyAdapter: onBindViewHolder
09-13 14:31:01.406 24304-24304/? I/MyAdapter: onViewAttachedToWindow


三个的时候

09-13 14:31:59.614 26325-26325/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:31:59.647 26325-26325/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:31:59.647 26325-26325/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:31:59.647 26325-26325/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:31:59.650 26325-26325/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:31:59.650 26325-26325/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:31:59.650 26325-26325/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:31:59.650 26325-26325/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:31:59.653 26325-26325/com.example.superdy.test I/MyAdapter: onCreateViewHolder
09-13 14:31:59.653 26325-26325/com.example.superdy.test I/MyAdapter: onBindViewHolder
09-13 14:31:59.654 26325-26325/com.example.superdy.test I/MyAdapter: onViewAttachedToWindow

09-13 14:31:59.654 26325-26325/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:31:59.654 26325-26325/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:31:59.654 26325-26325/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:31:59.654 26325-26325/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:31:59.657 26325-26325/com.example.superdy.test I/MyAdapter: onCreateViewHolder
09-13 14:31:59.657 26325-26325/com.example.superdy.test I/MyAdapter: onBindViewHolder
09-13 14:31:59.657 26325-26325/com.example.superdy.test I/MyAdapter: onViewAttachedToWindow

09-13 14:31:59.658 26325-26325/com.example.superdy.test I/MyAdapter: getItemCount
09-13 14:31:59.658 26325-26325/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:31:59.658 26325-26325/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:31:59.658 26325-26325/com.example.superdy.test I/MyAdapter: getItemViewType
09-13 14:31:59.659 26325-26325/com.example.superdy.test I/MyAdapter: onCreateViewHolder
09-13 14:31:59.659 26325-26325/com.example.superdy.test I/MyAdapter: onBindViewHolder
09-13 14:31:59.659 26325-26325/com.example.superdy.test I/MyAdapter: onViewAttachedToWindow


可以看到规律是,前面有四个getItemCount,这是固定的,

然后每一个循环中一次getItemCount,三次getItemViewType

然后onCreateViewHolder,onBindViewHolder,onViewAttachedToWindow

都是只有一次。










你可能感兴趣的:(安卓,recycleView,recycleView)