自定义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();
        }
    }
}


内容概要:文章以“智能网页数据标注工具”为例,深入探讨了谷歌浏览器扩展在毕业设计中的实战应用。通过开发具备实体识别、情感分类等功能的浏览器扩展,学生能够融合前端开发、自然语言处理(NLP)、本地存储与模型推理等技术,实现高效的网页数据标注系统。文中详细解析了扩展的技术架构,涵盖Manifest V3配置、内容脚本与Service Worker协作、TensorFlow.js模型在浏览器端的轻量化部署与推理流程,并提供了核心代码实现,包括文本选择、标注工具栏动态生成、高亮显示及模型预测功能。同时展望了多模态标注、主动学习与边缘计算协同等未来发展方向。; 适合人群:具备前端开发基础、熟悉JavaScript和浏览器机制,有一定AI模型应用经验的计算机相关专业本科生或研究生,尤其适合将浏览器扩展与人工智能结合进行毕业设计的学生。; 使用场景及目标:①掌握浏览器扩展开发全流程,理解内容脚本、Service Worker与弹出页的通信机制;②实现在浏览器端运行轻量级AI模型(如NER、情感分析)的技术方案;③构建可用于真实场景的数据标注工具,提升标注效率并探索主动学习、协同标注等智能化功能。; 阅读建议:建议结合代码实例搭建开发环境,逐步实现标注功能并集成本地模型推理。重点关注模型轻量化、内存管理与DOM操作的稳定性,在实践中理解浏览器扩展的安全机制与性能优化策略。
基于Gin+GORM+Casbin+Vue.js的权限管理系统是一个采用前后端分离架构的企业级权限管理解决方案,专为软件工程和计算机科学专业的毕业设计项目开发。该系统基于Go语言构建后端服务,结合Vue.js前端框架,实现了完整的权限控制和管理功能,适用于各类需要精细化权限管理的应用场景。 系统后端采用Gin作为Web框架,提供高性能的HTTP服务;使用GORM作为ORM框架,简化数据库操作;集成Casbin实现灵活的权限控制模型。前端基于vue-element-admin模板开发,提供现代化的用户界面和交互体验。系统采用分层架构和模块化设计,确保代码的可维护性和可扩展性。 主要功能包括用户管理、角色管理、权限管理、菜单管理、操作日志等核心模块。用户管理模块支持用户信息的增删改查和状态管理;角色管理模块允许定义不同角色并分配相应权限;权限管理模块基于Casbin实现细粒度的访问控制;菜单管理模块动态生成前端导航菜单;操作日志模块记录系统关键操作,便于审计和追踪。 技术栈方面,后端使用Go语言开发,结合Gin、GORM、Casbin等成熟框架;前端使用Vue.js、Element UI等现代前端技术;数据库支持MySQL、PostgreSQL等主流关系型数据库;采用RESTful API设计规范,确保前后端通信的标准化。系统还应用了单例模式、工厂模式、依赖注入等设计模式,提升代码质量和可测试性。 该权限管理系统适用于企业管理系统、内部办公平台、多租户SaaS应用等需要复杂权限控制的场景。作为毕业设计项目,它提供了完整的源码和论文文档,帮助学生深入理解前后端分离架构、权限控制原理、现代Web开发技术等关键知识点。系统设计规范,代码结构清晰,注释完整,非常适合作为计算机相关专业的毕业设计参考或实际项目开发的基础框架。 资源包含完整的系统源码、数据库设计文档、部署说明和毕
### 创建或使用自定义 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> ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值