自定义ViewGroup

本文介绍了一种自定义ViewGroup实现的ScrollView效果,当滑动距离超过屏幕高度的1/3时,会自动滚动到相邻的控件。通过继承ViewGroup并重写关键方法,实现了特殊的滚动行为。

实现ScrollView的效果,且滚动带有“黏性”,滑动距离大于控件高度(屏幕高度)的1/3时,会自动滚动到上一个(下一个)控件

public class DiyViewGroup extends ViewGroup {
    private int mScreenHeight;
    private int mLastY;
    private Scroller mScroller;
    private int mStart;
    private int mEnd;

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

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

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

    // 初始化mScreenHeight和mScroller
    private void init(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenHeight = outMetrics.heightPixels;
        mScroller = new Scroller(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        // 对子View进行布局
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if(child.getVisibility() != GONE) {
                child.layout(l, mScreenHeight * i, r, mScreenHeight * (i + 1));
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int count = getChildCount();
        // 通知子View对自身进行测量
        for (int i = 0; i <count ; i++) {
            View childView = getChildAt(i);
            // 可能子View的宽高是match_parent,所以要将父控件的宽高传进去
            measureChild(childView, widthMeasureSpec, heightMeasureSpec);
        }
        // 设置ViewGroup自己的宽高
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = mScreenHeight * count;
        setMeasuredDimension(width, height);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("qqq", "onTouchEvent: down");
                mLastY = y;
                // 记录触摸起点
                // getScrollY()从最原始位置向上滚动的距离
                mStart = getScrollY();
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("qqq", "onTouchEvent: move");
                if(!mScroller.isFinished()) {
                    // 停止动画,立即移动到终点位置
                    mScroller.abortAnimation();
                }
                int dy = mLastY - y;
                if(getScrollY() < 0) {
                    dy = 0;
                }
                if(getScrollY() + mScreenHeight > getHeight()) {
                    dy = 0;
                }
                scrollBy(0, dy);
                mLastY = y;
                break;
            case MotionEvent.ACTION_UP:
                Log.i("qqq", "onTouchEvent: up");
                // 记录触摸终点
                mEnd = getScrollY();
                int dScrollY = mEnd - mStart;
                if(dScrollY > 0) {
                    if(dScrollY < mScreenHeight / 3) {
                        // 第三个参数第四个参数分别是向左移动的距离和向上移动的距离
                        mScroller.startScroll(0, getScrollY(), 0, -dScrollY);
                    } else {
                        mScroller.startScroll(0, getScrollY(), 0, mScreenHeight - dScrollY);
                    }
                } else {
                    if(-dScrollY < mScreenHeight / 3) {
                        mScroller.startScroll(0, getScrollY(), 0, -dScrollY);
                    } else {
                        mScroller.startScroll(0, getScrollY(), 0, -(mScreenHeight - (-dScrollY)));
                    }
                }
                break;
        }
        postInvalidate();
        return true;
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if(mScroller.computeScrollOffset()) {
            scrollTo(0, mScroller.getCurrY());
            postInvalidate();
        }
    }
}


这个是完整源码 python实现 Flask,Vue 【python毕业设计】基于Python的Flask+Vue物业管理系统 源码+论文+sql脚本 完整版 数据库是mysql 本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发展随后依照传统的软件开发流程,最先为系统挑选适用的言语和软件开发平台,依据需求分析开展控制模块制做和数据库查询构造设计,随后依据系统整体功能模块的设计,制作系统的功能模块图、E-R图。随后,设计框架,依据设计的框架撰写编码,完成系统的每个功能模块。最终,对基本系统开展了检测,包含软件性能测试、单元测试和性能指标。测试结果表明,该系统能够实现所需的功能,运行状况尚可并无明显缺点。本文首先实现了基于Python的Flask+Vue物业管理系统技术的发
### 创建或使用自定义 ViewGroup in Android 在 Android 中,创建自定义 `ViewGroup` 是一个复杂但非常强大的功能。它允许开发者根据自己的需求设计和实现布局逻辑。以下是一个完整的指南,涵盖从基本概念到具体实现的各个方面。 #### 1. 自定义 ViewGroup 的基本原理 `ViewGroup` 是一个容器类,它可以包含其他 `View` 或 `ViewGroup`。与普通的 `View` 不同,`ViewGroup` 需要处理子元素的测量、布局和绘制。因此,在自定义 `ViewGroup` 时,通常需要重写以下几个方法: - `onMeasure()`:用于测量当前视图及其子视图的大小[^1]。 - `onLayout()`:用于确定每个子视图的位置[^1]。 - `generateLayoutParams()` 和 `checkLayoutParams()`:用于处理子视图的布局参数,避免出现 `ClassCastException` 等错误[^2]。 #### 2. 实现步骤 ##### (1) 创建自定义 ViewGroup 类 首先,继承 `ViewGroup` 并定义构造函数。例如: ```java public class CustomViewGroup extends ViewGroup { public CustomViewGroup(Context context) { super(context); } public CustomViewGroup(Context context, AttributeSet attrs) { super(context, attrs); } public CustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } } ``` ##### (2) 重写 `onMeasure()` 方法 `onMeasure()` 方法用于测量当前视图及其子视图的大小。可以通过遍历所有子视图并调用它们的 `measure()` 方法来完成测量过程。例如: ```java @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); } setMeasuredDimension(width, height); // 设置最终的宽高 } ``` ##### (3) 重写 `onLayout()` 方法 `onLayout()` 方法用于确定每个子视图的位置。通过调用子视图的 `layout()` 方法,可以将它们放置在指定的位置。例如: ```java @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = getChildCount(); int x = 0; int y = 0; for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (child.getVisibility() != GONE) { int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); child.layout(x, y, x + childWidth, y + childHeight); x += childWidth; // 假设水平排列 } } } ``` ##### (4) 处理布局参数 为了避免运行时异常(如 `ClassCastException`),需要重写 `generateLayoutParams()` 和 `checkLayoutParams()` 方法。例如: ```java @Override protected LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); } @Override protected boolean checkLayoutParams(LayoutParams p) { return p instanceof MarginLayoutParams; } ``` #### 3. 示例代码 以下是一个完整的自定义 `ViewGroup` 示例,实现了水平排列的子视图布局: ```java public class HorizontalViewGroup extends ViewGroup { public HorizontalViewGroup(Context context) { super(context); } public HorizontalViewGroup(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); int totalWidth = 0; int maxHeight = 0; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (child.getVisibility() != GONE) { measureChild(child, widthMeasureSpec, heightMeasureSpec); totalWidth += child.getMeasuredWidth(); maxHeight = Math.max(maxHeight, child.getMeasuredHeight()); } } setMeasuredDimension(resolveSize(totalWidth, widthMeasureSpec), resolveSize(maxHeight, heightMeasureSpec)); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int x = 0; int y = 0; int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View child = getChildAt(i); if (child.getVisibility() != GONE) { int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); child.layout(x, y, x + childWidth, y + childHeight); x += childWidth; } } } @Override protected LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); } @Override protected boolean checkLayoutParams(LayoutParams p) { return p instanceof MarginLayoutParams; } } ``` #### 4. 使用自定义 ViewGroup 在 XML 文件中声明自定义 `ViewGroup`,并添加子视图。例如: ```xml <com.example.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Item 1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Item 2" /> </com.example.CustomViewGroup> ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值