Android自定义ViewGroup实现可滚动的横向布局(2)
发布时间 - 2026-01-10 21:57:50 点击率:次上一篇文章自定义viewgroup(1)地址:https://www./article/100608.htm

这里直接代码:
package com.example.libingyuan.horizontallistview.ScrollViewGroup;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;
/**
* 自定义ViewGroup
* 在横向布局的基础上,增加啦滚动效果,但是没有边界限制
*/
public class ScrollViewGroup extends ViewGroup {
private Scroller mScroller;
private float mLastMotionX = 0;
public ScrollViewGroup(Context context) {
this(context, null);
}
public ScrollViewGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
mScroller = new Scroller(context);
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
float x = event.getX();
switch (action) {
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
mLastMotionX = event.getX();
break;
case MotionEvent.ACTION_MOVE:
float delt = mLastMotionX - x;
mLastMotionX = x;
scrollBy((int) delt, 0);
break;
case MotionEvent.ACTION_UP:
invalidate();
break;
default:
break;
}
return true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//重新设置宽高
this.setMeasuredDimension(measureWidth(widthMeasureSpec, heightMeasureSpec), measureHeight(widthMeasureSpec, heightMeasureSpec));
}
/**
* 测量宽度
*/
private int measureWidth(int widthMeasureSpec, int heightMeasureSpec) {
// 宽度
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
//父控件的宽(wrap_content)
int width = 0;
int childCount = getChildCount();
//重新测量子view的宽度,以及最大高度
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
width += childWidth;
}
return modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width;
}
/**
* 测量高度
*/
private int measureHeight(int widthMeasureSpec, int heightMeasureSpec) {
//高度
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
//父控件的高(wrap_content)
int height = 0;
int childCount = getChildCount();
//重新测量子view的宽度,以及最大高度
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
height += childHeight;
}
height = height / childCount;
return modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childLeft = 0;
int childWidth;
int height = getHeight();
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
child.layout(childLeft, 0, childLeft + childWidth, height);
childLeft += childWidth;
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Android
# ViewGroup
# 横向布局
# android开发之横向滚动/竖向滚动的ListView(固定列头)
# Android程序开发之ListView实现横向滚动(带表头与固定列)
# 详解Android使GridView横向水平滚动的实现方式
# Android使用GridView实现横向滚动效果
# Android开发实现横向列表GridView横向滚动的方法【附源码下载】
# Android GridView实现横向列表水平滚动
# Android实现自定义的弹幕效果
# 实例解析如何在Android应用中实现弹幕动画效果
# Android 实现仿网络直播弹幕功能详解及实例
# Android实现横向无限循环滚动的单行弹幕效果
# 自定义
# 基础上
# 上一
# 大家多多
# super
# defStyleAttr
# int
# Override
# void
# init
# attrs
# float
# mScroller
# private
# null
# context
# mLastMotionX
# computeScroll
# Auto
# TODO
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
如何在IIS7上新建站点并设置安全权限?
Laravel Facade的原理是什么_深入理解Laravel门面及其工作机制
Laravel如何配置Horizon来管理队列?(安装和使用)
浅析上传头像示例及其注意事项
Win11怎么查看显卡温度 Win11任务管理器查看GPU温度【技巧】
java获取注册ip实例
高端建站如何打造兼具美学与转化的品牌官网?
Laravel如何使用Blade模板引擎?(完整语法和示例)
Python正则表达式进阶教程_复杂匹配与分组替换解析
如何自己制作一个网站链接,如何制作一个企业网站,建设网站的基本步骤有哪些?
如何获取PHP WAP自助建站系统源码?
零基础网站服务器架设实战:轻量应用与域名解析配置指南
软银砸40亿美元收购DigitalBridge 强化AI资料中心布局
Java解压缩zip - 解压缩多个文件或文件夹实例
nodejs redis 发布订阅机制封装实现方法及实例代码
佛山企业网站制作公司有哪些,沟通100网上服务官网?
如何挑选优质建站一级代理提升网站排名?
Linux网络带宽限制_tc配置实践解析【教程】
Laravel软删除怎么实现_Laravel Eloquent SoftDeletes功能使用教程
如何在Tomcat中配置并部署网站项目?
如何在阿里云虚拟服务器快速搭建网站?
如何在云虚拟主机上快速搭建个人网站?
Laravel如何处理异常和错误?(Handler示例)
Laravel如何生成URL和重定向?(路由助手函数)
如何实现javascript表单验证_正则表达式有哪些实用技巧
网站图片在线制作软件,怎么在图片上做链接?
Laravel如何实现用户密码重置功能?(完整流程代码)
香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化
Claude怎样写约束型提示词_Claude约束提示词写法【教程】
Laravel全局作用域是什么_Laravel Eloquent Global Scopes应用指南
怎么用AI帮你为初创公司进行市场定位分析?
Laravel如何使用.env文件管理环境变量?(最佳实践)
活动邀请函制作网站有哪些,活动邀请函文案?
Win10如何卸载预装Edge扩展_Win10卸载Edge扩展教程【方法】
C语言设计一个闪闪的圣诞树
laravel怎么使用数据库工厂(Factory)生成带有关联模型的数据_laravel Factory生成关联数据方法
Laravel如何实现多级无限分类_Laravel递归模型关联与树状数据输出【方法】
宙斯浏览器视频悬浮窗怎么开启 边看视频边操作其他应用教程
iOS中将个别页面强制横屏其他页面竖屏
Laravel Docker环境搭建教程_Laravel Sail使用指南
免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?
如何快速完成中国万网建站详细流程?
Laravel如何处理文件上传_Laravel Storage门面实现文件存储与管理
实现点击下箭头变上箭头来回切换的两种方法【推荐】
Laravel如何连接多个数据库_Laravel多数据库连接配置与切换教程
CSS3怎么给轮播图加过渡动画_transition加transform实现【技巧】
高端网站建设与定制开发一站式解决方案 中企动力
敲碗10年!Mac系列传将迎来「触控与联网」双革新
如何在服务器上配置二级域名建站?
如何快速搭建高效简练网站?

