1.15 ListView & RecyclerView

less than 1 minute read

ListView

  • 流程
    • 核心概念, activeViews缓存当前激活(显示)的View, scrapViews缓存废弃(移除屏幕)的View
    • 两次onLayout()
      • 第一次: layoutChild() -> 由于getChildCount()等于0 fillDown() -> 循环makeAndAddView()
        • -> obtainView() -> getView()
        • -> setupView() -> addViewInLayout()把View add到ListView中
      • 第二次: layoutChild()
        • -> fillActiveViews()缓存到RecycleBin#mActiveView
        • -> fillSpecific() -> makeAndAddView()
          • -> mRecycler#getActiveView() 获取缓存的itemView
          • -> setupView() -> attachViewToParent()
  • 缓存机制, 两级缓存

RecyclerView

  • 流程
    • onMeasure() //往父容器添加子View并测量子View的大小
      • dispatchLayoutStep2()
        • LayoutManager#onLayoutChildren()
          • LayoutManager#fill()
            • LayoutManager#layoutChunk()
              • addView() -> ViewGroup#addView()
              • layoutDecorated()
    • onLayout()
      • dispatchLayoutStep1()
        • mViewStore#addToPreLayout() //存放ViewHolder的信息
      • dispatchLayoutStep2()
      • dispatchLauouyStep3()
        • mViewStore#addToPostLayout() //存放动画结束的状态
    • mViewFlinger#fling()
      • mScroller#fling()
      • LayoutManager#scrollVerticallyBy() *
  • 缓存机制, 四级缓存
    • mAttachedscrapped
    • mCachedViews
    • mViewCacheExtension
    • mRecyclerPool

ListView & RecyclerView区别

* `RecyclerView`, 灵活, 扩展性高, 可局部刷新, 四层缓存, 数据量大时使用
* `ListView`, 使用简单, 性能上和`RecyclerView`相差不大, 双缓存