In a word, space for time, time for space
It is recommended to useBaseRecyclerViewAdapterHelper
- Generally speaking, negative data operations are performed in a custom Adapter. For convenience, some simple logic processing is also included in the Adapter. The problem with this is that these logical operations are performed multiple times during Item reuse.
Example: general helper. SetText (R.i which vTime, TimeUtils. Millis2String (timestamp) optimization example: data class Data(val timestamp:Long){ val formatTime:String by lazy { TimeUtils.millis2String(timestamp) } } helper.setText(R.id.tvTime,data.formatTime)Copy the code
In the above code, we set a lazy data formatTime in the entity class, which holds the formatted date data with a single argument, so that the date formatting logic is performed only once in an item and then directly evaluated
- RecyclerView will add and delete data
Example: general mData. AddAll (list) adapter. NotifyDataSetChanged () optimization example: adapter. AddData (list)Copy the code
Refresh the entire list of data, and refresh only new data, the former must cause page flashing, do not use unless the whole data refresh
-
ConstraintLayout is a layout optimization that minimizes layout nesting and controls as few as possible. Complex controls can use the form of a custom View. ConstraintLayout in ItemView is not a very good performance and is not recommended
-
For some RecyclerView nested RecyclerView layouts, such as the outer list, with an internal header following the list of items, you can use multiple layouts for display, rather than using nesting
-
The XML file is synchronized out of the ItemView by a time-consuming IO operation. The View View is synchronized out of the View View
-
If the height of RecyclerView’s ItemView is fixed, use setHasFixedSize(True) to avoid measuring the height of items multiple times
-
For RecyclerView, if there is no requirement to use item display animation, you can cancel item animation
((SimpleItemAnimator)mRvList.getItemAnimator()).setSupportsChangeAnimations(false);
Copy the code
-
For a page a Tab with multiple RecyclerView, and RecyclerView to use the same Adapter, then you can use setRecycledViewPool(pool), shared recycling pool, Avoid creating a recycling pool for every RecyclerView, especially when RecyclerView nested RecyclerView, internal RecyclerView must use the same Adapter, this time it is necessary to use recycling pool
-
Depending on the situation, use setItemViewCacheSize(size) to increase the number of RecyclerView caches and trade space for time to improve fluency
-
Instead of using setOnClickListener for item click times in the reuse section, which would repeat the setting of the click listener, create a Listener interface that passes in the control’s ID and the current item position to handle the click listener based on the ID and position
-
Depending on the situation, you can add slide monitor to RecyclerView, cancel image loading when fast sliding, start image loading when slow sliding or stop, cooperate with picture loading framework Glide Picasso, etc
-
Images can be preloaded using Glide’s Preload () or other image frames, depending on the case, to display images without reloading them directly from the cache
-
Rewrite the LayoutManager getExtraLayoutSpace() method to return the screen height, used to increase the RecyclerView reserved extra space
-
SetInitialItemPrefetchCount (), the next entry has not yet appeared, the next entry has not been created, binding and layout screen rendering, however, before the entry has been completed, the next entry has not appeared, this time there will be a long time, so this part of the use, The creation and binding of the next item is carried out first, and when the next item appears, it just needs to be rendered. The number control is generally 1 more than the number of screens, for example, a screen will display a little more than 5, then pass in 6