在自定义viewgroup(5):https://www./article/100639.htm的基础上,添加使用adapter来适配数据,这样更加的方便,这里只是使用adapter适配数据,不能更新。
package com.example.libingyuan.horizontallistview.ScrollViewGroup;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.Scroller;
/**
* 自定义ViewGroup(横向滚动)
*/
public class ScrollViewGroup extends ViewGroup {
//滚动计算辅助类
private Scroller mScroller;
//屏幕宽度
private int screenWidth;
//可以移动的最大距离
private int mMaxDistance;
//自定义手势监听类
private ScrollTouchLisener mTouchLisener;
//手势监听
private GestureDetector mDetector;
private BaseAdapter mAdapter;
/**
* 使用new关键字创建对象的时候调用
*/
public ScrollViewGroup(Context context) {
this(context, null);
}
/**
* 在XML文件中使用的时候调用
*/
public ScrollViewGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
/**
* 在xml文件中调用,并且使用了自定义属性的时候调用
*/
public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
/**
* 初始化方法
* 初始化滚动辅助类Scroller以及计算出屏幕宽度
*/
private void init(Context context) {
//初始化辅助类
mScroller = new Scroller(context);
//获取屏幕宽度
WindowManager manager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(outMetrics);
screenWidth = outMetrics.widthPixels;
//手势指示器初始化
mTouchLisener = new ScrollTouchLisener();
mDetector = new GestureDetector(context, mTouchLisener);
}
/**
* 滚动时需要重写的方法,用于控制滚动
*/
@Override
public void computeScroll() {
//判断滚动时候停止
if (mScroller.computeScrollOffset()) {
//滚动到指定的位置
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
//这句话必须写,否则不能实时刷新
invalidate();
}
}
/**
* 手指触屏事件监听
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
mDetector.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP) {
this.onUp(event);
}
return true;
}
public void setAdapter(BaseAdapter adapter) {
this.mAdapter = adapter;
requestLayout();
}
/*
*测量方法,测量父布局的宽度和高度
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
removeAllViews();
for (int i = 0; i < mAdapter.getCount(); i++) {
View child = mAdapter.getView(i, null, null);
addView(child);
}
//重新设置宽高
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);
// LayoutParams lp = child.getLayoutParams();
/* MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
width += childWidth;*/
width += child.getMeasuredWidth();
}
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 += child.getMeasuredHeight();
}
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;//子View左边的间距
int childWidth;//子View的宽度
int height = getHeight();//屏幕的宽度
int childCount = getChildCount();//子View的数量
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
/* MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;*/
childWidth = child.getMeasuredWidth();
child.layout(childLeft, 0, childLeft + childWidth, height);
childLayout(child);
childLeft += childWidth;
}
}
/**
*让子View的子View和子View大小一样
*/
private void childLayout(View child){
if (child==null){
throw new IllegalStateException("ScrollViewGroup must has one child");
}
if (child instanceof ViewGroup){
if(((ViewGroup)child).getChildCount()>0)
throw new IllegalStateException("view can host only one direct child");
((ViewGroup) child).getChildAt(0).layout(0,0,child.getWidth(),child.getHeight());
}
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
/*
*按下事件 ACTION_DOWN
*/
public boolean onDown(MotionEvent e) {
//得到最后一个子View
View lastChild = getChildAt(getChildCount() - 1);
//获取滑动的最大滑动距离(最后一个Child的右边框的坐标减去屏幕的宽度)
int finalyChild = (int) (lastChild.getX() + lastChild.getWidth() - screenWidth);
mMaxDistance = finalyChild;
//如果停止滚动则取消动画(即手指按下就停止滚动)
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
return false;
}
/*
*抬起事件 ACTION_UP
*/
public boolean onUp(MotionEvent e) {
//如果滑动的距离小于第一个控件的最左边(0)则回弹至(0,0)点
if (getScrollX() <= 0) {
scrollTo(0, 0);
}
//如果滑动的距离大于最大可滑动距离则滑动到最后一个子View
if (getScrollX() >= mMaxDistance) {
scrollTo(mMaxDistance, 0);
}
//刷新界面
invalidate();
return false;
}
/*
*ACTION_DOWN 、短按不移动
*/
public void onShowPress(MotionEvent e) {
}
/*
*短按ACTION_DOWN、ACTION_UP
*/
public boolean onSingleTapUp(MotionEvent e) {
return false;
}
/*
*ACTION_DOWN 、慢滑动
*/
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
//滚动
scrollBy((int) distanceX, 0);
return false;
}
// ACTION_DOWN 、长按不滑动
public void onLongPress(MotionEvent e) {
}
/*
*ACTION_DOWN 、快滑动、 ACTION_UP
*/
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
mScroller.fling(getScrollX(), 0, (int) -velocityX, 0, 0, mMaxDistance, 0, 0);
return false;
}
/**
* 自定义手势监听类
*/
private class ScrollTouchLisener implements GestureDetector.OnGestureListener {
//按下事件
@Override
public boolean onDown(MotionEvent e) {
return ScrollViewGroup.this.onDown(e);
}
//单击事件
@Override
public void onShowPress(MotionEvent e) {
ScrollViewGroup.this.onShowPress(e);
}
//手指抬起事件
@Override
public boolean onSingleTapUp(MotionEvent e) {
return ScrollViewGroup.this.onSingleTapUp(e);
}
//滚动事件
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return ScrollViewGroup.this.onScroll(e1, e2, distanceX, distanceY);
}
//长按事件
@Override
public void onLongPress(MotionEvent e) {
ScrollViewGroup.this.onLongPress(e);
}
//滑动事件
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return ScrollViewGroup.this.onFling(e1, e2, velocityX, velocityY);
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Android
# viewgroup
# adapter
# Kotlin编写Android适配器Adapter
# 浅析Android之Adapter用法总结
# Android ListView适配器(Adapter)优化方法详解
# Android设计模式之适配器(Adapter)模式
# Android Adapter的几个常用方法
# 简单好用的Adapter---ArrayAdapter详解
# 自定义
# 按下
# 第一个
# 按不
# 基础上
# 这句话
# 重写
# 大可
# 单击
# 大家多多
# 计算出
# 时需
# 测量方法
# 使用了
# super
# defStyleAttr
# xml
# void
# manager
# init
相关文章:
如何获取PHP WAP自助建站系统源码?
官网自助建站系统:SEO优化+多语言支持,快速搭建专业网站
建站之星Pro快速搭建教程:模板选择与功能配置指南
非常酷的网站设计制作软件,酷培ai教育官方网站?
常州自助建站:操作简便模板丰富,企业个人快速搭建网站
如何注册花生壳免费域名并搭建个人网站?
Dapper的Execute方法的返回值是什么意思 Dapper Execute返回值详解
成都网站制作报价公司,成都工业用气开户费用?
,想在网上投简历,哪几个网站比较好?
成都网站制作公司哪家好,四川省职工服务网是做什么用?
保定网站制作方案定制,保定招聘的渠道有哪些?找工作的人一般都去哪里看招聘信息?
如何在云虚拟主机上快速搭建个人网站?
动图在线制作网站有哪些,滑动动图图集怎么做?
家具网站制作软件,家具厂怎么跑业务?
建站之星代理平台如何选择最佳方案?
广东企业建站网站优化与SEO营销核心策略指南
济南网站制作的价格,历城一职专官方网站?
建站之星伪静态规则如何设置?
婚礼视频制作网站,学习*后期制作的网站有哪些?
头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?
建站之星如何助力企业快速打造五合一网站?
昆明高端网站制作公司,昆明公租房申请网上登录入口?
岳西云建站教程与模板下载_一站式快速建站系统操作指南
小程序网站制作需要准备什么资料,如何制作小程序?
微网站制作教程,不会写代码,不会编程,怎么样建自己的网站?
南平网站制作公司,2025年南平市事业单位报名时间?
网站制作需要会哪些技术,建立一个网站要花费多少?
如何通过山东自助建站平台快速注册域名?
建站之星如何保障用户数据免受黑客入侵?
如何快速配置高效服务器建站软件?
北京网页设计制作网站有哪些,继续教育自动播放怎么设置?
早安海报制作网站推荐大全,企业早安海报怎么每天更换?
如何用花生壳三步快速搭建专属网站?
如何快速生成可下载的建站源码工具?
建站之星如何实现PC+手机+微信网站五合一建站?
如何做静态网页,sublimetext3.0制作静态网页?
整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?
公司网站的制作公司,企业网站制作基本流程有哪些?
制作网站的网址是什么,请问后缀为.com和.com.cn还有.cn的这三种网站是分别是什么类型的网站?
北京企业网站设计制作公司,北京铁路集团官方网站?
云南网站制作公司有哪些,云南最好的招聘网站是哪个?
专业公司网站制作公司,用什么语言做企业网站比较好?
代刷网站制作软件,别人代刷火车票靠谱吗?
如何在宝塔面板创建新站点?
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
c# 在高并发场景下,委托和接口调用的性能对比
建站主机数据库如何配置才能提升网站性能?
中山网站推广排名,中山信息港登录入口?
如何在香港服务器上快速搭建免备案网站?
GML (Geography Markup Language)是什么,它如何用XML来表示地理空间信息?
*请认真填写需求信息,我们会在24小时内与您取得联系。