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;
	}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值