当前位置:网站首页>Efficient use of RecyclerView Section 3

Efficient use of RecyclerView Section 3

2022-07-31 14:56:00 [email protected]

In the iteration of requirements development, the implementation of some specific interactive functions will always be encountered.

One, get the visible Item subscript

When developing functions, sometimes when you need to slide to the specified subscript or slide to the bottom or head of the screen, you will think of getting the currently visible item subscript for processing.

 //Get the first visible Item subscriptlayoutManager.findFirstCompletelyVisibleItemPosition()//Get the last visible Item subscriptlayoutManager.findLastCompletelyVisibleItemPosition()

Mainly use RecyclerView's LayoutManager to get the first and last visible Item subscripts

Second, ItemDecorationCount

When using RecyclerView to nest RecyclerView, if ItemDecoration is added to the child RecyclerView, and some operations such as page pull-down refresh, you will find that the spacing of the list keeps getting wider. This is caused by the reuse of Item by RecyclerView.When you need to use ItemDecorationCount to judge, to prevent repeated addition of ItemDecoration.

 //Determine whether the RecyclerView adds a dividing line, without this judgment, when the pull-down refresh is performed, the spacing will be added repeatedly, causing the spacing to become largerif (recyclerView.itemDecorationCount==0){//Add custom divider or spacing}

Third, Item slides to the center

When implementing the option list, in some scenarios, the selected item will be required to be centered, so you need to customize the LayoutManager to implement it, as follows:

import android.content.Context;import android.util.DisplayMetrics;import androidx.recyclerview.widget.LinearLayoutManager;import androidx.recyclerview.widget.LinearSmoothScroller;import androidx.recyclerview.widget.RecyclerView;public class CenterLinearLayoutManager extends LinearLayoutManager {static int lastPosition = 0;static int targetPosition = 0;public CenterLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {super(context, orientation, reverseLayout);}@Overridepublic void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {CenterSmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());smoothScroller.setTargetPosition(position);startSmoothScroll(smoothScroller);}public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int lastPosition, int position) {lastPosition = lastPosition;targetPosition = position;smoothScrollToPosition(recyclerView, state, position);}public static class CenterSmoothScroller extends LinearSmoothScroller {private static float duration = 400f;public CenterSmoothScroller(Context context) {super(context);}@Overridepublic int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);}@Overrideprotected float calculateSpeedPerPixel(DisplayMetrics displayMetrics) {float newDuration = duration / (Math.abs(targetPosition - lastPosition));return newDuration / displayMetrics.densityDpi;}@Overrideprotected int calculateTimeForScrolling(int dx) {return super.calculateTimeForScrolling(dx);}}}

When using
1) Create a custom LayoutManager

 private val centerLayoutManager by lazy {CenterLinearLayoutManager(activity,LinearLayoutManager.HORIZONTAL,false)}

2) Slide to the specified subscript

centerLayoutManager.smoothScrollToPosition(recyclerView,RecyclerView.State(),position //specified subscript)

In some scenarios, it is necessary to obtain the first and last visible subscripts in one, and calculate the item subscripts in the middle of the screen for some interactive processing.

原网站

版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/212/202207311451011501.html