Projects often need to realize the ScrollView embedded in one or more recyclerViews. This usually leads to several problems

  • Page sliders are stuck
  • The ScrollView height is abnormal
  • RecyclerView content display is not complete

This paper will use a variety of ways to solve the above problems

Slide caton solution

If there is only slip stuck problem, you can use the following two simple ways to solve the problem quickly

RecyclerView internal method was used

12
recyclerView.setHasFixedSize(true); recyclerView.setNestedScrollingEnabled(false);

Wherein, the setHasFixedSize(true) method makes RecyclerView can fix its size is not affected by adapter changes; And setNestedScrollingeEnabled (false) method is a further call setNestedScrollingeEnabled RecyclerView internal NestedScrollingChildHelper object (false) The method is as follows

123
public void setNestedScrollingEnabled(boolean enabled) { getScrollingChildHelper().setNestedScrollingEnabled(enabled); }

And NestedScrollingChildHelper object through close RecyclerView nested sliding characteristics, the method is as follows

123456
public void setNestedScrollingEnabled(boolean enabled) { if (mIsNestedScrollingEnabled) { ViewCompat.stopNestedScroll(mView); } mIsNestedScrollingEnabled = enabled; }

In this way, it limits the sliding of RecyclerView itself, and the whole page sliding can be realized only by ScrollView, which can solve the problem of sliding lag

Rewrite the LayoutManager

123456
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) { @Override public boolean canScrollVertically() { return false; }};

This method makes the vertical sliding of RecyclerView always return false, its purpose is also to limit its own sliding

Integrated solution

To solve the above three problems, you can use the following methods

Insert the LinearLayout/RelativeLayout

A layer of LinearLayout insertion on the original layout/RelativeLayout, form the following layout

Rewrite the LayoutManager

The core idea of this method is to override the onMeasure() method in LayoutManager, namely

1234
@Overridepublic void onMeasure(RecyclerView.Recycler recycler, RecyclerView.State state, int widthSpec, int heightSpec) { super.onMeasure(recycler, state, widthSpec, heightSpec); }

To re-realize the calculation of RecyclerView height, so that it can show the correct height in ScrollView, the specific rewriting method can refer to this article

www.cnblogs.com/tianzhijiex…

Rewrite the ScrollView

The core idea of this method is to intercept the sliding event by rewriting the onInterceptTouchEvent(MotionEvent EV) method of ScrollView, so that the sliding event can be directly transferred to RecyclerView. The specific rewriting method can be referred to as follows

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
/** * Created by YH on 2017/10/10. */ public class RecyclerScrollView extends ScrollView { private int slop; private int touch; public RecyclerScrollView(Context context) { super(context); setSlop(context); } public RecyclerScrollView(Context context, AttributeSet attrs) { super(context, attrs); setSlop(context); } public RecyclerScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setSlop(context); } /** * Intercept the current touch event * @param ev touch event * @return true: Call the onMotionEvent() method, */ @override public Boolean onInterceptTouchEvent(MotionEvent ev) {switch (ev.getAction()) {case ACTION_DOWN: // Save the current touch ordinate value touch = (int) ev.getrawy (); break; ACTION_MOVE: // If (math.abs ((int) ev.getrawy () -touch) > slop) return true; break; } return super.onInterceptTouchEvent(ev); } /** * get the touch SLOp value of the corresponding context / private void setSlop(context context) {slop = ViewConfiguration.get(context).getScaledTouchSlop(); }}

In fact, although we can use a variety of ways to solve a series of problems caused by ScrollView nested RecyclerView, the above solutions will make RecyclerView display all contents at one time in the page loading process, so when there are too many items under RecyclerView, This will affect the operating efficiency of the entire application. Based on this, in this case, we should try to avoid using ScrollView nested RecyclerView layout