本文主要是介绍ViewPager嵌套(内层ViewPager可以滑动之父类兼容listView),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package com.peopledailychina.activity.widget; import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.ViewGroup; /** * viewpage 和listview 相互冲突 将父view 传递到viewpage 里面 使用父类的方法 * parent.requestDisallowInterceptTouchEvent(true); 当 * requestDisallowInterceptTouchEvent 如果为true的时候 表示:父view 不拦截子view的touch 事件 * 这个方法只是改变flag */ public class FinalViewPager extends ViewPager {private ViewGroup parent; private float startX; private float startY; private float lastX; private float lastY; private float endX; private float endY; public boolean isFixTouch = true; enum Orientation {VERTICAL, HORIZONTAL; }public FinalViewPager(Context context) {super(context); // TODO Auto-generated constructor stub }public FinalViewPager(Context context, AttributeSet attrs) {super(context, attrs); }@Override protected void onAttachedToWindow() {// TODO Auto-generated method stub super.onAttachedToWindow(); this.parent = (ViewGroup) getParent(); }public void setNestedpParent(ViewGroup parent) {this.parent = parent; }@Override public boolean dispatchTouchEvent(MotionEvent ev) {if (isFixTouch) {if (parent == null) {return super.dispatchTouchEvent(ev); }float x = ev.getX(); float y = ev.getY(); switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:parent.requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE:float moveX = x - lastX; float moveY = y - lastY; if (Math.abs(moveX) / Math.abs(moveY) > 1) {parent.requestDisallowInterceptTouchEvent(true); } else {parent.requestDisallowInterceptTouchEvent(false); }break; case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:break; default:break; }lastX = x; lastY = y; }return super.dispatchTouchEvent(ev); }public boolean isFixTouch() {return isFixTouch; }public void setFixTouch(boolean fixTouch) {isFixTouch = fixTouch; } // @Override // public boolean onTouchEvent(MotionEvent arg0) { // // TODO Auto-generated method stub // switch (arg0.getAction()) { // case MotionEvent.ACTION_UP: // case MotionEvent.ACTION_CANCEL: // return true; // default: // break; // } // return super.onTouchEvent(arg0); // } private Orientation getScrollOrientation(MotionEvent ev) {switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:startX = ev.getX(); startY = ev.getY(); break; case MotionEvent.ACTION_MOVE:float moveX = ev.getX() - startX; float moveY = ev.getY() - startY; if (Math.abs(moveX) >= Math.abs(moveY)) {return Orientation.HORIZONTAL; }break; case MotionEvent.ACTION_UP:startX = 0; startY = 0; break; default:break; }return Orientation.VERTICAL; } }
这篇关于ViewPager嵌套(内层ViewPager可以滑动之父类兼容listView)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!