Android横向滚动自动居中的HorizontalScrollView

2024-05-10 05:58

本文主要是介绍Android横向滚动自动居中的HorizontalScrollView,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说实话百度没到合适的,就只能自己改改了。

开发目标:

1.支持横向滚动
2.滑动停止能够自动滚动到item中间的位置
3.点击item自动滚动到选中item的位置
4.第一个item和最后一个item要可以滚动到中间位置

Like this:

在这里插入图片描述

思路:

1.HorizontalScrollView改造一下
2.RecycleView也支持横向滚动(SnapHelper这个还玩不转改起来要吃力)
3.Viewpage同样做到横向切换(viewpager+fragment 觉得会麻烦些)

最后选用HorizontalScrollView

android:clipToPadding="false"这个属性意思是能否在padding区域内绘图
默认是true也就是设置padding后可视区域就变小了
如果设置成false则不会影响可视区域,更像是设置的padding没起作用

这是我的代码:

1.activity部分:

  private void initHorizontal() {AutoCenterHorizontalScrollView autoCenterHorizontalScrollView;autoCenterHorizontalScrollView = findViewById(R.id.achs_test);//测试用的随机字符串集合List<String> names =new ArrayList<>();for(int i=0;i<50;i++){String a = ""+i;for(int j=0;j<i%4;j++){a=a+"A";}names.add(a);}//adapter去处理itemViewHorizontalAdapter hadapter = new HorizontalAdapter(mContext,names);autoCenterHorizontalScrollView.setAdapter(hadapter);autoCenterHorizontalScrollView.setOnSelectChangeListener(new AutoCenterHorizontalScrollView.OnSelectChangeListener() {@Overridepublic void onSelectChange(int position) {((TextView) findViewById(R.id.tv_index)).setText("当前"+position);}});autoCenterHorizontalScrollView.setCurrentIndex(39);}

2.HorizontalAdapter(处理itemView)

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;import cn.demomaster.huan.quickdevelop.R;
import cn.demomaster.huan.quickdeveloplibrary.widget.AutoCenterHorizontalScrollView;/*** Created by Squirrel桓 on 2018/12/15.*/
public class HorizontalAdapter implements AutoCenterHorizontalScrollView.HAdapter {List<String> names = new ArrayList<>();private Context context;public HorizontalAdapter(Context context, List<String> names) {this.names = names;this.context = context;}@Overridepublic int getCount() {return names.size();}@Overridepublic RecyclerView.ViewHolder getItemView(int i) {View v = LayoutInflater.from(context).inflate(R.layout.item_warp2, null, false);HViewHolder hViewHolder = new HViewHolder(v);hViewHolder.textView.setBackgroundColor(Color.BLACK);hViewHolder.textView.setText(names.get(i));return hViewHolder;}@Overridepublic void onSelectStateChanged(RecyclerView.ViewHolder viewHolder, int position, boolean isSelected) {if (isSelected) {((HViewHolder) viewHolder).textView.setBackgroundColor(Color.RED);} else {((HViewHolder) viewHolder).textView.setBackgroundColor(Color.BLACK);}}public static class HViewHolder extends RecyclerView.ViewHolder {public final TextView textView;public HViewHolder(View itemView) {super(itemView);textView = (TextView) itemView.findViewById(R.id.tv_tab_name);}}
}

3.AutoCenterHorizontalScrollView


import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;import java.util.ArrayList;
import java.util.List;/*** @author squirrel桓* @date 2018/12/14.* description:Android横向滚动居中的HorizontalScrollView*/
public class AutoCenterHorizontalScrollView extends HorizontalScrollView {public AutoCenterHorizontalScrollView(Context context) {super(context);init();}public AutoCenterHorizontalScrollView(Context context, AttributeSet attrs) {super(context, attrs);init();}public AutoCenterHorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}@Overridepublic void addView(View child) {super.addView(child);init();}/*** itemView适配器很随意*/private HAdapter adapter;public static interface HAdapter {int getCount();//获取子view个数RecyclerView.ViewHolder getItemView(int position);//获取指定index的viewvoid onSelectStateChanged(RecyclerView.ViewHolder itemView,int position,boolean isSelected);//改变选中状态}List<RecyclerView.ViewHolder> viewHolders = new ArrayList<>();//自己组装itemViewpublic void setAdapter(final HAdapter adapter) {if(adapter==null||adapter.getCount()==0){return;}this.adapter = adapter;viewHolders.clear();removeAllViews();LinearLayout linearLayout = new LinearLayout(getContext());for (int i = 0; i < adapter.getCount(); i++) {viewHolders.add(adapter.getItemView(i));LinearLayout linearLayout1 = new LinearLayout(getContext());linearLayout1.addView(viewHolders.get(i).itemView);linearLayout1.setTag(i);linearLayout1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {int index = (int) view.getTag();if (adapter != null) {//处理上一个adapter.onSelectStateChanged(viewHolders.get(lastIndex) ,lastIndex,false);//触发选中事件回调//处理当前选中的adapter.onSelectStateChanged(viewHolders.get(index) ,index,true);//触发选中事件回调lastIndex = index;}smoothScrollTo(getChildCenterPosition(index), 0);//点击某个item滚动到指定位置}});linearLayout.addView(linearLayout1);}addView(linearLayout);init();}/*** 获取item的X位置** @param index* @return*/private int getChildCenterPosition(int index) {offset_current = super.computeHorizontalScrollOffset();if (getChildCount() <= 0) {return 0;}ViewGroup viewGroup = (ViewGroup) getChildAt(0);if (viewGroup == null || viewGroup.getChildCount() == 0) {return 0;}int offset_tmp = 0;for (int i = 0; i < viewGroup.getChildCount(); i++) {View child = viewGroup.getChildAt(i);int child_width = child.getWidth();offset_tmp = offset_tmp + child_width;if (i == index) {offset_target = offset_tmp - child_width / 2 - viewGroup.getChildAt(0).getWidth() / 2;setCurrent(i);return offset_target;}}return 0;}private int paddingLeft = 0;//左侧内边距private int paddingRight = 0;//右侧内边距private float touchDown_X;//判断是否是点击还是滑动来用void init() {//添加触摸事件,滑动事件会触发this.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View view, MotionEvent motionEvent) {if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {//按下事件记录x坐标touchDown_X = motionEvent.getX();}if (motionEvent.getAction() == MotionEvent.ACTION_UP||motionEvent.getAction() == MotionEvent.ACTION_CANCEL) {//抬起事件判断是否是滑动事件if (touchDown_X != motionEvent.getX()) {//抬起事件则,触发touchDown_X = motionEvent.getX();handler.removeCallbacks(scrollerTask);handler.postDelayed(scrollerTask, delayMillis);}}return false;}});if (getChildCount() <= 0) {return;}ViewGroup viewGroup = (ViewGroup) getChildAt(0);if (viewGroup == null || viewGroup.getChildCount() == 0) {return;}//一下代码是设置padding,实现第一个itemview和最后一个能够居中View first = viewGroup.getChildAt(0);int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);first.measure(w, h);int first_width = first.getMeasuredWidth();View last = viewGroup.getChildAt(viewGroup.getChildCount() - 1);last.measure(w, h);int last_width = last.getMeasuredWidth();paddingLeft = getScreenWidth(getContext()) / 2 - first_width / 2;paddingRight = getScreenWidth(getContext()) / 2 - last_width / 2;setPadding(paddingLeft, getPaddingTop(), paddingRight, getBottom());//设置默认位置setCurrentIndex(currentIndex);}/*** Runnable延迟执行的时间*/private long delayMillis = 100;/*** 上次滑动left,即x*/private long lastScrollLeft = -1;private long nowScrollLeft = -1;private Runnable scrollerTask = new Runnable() {@Overridepublic void run() {if ((nowScrollLeft == lastScrollLeft)) {lastScrollLeft = nowScrollLeft;nowScrollLeft = -1;int index = getCurrentIndex();if (offset_target != offset_current) {Log.d(tag, "offset_target=" + offset_target + ",offset_current=" + offset_current);smoothScrollTo(offset_target, 0);}if (adapter != null&&adapter.getCount()>0&&currentIndex<adapter.getCount()) {//处理上一个adapter.onSelectStateChanged(viewHolders.get(lastIndex) ,lastIndex,false);//触发选中事件回调//处理当前选中的adapter.onSelectStateChanged(viewHolders.get(index) ,index,true);//触发选中事件回调lastIndex = index;}} else {lastScrollLeft = nowScrollLeft;postDelayed(this, delayMillis);}}};/*** 用来判断滚动是否滑动*/private Handler handler = new Handler();String tag = "AutoCenter";@Overrideprotected void onScrollChanged(int l, int t, int oldl, int oldt) {super.onScrollChanged(l, t, oldl, oldt);Log.i(tag, "left=" + l);// 更新ScrollView的滑动位置nowScrollLeft = l;}@Overrideprotected int computeHorizontalScrollRange() {Log.i(tag, "横向总宽度 computeHorizontalScrollRange:" + super.computeHorizontalScrollRange());Log.i(tag, "computeHorizontalScrollRange2:" + (super.computeHorizontalScrollRange() + getScreenWidth(getContext())));return super.computeHorizontalScrollRange() + paddingLeft + paddingRight;}@Overrideprotected int computeHorizontalScrollOffset() {Log.i(tag, "当前位置 computeHorizontalScrollOffset:" + super.computeHorizontalScrollOffset());return super.computeHorizontalScrollOffset() + paddingLeft;}private int offset_target;//目标位置private int offset_current;//当前位置private int currentIndex = 0;//当前选中的item的positionprivate int lastIndex=0;//上一次选中的位置private void setCurrent(int currentIndex) {this.currentIndex = currentIndex;if (adapter != null&&adapter.getCount()>0&&currentIndex<adapter.getCount()) {//处理上一个adapter.onSelectStateChanged(viewHolders.get(lastIndex) ,lastIndex,false);//触发选中事件回调//处理当前选中的adapter.onSelectStateChanged(viewHolders.get(currentIndex) ,currentIndex,true);//触发选中事件回调lastIndex = currentIndex;if(onSelectChangeListener!=null){onSelectChangeListener.onSelectChange(currentIndex);}}}public void setCurrentIndex(int currentIndex) {setCurrent(currentIndex);if (getChildCount() <= 0) {return ;}ViewGroup viewGroup = (ViewGroup) getChildAt(0);if (viewGroup == null || viewGroup.getChildCount() == 0) {return ;}int offset_tmp = 0;for (int i = 0; i < viewGroup.getChildCount(); i++) {View child = viewGroup.getChildAt(i);int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);child.measure(w, h);int child_width = child.getMeasuredWidth();offset_tmp = offset_tmp + child_width;if (i ==currentIndex) {View child0 = viewGroup.getChildAt(0);child0.measure(w, h);int child_width0 = child0.getMeasuredWidth();offset_target = offset_tmp - child_width / 2 - child_width0 / 2;this.post(new Runnable() {@Overridepublic void run() {smoothScrollTo(offset_target, 0);}});return;}}}//获取当前选中的item的positionpublic int getCurrentIndex() {offset_current = super.computeHorizontalScrollOffset();if (getChildCount() <= 0) {return 0;}ViewGroup viewGroup = (ViewGroup) getChildAt(0);if (viewGroup == null || viewGroup.getChildCount() == 0) {return 0;}int offset_tmp = 0;for (int i = 0; i < viewGroup.getChildCount(); i++) {View child = viewGroup.getChildAt(i);int child_width = child.getWidth();offset_tmp = offset_tmp + child_width;if (offset_tmp > offset_current) {offset_target = offset_tmp - child_width / 2 - viewGroup.getChildAt(0).getWidth() / 2;setCurrent(i);break;}}return currentIndex;}/*** 选中改变时触发回调*/public OnSelectChangeListener onSelectChangeListener;public void setOnSelectChangeListener(OnSelectChangeListener onSelectChangeListener) {this.onSelectChangeListener = onSelectChangeListener;setCurrent(currentIndex);}public static interface OnSelectChangeListener {void onSelectChange( int position);}/*** 获取屏幕宽度** @return*/public static int getScreenWidth(Context context) {return getDisplayMetrics(context).widthPixels;}/*** 获取 DisplayMetrics** @return*/public static DisplayMetrics getDisplayMetrics(Context context) {return context.getResources().getDisplayMetrics();}}

4.R.layout.activity_center_horizental

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".sample.CenterHorizontalActivity"><cn.demomaster.huan.quickdeveloplibrary.widget.AutoCenterHorizontalScrollViewandroid:id="@+id/achs_test"android:layout_width="match_parent"android:layout_height="wrap_content"android:clipToPadding="false"></cn.demomaster.huan.quickdeveloplibrary.widget.AutoCenterHorizontalScrollView><TextViewandroid:id="@+id/tv_index"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:textSize="40dp"android:textColor="@color/white"android:background="@color/gray"android:text="ViewPager"/></LinearLayout>

5.R.layout.item_warp2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="wrap_content"android:orientation="vertical"android:layout_height="wrap_content"tools:ignore="MissingDefaultResource"><TextViewandroid:id="@+id/tv_tab_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:padding="5dp"android:layout_margin="5dp"android:textColor="@color/white"android:background="@color/black"android:gravity="center"/>
</LinearLayout>
说说还遗留的问题吧,

1.滚动结束事件感觉不理的不好,百度到的开启任务定时判断当前位置是否等于上次的位置来确定是否结束。
2.用下面这种方法计算view宽度时会有不准确的时候,不懂原理。

View first = viewGroup.getChildAt(0);int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);first.measure(w, h);int first_width = first.getMeasuredWidth();

这篇关于Android横向滚动自动居中的HorizontalScrollView的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/975625

相关文章

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

基于51单片机的自动转向修复系统的设计与实现

文章目录 前言资料获取设计介绍功能介绍设计清单具体实现截图参考文献设计获取 前言 💗博主介绍:✌全网粉丝10W+,CSDN特邀作者、博客专家、CSDN新星计划导师,一名热衷于单片机技术探索与分享的博主、专注于 精通51/STM32/MSP430/AVR等单片机设计 主要对象是咱们电子相关专业的大学生,希望您们都共创辉煌!✌💗 👇🏻 精彩专栏 推荐订阅👇🏻 单片机

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Python3 BeautifulSoup爬虫 POJ自动提交

POJ 提交代码采用Base64加密方式 import http.cookiejarimport loggingimport urllib.parseimport urllib.requestimport base64from bs4 import BeautifulSoupfrom submitcode import SubmitCodeclass SubmitPoj():de

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

android应用中res目录说明

Android应用的res目录是一个特殊的项目,该项目里存放了Android应用所用的全部资源,包括图片、字符串、颜色、尺寸、样式等,类似于web开发中的public目录,js、css、image、style。。。。 Android按照约定,将不同的资源放在不同的文件夹中,这样可以方便的让AAPT(即Android Asset Packaging Tool , 在SDK的build-tools目

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR