ScrollView嵌套RecyclerView滑动卡顿,无惯性

来源:互联网 发布:哪种笔记本适用于编程 编辑:程序博客网 时间:2024/04/28 19:58

ScrollView嵌套RecyclerView时,会出现滑动卡顿,不平滑的效果。对此有两种解决方案。

方案一

设置RecyclerView属性方法

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

方案二

如果方案一无效,不妨试试重写ScrollViewonInterceptTouchEvent()方法,强制让其触摸事件都交给其子控件去处理

public class RecycleScrollView extends ScrollView {    private int downX;    private int downY;    private int mTouchSlop;    public RecycleScrollView(Context context) {        super(context);        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();    }    public RecycleScrollView(Context context, AttributeSet attrs) {        super(context, attrs);        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();    }    public RecycleScrollView(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);    }}

通过以上操作,界面就不会再卡顿了,还原了原本的惯性。

阅读全文
0 0
原创粉丝点击