Android自定义ViewGroup实现可滚动的横向布局(2)
作者:封魔之殇 发布时间:2022-10-08 17:25:27
标签:Android,ViewGroup,横向布局
上一篇文章自定义viewgroup(1)地址:https://www.jb51.net/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);
}
}


猜你喜欢
- 简介功能需求如图所示,点击下一个按钮,所有卡片向右滚动,其中最后一张需要变更为最前面的一张,点击上一个按钮,所有卡片向左滚动,最前面的一张需
- 需求是要做几个小游戏的抽奖功能,需要根据不同的游戏有不同的抽奖规则,其中也有很多共性,可归纳为只按奖品占比抽取、奖品占比与奖品数量抽取、分段
- 高分配速率(High Allocation Rate)分配速率(Allocation rate)表示单位时间内分配的内存量。通常使用&nbs
- Android程序调用本机googlemap,传递起始和终点位置,生成路线图if (wodeweizhiPoint != null) { i
- 目标:list中有0到39共40个元素,删除其中索引是10、20、30的元素方案一:使用普通for循环从前往后遍历再删除//初始化List列
- 本文实例讲述了C#将布尔类型转换成字节数组的方法。分享给大家供大家参考。具体如下:byte[] b = null;b = BitConver
- 本文为大家分享了Android操作蓝牙2.0的使用方法,供大家参考,具体内容如下1.Android操作蓝牙2.0的使用流程(1)找到设备uu
- c#控件实现类似c++中ocx控件功能c++中ocx控件1、控件方法2、控件事件c#很容易实现c++中ocx中控件方法的功能,但是实现类似c
- 首先使用PImage来实例化对象,再通过loadImage赋值,两层for循环遍历图片上的像素点,每隔5个像素点,画一个直径为3的圆。颜色通
- 返回json格式数据时间格式配置数据库里面查出来的时间是时间错格式,前段需要处理才能展示相应的格式,自己一个个转的话太麻烦,所以可以在apl
- 用户可以自定义打印某一年的年历,即:把某一年的日历全部打印出来如把2013年的年历打印出来如下:January 2013&nbs
- 在java的JFrame内通过创建匿名对象的方式做登录界面package com.sxt;import java.awt.Container
- 本文实例为大家分享了WPF实现半圆形导航菜单的具体代码,供大家参考,具体内容如下实现效果如下:思路:扇形自定义控件组合成半圆型菜单,再通过c
- 前文本章是关于Java流程控制语句的最全汇总,本篇为汇总上篇。流程是人们生活中不可或缺的一部分,它表示人们每天都在按照一定的流程做事。比如出
- 本文实例讲述了C#清除WebBrowser中Cookie缓存的方法。分享给大家供大家参考,具体如下:最近用C#写一个程序,用一个窗体中的We
- JDK * 的过程JDK * 采用字节重组,重新生成对象来替代原始对象,以达到 * 的目的。JDK中有一个规范,在ClassPath下
- 本文实例为大家分享了android通过servlet上传文件到服务器的具体代码,供大家参考,具体内容如下服务器端:部署在Tomcat上,直接
- Android总体有五大布局:线性布局(LiearLayout): 屏幕垂直或水平方向布局。帧布局(FrameLayout):控件从屏幕左上
- 中午没事,把去年刚毕业那会画的几张图翻出来了,大概介绍Winform应用程序运行的过程,以及TCP协议在Winform中的应用。如果有Win
- 目录:1.list中添加,获取,删除元素;2.list中是否包含某个元素;3.list中根据索引将元素数值改变(替换);4.list中查看(