当
ScrollView嵌套RecyclerView
时,会出现滑动卡顿,不平滑的效果。对此有两种解决方案。
方案一
设置
RecyclerView
属性方法
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
方案二
如果方案一无效,不妨试试重写
ScrollView
的onInterceptTouchEvent()
方法,强制让其触摸事件都交给其子控件去处理
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);
}
}
通过以上操作,界面就不会再卡顿了,还原了原本的惯性。