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


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

  • 相关阅读:
    如何区分DDR1 DDR2 DDR3内存条
    《闪电战》德军攻略
    WINDOWS SERVER 2008 R2安装指南
    【django】django学得好迷茫啊 来个学习规划吧
    【阅读】提问的智慧+有效的报告BUG
    【Python】logging模块学习笔记
    【接口测试】进度表
    【django】django深入学习笔记
    【随笔】2014工作总结
    【英语】Bingo口语笔记(47)
  • 原文地址:https://www.cnblogs.com/riskyer/p/3333777.html
Copyright © 2020-2023  润新知