This is the 21st day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

preface

RecycleView is the most common list control we use after Listview. What’s the difference between them

RecycleView and ListView

1. Caching mechanism

RecyclerView is more than two levels of ListView cache, support multiple separate ItemView cache, support developers to customize the cache processing logic, support all RecyclerView share the same RecyclerViewPool(cache pool).

ListView and RecyclerView cache mechanisms are basically the same:

MActiveViews and mAttachedScrap are similar in function. The purpose of mActiveViews is to quickly reuse the list item visible on the screen, ItemView, without the need to recreate view and bindView.

MScrapView is similar to mCachedViews + mReyclerViewPool. The purpose of mScrapView is to cache items that leave the screen and reuse items that are about to enter the screen.

RecyclerView advantage lies in the use of A.cacheviews, can be done off-screen list items ItemView into the screen without bindView rapid reuse; B. RecyclerPool can be used for multiple RecyclerView, in specific scenarios, such as viewpaper+ multiple list pages have the advantage. Objectively speaking, RecyclerView has strengthened and improved the cache mechanism of ListView in specific scenarios.

RecyclerView RecyclerView.ViewHolder cache recyclerView. ViewHolder cache recyclerView. ViewHolder cache RecyclerView.

ListView Cache View.

In RecyclerView, mCacheViews(off-screen) cache is obtained by matching POS to obtain the cache of the target location. The advantage of this is that when the data source data is unchanged, there is no need to re-bindView, and it is also off-screen cache. Pos – > View; pos – >view; pos – >view; In RecyclerView, viewholder is obtained by pos, that is, pos – > (View, Viewholder, flag);

2, RecyclerView advantages

The differences between RecyclerView and ListView in basic use are as follows:

Write ViewHolder to normalize RecyclerView reuse Item work for you. No longer need to call setTag RecyclerView as ListView requires an extra step of LayoutManager Settings

3. ListView usage scenario

If the page is not complex, also does not need too many functions, only needs the simple list function, then can continue to use ListView, after all, the implementation is easier than RecyclerView.

Custom can set the maximum height of RecycleView

There is a requirement in the project to support lists with a maximum height, but reyCycleView does not have this property, so what do we do?

class PHMaxHeightRecyclerView : RecyclerView {// maxHeight private var mMaxHeight = 0 /*** * maxHeight */ Int) { this.mMaxHeight = maxHeight requestLayout() } constructor(context: Context) : super(context) {} constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { init(context, attrs) } constructor(context: Context, attrs: AttributeSet? , defStyleAttr: Int) : super(context, attrs, defStyleAttr) { init(context, attrs) } private fun init(context: Context, attrs: AttributeSet?) { val arr = context.obtainStyledAttributes(attrs, R.styleable.PHMaxRecyclerView) mMaxHeight = arr.getLayoutDimension(R.styleable.PHMaxRecyclerView_maxHeight, mMaxHeight) arr.recycle() } override fun onMeasure(widthSpec: Int, heightSpec: Int) { super.onMeasure(widthSpec, heightSpec) val height = measuredHeight if (mMaxHeight ! = 0 && height > mMaxHeight) { setMeasuredDimension(widthSpec, mMaxHeight) } else { setMeasuredDimension(widthSpec, height) } } }Copy the code

Use:

<com.utils.PHMaxHeightRecyclerView
    android:id="@+id/max_recycleview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:maxHeight="420mm"
    >

</com.utils.PHMaxHeightRecyclerView>
Copy the code

Isn’t that easy

conclusion

Or recommend you use RecycleView, after all, it is to replace the ListView and GridView, better performance, more flexible, why not use, you say