android中控件公用产生的冲突的解决办法

1.ViewPager嵌套HorizontalScrollView滑动冲突的解决办法,重写ViewPager

 

public class ZdyViewPage extends ViewPager {

	public ZdyViewPage(Context context) {
		super(context);
	}

	public ZdyViewPage(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected boolean canScroll(View v, boolean arg1, int arg2, int arg3,
			int arg4) {
		if (v instanceof HorizontalScrollView) {
			return true;
		}
		return super.canScroll(v, arg1, arg2, arg3, arg4);
	}

}

 


2.ScrollView嵌套ViewPager滑动冲突的解决办法,重写ScrollView

 

public class NotifyingScrollView extends ScrollView {
    private Context mContext;
    
    /*避免Scroview和ViewPager控件冲突的解决办法*/
    private boolean canScroll;
    private GestureDetector mGestureDetector;
    View.OnTouchListener mGestureListener;

    public NotifyingScrollView(Context context) {
        super(context);
        mContext = context;
    }

    public NotifyingScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mGestureDetector = new GestureDetector(new YScrollDetector());
        canScroll = true;
    }

    public NotifyingScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }
    
    /*避免Scroview和ViewPager控件冲突的解决办法*/
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_UP)
            canScroll = true;
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }
 
    class YScrollDetector extends SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if(canScroll)
                if (Math.abs(distanceY) >= Math.abs(distanceX))
                    canScroll = true;
                else
                    canScroll = false;
            return canScroll;
        }
    }
}


3.ListView嵌套ViewPager滑动冲突的解决办法,重写ListView

 

 

public class MyListview extends ListView {

	private Context mContext;

	public MyListview(Context context) {
		super(context);
		mContext = context;
	}

	public MyListview(Context context, AttributeSet attrs, Context mContext) {
		super(context, attrs);
		this.mContext = mContext;
	}

	public MyListview(Context context, AttributeSet attrs, int defStyle,
			Context mContext) {
		super(context, attrs, defStyle);
		this.mContext = mContext;
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		super.onInterceptTouchEvent(ev);
		return false;
	}

}


在开发中还会遇到很多的控件组合,大家有遇到过的希望都能把代码贴上来,大家共同学习!!!

 

 

posted on 2013-09-22 20:23  you Richer  阅读(374)  评论(1编辑  收藏  举报