返回

RecyclerView源码分析(三)测绘流程下篇

Android

大家好,欢迎来到RecyclerView源码分析的第三篇。在上一篇中,我们从RecyclerView的源码牵出了测量和布局的核心逻辑都在dispatchLayoutStep系列方法中。这篇我们就分析下dispatchLayoutStep系列方法。

dispatchLayoutStep系列方法

dispatchLayoutStep系列方法是RecyclerView的核心布局方法,负责执行布局过程中的各个步骤。这些方法包括:

  • dispatchLayoutStep1():执行布局过程的第一步,负责测量和布局子视图。
  • dispatchLayoutStep2():执行布局过程的第二步,负责回收和重用子视图。
  • dispatchLayoutStep3():执行布局过程的第三步,负责更新子视图的位置和大小。

dispatchLayoutStep1()

dispatchLayoutStep1()方法是dispatchLayoutStep系列方法中的第一步,负责测量和布局子视图。该方法首先调用LayoutManager的onLayoutChildren()方法来测量和布局子视图,然后调用LayoutManager的onLayoutCompleted()方法来完成布局过程。

dispatchLayoutStep2()

dispatchLayoutStep2()方法是dispatchLayoutStep系列方法中的第二步,负责回收和重用子视图。该方法首先调用LayoutManager的recycleChildren()方法来回收子视图,然后调用LayoutManager的detachAndScrapAttachedViews()方法来重用子视图。

dispatchLayoutStep3()

dispatchLayoutStep3()方法是dispatchLayoutStep系列方法中的第三步,负责更新子视图的位置和大小。该方法首先调用LayoutManager的updateChildViews()方法来更新子视图的位置和大小,然后调用LayoutManager的onLayoutCompleted()方法来完成布局过程。

总结

dispatchLayoutStep系列方法是RecyclerView的核心布局方法,负责执行布局过程中的各个步骤。这些方法包括dispatchLayoutStep1()、dispatchLayoutStep2()和dispatchLayoutStep3()。dispatchLayoutStep1()方法负责测量和布局子视图,dispatchLayoutStep2()方法负责回收和重用子视图,dispatchLayoutStep3()方法负责更新子视图的位置和大小。