RecycleView源码分析

一、RecycleView概述

1、简介

       A flexible view for providing a limited window into a large data set.

       与RecycleView比较相关的几个概念

  • Adapter: A subclass of RecyclerView.Adapter responsible for providing views that represent items in a data set.
  • Position: The position of a data item within an Adapter.
  • Index: The index of an attached child view as used in a call to getChildAt(int). Contrast with Position.
  • Binding: The process of preparing a child view to display data corresponding to a position within the adapter.
  • Recycle (view): A view previously used to display data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later. This can drastically improve performance by skipping initial layout inflation or construction.
  • Scrap (view): A child view that has entered into a temporarily detached state during layout. Scrap views may be reused without becoming fully detached from the parent RecyclerView, either unmodified if no rebinding is required or modified by the adapter if the view was considered dirty.
  • Dirty (view): A child view that must be rebound by the adapter before being displayed.

        关于position的一些注意点和说明:

there are two types of position related methods in RecyclerView:

  • layout position: Position of an item in the latest layout calculation. This is the position from the LayoutManager's perspective.
  • adapter position: Position of an item in the adapter. This is the position from the Adapter's perspective.

These two positions are the same except the time between dispatching adapter.notify* events and calculating the updated layout.

2、整体架构

  • RecycleView也是通过观察者模式,数据更新时,通过Adapter通知RecycleView更新。RecycleView为Observer,Adapter为Subject
  • RecycleView的item的布局主要由LayoutManager完成
  • RecycleView每个Item的动画主要由ItemAnimator完成
  • Recycler完成item的回收
  • State、AdapterHelper和ChildHelper是内部几个比较重要的类,分别管理部分数据和业务

二、源码分析

备注:以下所有代码均基于Android 23版本分析

1、measure、layout、draw

(1) measure 过程

  • 完成RecycleView这个ViewGroup的measure过程
  • 更新mState全局变量的状态,包括当前是否为preLayout,itemCount为多少等

(2) layout 过程

      RecycleView的layout过程,在真正完成layout的操作之外,还包含两个过程,分别为preLayout何和postLayout。

  • preLayout过程:记录数据改变之前,item view相关的信息
  • postLayout过程:记录数据改变之后,item view相关信息,以及对比preLayout中存储的item信息,完成相应动画
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    ......
    dispatchLayout();
    ......
}
void dispatchLayout() {
    ......
    if (mState.mRunSimpleAnimations) {
        // Step 0: Find out where all non-removed items are, pre-layout
        ......
    }
 
 
    if (mState.mRunPredictiveAnimations) {      //pre-layout
        // Step 1: run prelayout: This will use the old positions of items. The layout manager
        // is expected to layout everything, even removed items (though not to add removed
        // items back to the container). This gives the pre-layout position of APPEARING views
        // which come into existence as part of the real layout.
        ......
    } else {
        //remove the info
        ......
    }
 
 
    // Step 2: Run layout
    mState.mInPreLayout = false;
    mLayout.onLayoutChildren(mRecycler, mState);
 
 
    if (mState.mRunSimpleAnimations) {
        // Step 3: Find out where things are now, post-layout
        ......
 
        // Step 4: Animate DISAPPEARING and REMOVED items
        ......
 
        // Step 5: Animate APPEARING and ADDED items
        ......
 
        // Step 6: Animate PERSISTENT items
        ......
 
        // Step 7: Animate CHANGING items
        ......
    }
 
 
    ......
}
  • 在layout过程中,主要是调用mLayoutManager的onLayoutChildren完成布局工作
  • 如果RecycleView的item再在变化过程中需要展现动画,则在真正完成布局之前需要记录相关信息(step0和step1),完成布局之后需要完成动画(step3到step7)
  • 如果不需要动画,RecycleView可以省去不少工作,性能会有所提升

下面以LinearLayoutManager举例说明

// layout algorithm(布局算法说明):
// 1) by checking children and other variables, find an anchor coordinate and an anchor item position.
// 2) fill towards start, stacking from bottom
// 3) fill towards end, stacking from top
// 4) scroll to fulfill requirements like stack from bottom.

@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
    // layout algorithm:
    ...... 
 
    // calculate anchor position and co
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值