美文网首页
自定义ViewGroup(1)

自定义ViewGroup(1)

作者: 涛涛123759 | 来源:发表于2018-04-13 14:00 被阅读9次

滑动左右两边的ListView独立滑动,滑动中间的ListView整体滑动

实现代码:

    public class MyLinearLayout extends LinearLayout {

        public MyLinearLayout(Context context) {
            this(context, null);
        }

        public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
            this(context, attrs, 0);
        }

        public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

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

        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            return true;
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            int width = getWidth() / getChildCount();
            int height = getHeight();
            int count = getChildCount();

            float eventX = event.getX();

            if (eventX < width) {// 滑动左边的 listView
                event.setLocation(width, event.getY());
                getChildAt(0).dispatchTouchEvent(event);

            } else if (eventX > width && eventX < 2 * width) {
                float eventY = event.getY();
                if (eventY < height ) {
                    event.setLocation(width / 2, event.getY());
                    for (int i = 0; i < count; i ++){
                        View child = getChildAt(i);
                        child.dispatchTouchEvent(event);
                    }
                    return true;
                } else if (eventY > height / 2){
    //                event.setLocation(width / 2, event.getY());
    //                getChildAt(1).dispatchTouchEvent(event);
                }
                return true;
            } else if (eventX > 2 * width){
                event.setLocation(width / 2, event.getY());
                getChildAt(2).dispatchTouchEvent(event);
                return true;
            }

            return true;
        }

    }

相关文章

网友评论

      本文标题:自定义ViewGroup(1)

      本文链接:https://www.haomeiwen.com/subject/weuckftx.html