import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import com.yitong.mobile.component.logging.Logs;
public class OptimizedRecyclerView extends RecyclerView {
private int mScrollPointerId;
private int mInitialTouchX, mInitialTouchY;
private int mTouchSlop;
public OptimizedRecyclerView(@NonNull Context context) {
super(context);
init();
}
public OptimizedRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public OptimizedRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
ViewConfiguration vc = ViewConfiguration.get(getContext());
this.mTouchSlop = vc.getScaledTouchSlop();
}
@Override
public void setScrollingTouchSlop(int slopConstant) {
ViewConfiguration vc = ViewConfiguration.get(this.getContext());
switch (slopConstant) {
case 0:
this.mTouchSlop = vc.getScaledTouchSlop();
case 1:
this.mTouchSlop = vc.getScaledPagingTouchSlop();
break;
default:
Logs.w("RecyclerView", "setScrollingTouchSlop(): bad argument constant " + slopConstant + "; using default value");
}
super.setScrollingTouchSlop(slopConstant);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
boolean canScrollHorizontally = getLayoutManager().canScrollHorizontally();
boolean canScrollVertically = getLayoutManager().canScrollVertically();
int action = e.getActionMasked();
int actionIndex = e.getActionIndex();
switch (action) {
case 0:
mScrollPointerId = e.getPointerId(0);
this.mInitialTouchX = (int) (e.getX() + 0.5F);
this.mInitialTouchY = (int) (e.getY() + 0.5F);
return super.onInterceptTouchEvent(e);
case 2:
int index = e.findPointerIndex(this.mScrollPointerId);
if (index < 0) {
Logs.e("RecyclerView", "Error processing scroll; pointer index for id " + this.mScrollPointerId + " not found. Did any MotionEvents get skipped?");
return false;
}
int x = (int) (e.getX(index) + 0.5F);
int y = (int) (e.getY(index) + 0.5F);
if (getScrollState() != 1) {
int dx = x - this.mInitialTouchX;
int dy = y - this.mInitialTouchY;
boolean startScroll = false;
if (canScrollHorizontally && Math.abs(dx) > this.mTouchSlop && Math.abs(dx) > Math.abs(dy)) {
startScroll = true;
}
if (canScrollVertically && Math.abs(dy) > this.mTouchSlop && Math.abs(dy) > Math.abs(dx)) {
startScroll = true;
}
Logs.d("MyRecyclerView", "canX:" + canScrollHorizontally + "--canY" + canScrollVertically + "--dx:" + dx + "--dy:" + dy + "--startScorll:" + startScroll + "--mTouchSlop" + mTouchSlop);
return startScroll && super.onInterceptTouchEvent(e);
}
return super.onInterceptTouchEvent(e);
case 5:
this.mScrollPointerId = e.getPointerId(actionIndex);
this.mInitialTouchX = (int) (e.getX(actionIndex) + 0.5F);
this.mInitialTouchY = (int) (e.getY(actionIndex) + 0.5F);
return super.onInterceptTouchEvent(e);
}
return super.onInterceptTouchEvent(e);
}
}
一个解决滑动冲突的RecyclerView
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...