What's the enhancement of RecyclerView over ListView?

前端 未结 3 701
广开言路
广开言路 2021-02-13 18:07

RecyclerView is added into v7 support library since Android API 22 officially. And many people said that it is a enhancement over ListView

3条回答
  •  长发绾君心
    2021-02-13 18:54

    And what exactly is the enhancement of RecyclerView over ListView?

    RecyclerView is not an "enhancement" "over ListView", strictly speaking. ListView actually does something; RecyclerView, on its own, does not. A more accurate comparison would be that the RecyclerView framework is an improvement over AdapterView, and to some extent the AbsListView parent class of ListView and GridView.

    RecyclerView focuses on widget recycling and gross child View management. It delegates everything else to other classes. AdapterView does far less of this, making it more difficult to extend functionally.

    Of note:

    • Laying out the children, within the scrollable space of the RecyclerView, is delegated to managers. Hence, not only do three ship with recyclerview-v7 (list, grid, staggered grid), but others can be developed for alternative scenarios (e.g., overlapping children, for a StackView or Gallery sort of experience).

    • Updates from adapters can be much more fine-grained. With AdapterView, you pretty much have to redraw the entire view (e.g., a ListView and all its rows) on any change of significance, especially when adding and removing items. The update mechanism in the RecyclerView adapters indicate the specific positions that change. Not only does this require less processing time, but it helps enable the animated effects that RecyclerView offers (again, with pluggable replacements) for adding, moving, and removing items.

    • Other stuff that was "baked into" ListView, like drawing dividers, is now pulled out into extension points, such as an ItemDecorator. Now, you can choose how to "decorate" items, with line dividers or boxes or colored bar separators or whatever. Decoration is not limited to "dividers", but can affect anything in the views that, for one reason or another, you consider to be separate from the item views themselves.

    RecyclerView, though, is fairly complicated to get going. What you get from ListView "out of the box" takes a lot more code -- yours or a third-party library's -- to match. For experienced developers, this is a feature, in that the code is replaceable with other code. For newcomers, this is a bug, in that there is a steeper learning curve for RecyclerView, IMHO.

提交回复
热议问题