有這樣一個(gè)需求:RecyclerView嵌套WebView時(shí)籍胯,當(dāng)WebView顯示寬度超出手機(jī)屏幕寬度時(shí),需要用手指左右滑動(dòng)WebView离福,但是由于手指在滑動(dòng)時(shí)很難保持水平滑動(dòng)杖狼,于是WebView和RecyclerView發(fā)生了滑動(dòng)沖突,當(dāng)手指稍微偏離水平方向時(shí)妖爷,WebView的滑動(dòng)便停止蝶涩,RecyclerView開始滑動(dòng),這樣的體驗(yàn)很不好絮识。于是乎便有了這樣的自定義WebView:
public class RecyclerViewFitWebView extends WebView {
public float oldY;
public float oldX;
public float newY;
public float newX;
public RecyclerViewFitWebView(Context context) {
super(context);
}
public RecyclerViewFitWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
getParent().getParent().requestDisallowInterceptTouchEvent(true);
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
newX = ev.getX();
newY = ev.getY();
break;
case MotionEvent.ACTION_MOVE:
//手指滑動(dòng)同時(shí)判斷滑動(dòng)方向绿聘,一旦滑動(dòng)方向大于+-60便調(diào)用
//getParent().getParent().requestDisallowInterceptTouchEvent(false);
//將滑動(dòng)事件交給RecyclerView來處理
oldX = newX;
oldY = newY;
newX = ev.getX();
newY = ev.getY();
float moveX = Math.abs(oldX - newX);
float moveY = Math.abs(oldY - newY);
//moveX * 1.73 < moveY ,判斷左右滑動(dòng)范圍為+-60度
if (moveX * 1.73 < moveY) {
getParent().getParent().requestDisallowInterceptTouchEvent(false);
}
break;
}
return super.onTouchEvent(ev);
}
}