Android自定义ViewGroup实现可滚动的横向布局(2)
作者:封魔之殇 时间:2022-10-08 17:25:27
上一篇文章自定义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);
}
}
标签:Android,ViewGroup,横向布局
0
投稿
猜你喜欢
java实现死锁的示例代码
2023-10-12 18:12:40
Android RecyclerView实现悬浮吸顶、分隔线、到底提示效果
2022-02-26 16:59:39
实例分析java中重载与重写的区别
2021-09-11 01:49:38
Mybatis全局配置及映射关系的实现
2022-07-03 09:31:39
Android项目中gradle的执行流程
2022-04-08 19:08:15
C#实现控制线程池最大数并发线程
2023-02-23 14:50:43
Android自定义弹框Dialog效果
2022-09-21 02:40:41
JMeter中的后端监听器的实现
2022-07-24 17:58:35
C#基础:基于const与readonly的深入研究
2023-09-26 14:22:30
Android中让按钮拥有返回键功能的方法及重写返回键功能
2021-08-08 10:33:56
C#实现将网页保存成图片的网页拍照功能
2021-06-22 14:36:01
C#数据类型转换(显式转型、隐式转型、强制转型)
2021-11-24 13:44:25
C#实现FTP上传文件的方法
2021-11-13 10:46:56
解决FeignClient发送post请求异常的问题
2022-08-08 15:55:58
java使用淘宝API读写json实现手机归属地查询功能代码
2021-05-31 03:27:38
Android手势密码view学习笔记(二)
2023-08-14 16:06:35
MybatisPlus使用代码生成器遇到的小问题(推荐)
2021-07-17 09:15:17
Windows下使用Graalvm将Springboot应用编译成exe大大提高启动和运行效率(推荐)
2023-09-27 02:05:02
C#实现的三种模拟自动登录和提交POST信息的方法
2022-05-28 01:49:32
java使用正则表达校验手机号码示例(手机号码正则)
2022-04-07 20:37:04