当ScrollView嵌套RecyclerView时,会出现滑动卡顿,不平滑的效果。对此有两种解决方案。
方案一
设置RecyclerView属性方法
recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
或者直接在recycleview中 添加属性
android:nestedScrollingEnabled="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);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
通过以上操作,界面就不会再卡顿了,还原了原本的惯性。