美文网首页
自定义左右滑动布局

自定义左右滑动布局

作者: 騒年 | 来源:发表于2016-05-27 17:46 被阅读0次

    自定义布局左右滑动,内含GridView

        public class SlideLayout extends LinearLayout implements View.OnTouchListener {
        /**
         * 手指按下的横坐标
         */
        private float xDown;
    
        /**
         * 在被判定为滚动之前用户手指可以移动的最大值。
         */
        private int touchSlop;
        private boolean leftViewShow = true;
        private View lefter;
    
        private ViewGroup.MarginLayoutParams lefterLayoutParams;
        private int leftWidth;
        private GridView gridView;
        private boolean isSlided = false;
    
        public SlideLayout(Context context) {
            super(context);
            init(context);
        }
    
        public SlideLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }
    
        public SlideLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init(context);
        }
    
        private void init(Context context) {
            touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
            lefter = LayoutInflater.from(context).inflate(R.layout.lefter_layout, null, true);
            addView(lefter, 0);
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            super.onLayout(changed, l, t, r, b);
            lefterLayoutParams = (MarginLayoutParams) lefter.getLayoutParams();
            leftWidth = lefter.getWidth();
            gridView = (GridView) getChildAt(1);
            gridView.setOnTouchListener(this);
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int width = -leftWidth / 2;
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    xDown = event.getRawX();
                    break;
                case MotionEvent.ACTION_MOVE:
                    float xMove = event.getRawX();
                    int distance = (int) (xMove - xDown);
    
                    if (Math.abs(distance) < touchSlop) {
                        isSlided = false;
                    } else
                        isSlided = true;
                    // 要让gridView失去焦点,否则被点击的那一项会一直处于选中状态
                    gridView.setPressed(false);
                    gridView.setFocusable(false);
                    gridView.setFocusableInTouchMode(false);
    
                    if (distance < 0) {
                        if (leftViewShow) {
                            lefterLayoutParams.leftMargin = distance / 3;
                            lefter.setLayoutParams(lefterLayoutParams);
                        }
                    }
    
                    if (distance > 0) {
                        if (!leftViewShow) {
                            lefterLayoutParams.leftMargin = distance / 3 - leftWidth;
                            lefter.setLayoutParams(lefterLayoutParams);
                        }
                    }
    
                    break;
                case MotionEvent.ACTION_UP:
                    // 判断leftView的坐标,如果隐藏的部分超过一半,就直接滑动过去全部隐藏
                    // 如果显示的部分超过一半,也直接滑动过来直接显示
                    if (lefterLayoutParams.leftMargin <= width) {
                        lefterLayoutParams.leftMargin = -leftWidth;
                        lefter.setLayoutParams(lefterLayoutParams);
                        leftViewShow = false;
                    } else if (lefterLayoutParams.leftMargin >= width) {
                        lefterLayoutParams.leftMargin = 0;
                        lefter.setLayoutParams(lefterLayoutParams);
                        leftViewShow = true;
                    }
    
                    // 解决onTouch和onClick的冲突
                    return isSlided;
            }
            return false;
        }
    
    }
    
    

    相关文章

      网友评论

          本文标题:自定义左右滑动布局

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