1. 程式人生 > >Android 解決ScrollView嵌套RecyclerView導致滑動不流暢的問題

Android 解決ScrollView嵌套RecyclerView導致滑動不流暢的問題

nes gets pic config 做的 dsc nbsp 導致 spa

最近做的項目中遇到了ScrollView嵌套RecyclerView,剛寫完功能測試,直接卡出翔了,後來通過網上查找資料和

自己的實踐,找出了兩種方法解決這個問題。

首先來個最簡單的方法:

recyclerView.setNestedScrollingEnabled(false);

這個方法就可以解決這一問題。

既然有首先那肯定有第二種解決的辦法,只不過相對於第一種方法來說就太麻煩了。

我們知道ScrollView嵌套listView或者GridView的時候需要自定義listView或者是GridView,在這兒我們也需要自定義,

但是也有區別,我們這兒不是自定義RecyclerView,而是自定義ScrollView。下面給出自定義的方法。

public class ScrollView extends ScrollView{

    private int downX;

    private int downY;

    private int mTouchSlop;

    public TopicScrollView(Context context) {

        super(context);

        mTouchSlop= ViewConfiguration.get(context).getScaledTouchSlop();

    }





    
public TopicScrollView(Context context, AttributeSet attrs) { super(context, attrs); mTouchSlop= ViewConfiguration.get(context).getScaledTouchSlop(); } public TopicScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mTouchSlop
= ViewConfiguration.get(context).getScaledTouchSlop(); } @Override public boolean onInterceptTouchEvent(MotionEvent e) { int action = e.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: downX = (int) e.getRawX(); downY = (int) e.getRawY(); break; case MotionEvent.ACTION_MOVE: int moveY = (int) e.getRawY(); if (Math.abs(moveY - downY) > mTouchSlop) { return true; } } return super.onInterceptTouchEvent(e); } }

Android 解決ScrollView嵌套RecyclerView導致滑動不流暢的問題