android:自定义横向滑动的 ViewGroup

效果图:
在这里插入图片描述
大概就是这样子的,然后可以左右滑动。

原来是想做成ViewPager的那种效果的,但是感觉那种更不实用。

这个效果很类似系统控件 HorizontalScrollView 了。

关键代码还是测量,布局,触摸事件拦截,触摸滑动。

完整代码:

public class HorizontalView extends ViewGroup {

    private static final int DEFAULT_DP = 20;
    int defaultSize;
    private int touchSlop;
    private float downInterceptX;
    private float downInterceptY;
    private int tapTimeout;
    private Scroller mScroller;
    private long elapsedRealtime;
    private int goodSize;

    public HorizontalView(Context context) {
        super(context);
        init(context);
    }

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

    public HorizontalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public HorizontalView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }


    private void init(Context context) {
        defaultSize = (int) ViewUtils.dp2px(context, DEFAULT_DP);
        LogUtils.e("default size ========%s", defaultSize);
        setWillNotDraw(false);
        ViewConfiguration configuration = ViewConfiguration.get(context);
        touchSlop = configuration.getScaledTouchSlop();
        tapTimeout = ViewConfiguration.getTapTimeout();
        mScroller = new Scroller(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        LogUtils.w(ViewUtils.logMeasureSpec(widthMeasureSpec, "1-w"));
        LogUtils.w(ViewUtils.logMeasureSpec(heightMeasureSpec, "1-h"));
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        LogUtils.i(ViewUtils.logMeasureSpec(widthMeasureSpec, "2-w"));
        LogUtils.i(ViewUtils.logMeasureSpec(heightMeasureSpec, "2-h"));

        int wMode = MeasureSpec.getMode(widthMeasureSpec);
        int wSize = MeasureSpec.getSize(widthMeasureSpec);
        int hMode = MeasureSpec.getMode(heightMeasureSpec);
        int hSize = MeasureSpec.getSize(heightMeasureSpec);
        this.goodSize = wSize;
        int childCount = getChildCount();
        if (childCount == 0) {
            LogUtils.e("no need to set wh, use super.");
            return;
        }
        // measureChildren(widthMeasureSpec, heightMeasureSpec); // 必须要调用
        int w = 0, h = defaultSize;
        for (int i = 0; i < childCount; ++i) {
            View child = getChildAt(i);
            if (child == null || child.getVisibility() == GONE) {
                continue;
            }
            measureChildWithMargins(child,
                    0, widthMeasureSpec,
                    0, heightMeasureSpec);
            int measuredWidth = child.getMeasuredWidth();
            int measuredHeight = child.getMeasuredHeight();
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            LogUtils.i("index=%s,wh=(%s,%s), lp:[%s,%s]", i,
                    measuredWidth, measuredHeight,
                    lp.leftMargin, lp.rightMargin);
            h = Math.max(h, measuredHeight + lp.topMargin + lp.bottomMargin);
            w += measuredWidth + lp.leftMargin + lp.rightMargin;
        }
        w += getPaddingStart() + getPaddingEnd();
        h += getPaddingTop() + getPaddingBottom();
        // w = Math.min(w, wSize); // 此时的 wSize 为 match_parent具体数值的大小
        // 保证 w 不会超过 parent 预期大小
        LogUtils.e("计算前的 ow,oh=(%s,%s),#,计算后的 wh(%s,%s)", wSize, hSize, w, h);
        if (wMode == MeasureSpec.EXACTLY && hMode == MeasureSpec.EXACTLY) {
            // LogUtils.e("no need to set wh, use super.");
            LogUtils.e("m1");
            setMeasuredDimension(w > wSize ? w : wSize, h > hSize ? h : hSize);
        } else if (wMode == MeasureSpec.EXACTLY) {
            LogUtils.e("m2");
            setMeasuredDimension(wSize, h);
        } else if (hMode == MeasureSpec.EXACTLY) {
            LogUtils.e("m3");
            setMeasuredDimension(w, hSize);
        } else {
            LogUtils.e("m4");
            setMeasuredDimension(w, h);
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        LogUtils.i("- -onLayout(%s,%s,%s,%s)", l, t, r, b);
        l += getPaddingStart();
        r -= getPaddingEnd();
        t += getPaddingTop();
        b -= getPaddingBottom();
        int childCount = getChildCount();
        LogUtils.e("childCount = %s", childCount);
        for (int i = 0; i < childCount; ++i) {
            View child = getChildAt(i);
            if (child == null || child.getVisibility() == GONE) {
                continue;
            }
            int measuredWidth = child.getMeasuredWidth();
            int measuredHeight = child.getMeasuredHeight();
            MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
            int w = measuredWidth + lp.leftMargin + lp.rightMargin;
            l += lp.leftMargin;
            t += lp.topMargin;
            LogUtils.d("layout child:");
            LogUtils.d("index=%s,#,l,t,r,b (%s,%s,%s,%s)",
                    i,
                    l, t,
                    l + measuredWidth, t + measuredWidth);
            child.layout(l, t, l + measuredWidth, t + measuredHeight);
            // for next child
            l += measuredWidth + lp.rightMargin;
            t -= lp.topMargin; // 下一个不能沿用上一个的 marginTop, 因为是横向排列,不是总纵向
            // t += measuredHeight + lp.bottomMargin;
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        LogUtils.e("old(%s,%s), now(%s,%s)", oldw, oldh, w, h);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        LogUtils.e("generateDefaultLayoutParams");
        return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        LogUtils.e("generateLayoutParams p");
        return new MarginLayoutParams(p);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean intercept = false;
        int actionMasked = ev.getActionMasked();
        float x = ev.getX();
        float y = ev.getY();
        switch (actionMasked) {
            case MotionEvent.ACTION_DOWN:
                downInterceptX = x;
                downInterceptY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                if (Math.abs(x - downInterceptX) > Math.abs(y - downInterceptY)
                        && Math.abs(x - downInterceptX) > touchSlop) {
                    intercept = true;
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return intercept || super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                elapsedRealtime = SystemClock.elapsedRealtime();
                break;
            case MotionEvent.ACTION_MOVE:
                float diffX = x - downInterceptX;
                float diffY = y - downInterceptY;
                scrollBy((int) -diffX, 0);
                // 由于 move 会被多次调用
                downInterceptX = x;
                downInterceptY = y;
                break;
            case MotionEvent.ACTION_UP:
                long realtime = SystemClock.elapsedRealtime();
                if (Math.abs(y - downInterceptY) < touchSlop
                        && Math.abs(x - downInterceptX) < touchSlop
                        && realtime - elapsedRealtime < tapTimeout) {
                    LogUtils.e("click me....");
                    performClick();
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return true;
    }

    @Override
    public boolean performClick() {
        return super.performClick();
    }


    @Override
    public void computeScroll() {
        // 没用上,没处理 ACTION_UP
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }

    @Override
    public void scrollTo(int x, int y) {
        // 重写 scrollTo 之后,不让其滑动超出边界
        LogUtils.v("scrollTo(%s,%s)", x, y);

        if (x > getWidth() - goodSize) {
            LogUtils.v("getWidth(), goodSize=%s,%s", getWidth(), goodSize);
            x = getWidth() - goodSize;
        }
        if (x < 0) {
            x = 0;
        }
        if (x != getScrollX()) {
            super.scrollTo(x, y);
        }
    }
}

主要步骤:

  1. 重写 generateLayoutParams 三个方法,让 child 支持 margin 属性
  2. 重写 onMeasure , 自己的宽度是 children 宽度总和
  3. 重写 onLayout , 自己的高度是 children 的最大值
  4. 重写 onIntercept, 是横向滑动就拦截
  5. 重写 onTouch, move 的时候,通过 scrollBy 进行滑动
  6. 重写 scrollTo, 不让其滑动超过自身的左右最大边界
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android自定义ViewGroup是指在Android开发中,通过继承ViewGroup类来创建自定义的布局容器。自定义ViewGroup可以用于实现一些特殊的布局效果,比如侧滑菜单、滑动卡片等等。通过自定义ViewGroup,我们可以更灵活地控制子视图的布局和交互行为,以满足特定的需求。自定义ViewGroup的实现主要包括重写onMeasure()方法和onLayout()方法,来测量和布局子视图。同时,我们还可以通过重写onInterceptTouchEvent()方法和onTouchEvent()方法来处理触摸事件,实现自定义的交互效果。如果你对自定义ViewGroup还不是很了解,或者正想学习如何自定义,可以参考相关的教程和文档,如引用\[1\]和引用\[2\]所提到的博客和官方文档。 #### 引用[.reference_title] - *1* [Android 手把手教您自定义ViewGroup(一)](https://blog.csdn.net/iteye_563/article/details/82601716)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [使用LayoutParams自定义安卓ViewGroup](https://blog.csdn.net/lfq88/article/details/127268493)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Android自定义ViewGroup](https://blog.csdn.net/farsight2009/article/details/62046643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值