自定义HorizontalScrollView嵌套HorizontalListView实现手势监听、按钮监听横向滚动功能

article/2025/9/25 2:37:28

    今日空闲花了点时间对以前自主实现的项目功能进行改进和优化, 其实一些界面的小功能有时候没实现过,也没经验类似项目功能经验,反而耗费的时间会更多。下面我所描述的界面功能就是我在对用RecyclerView控件不熟悉的情况下使用了HorizontalScrollView来实现的,在我把它优化成RecyclerView控件之前在此做个笔录,方便以后查阅。

       按照以往的惯例,先上效果图(既可以手势监听,也可以按钮监听)

1.HorizontalScrollView滑动到最左边的时候,左边箭头光标变灰色

2.HorizontalScrollView滑动到不是最左边也不是最右边的时候,左右两边箭头光标都是较深的灰色

3.HorizontalScrollView滑动到最右边的时候,右边箭头光标变灰色

       首先要明确实现的界面功能是:自定义HorizontalScrollView嵌套HorizontalListView实现手势监听、按钮监听滑动。

       此处给大家一个自定义HorizontalListView开源库的链接地址:http://pan.baidu.com/s/1dENbGIH(开源库也是我当初从谷歌下载的,那就借此篇文章感谢一下这位开源库的作者,当初是他这个开源库封装得比较好帮助我实现了这个项目所需功能)

       有了HorizontalListView开源库,接下去一切事情都好办了,首先需要一个自定义HorizontalScrollView,附上代码:

       (参考来自:http://www.cnblogs.com/java-koma/archive/2012/11/02/2751594.html)

public class MyHorizontalScrollView extends HorizontalScrollView
{
private Runnable scrollerTask;
private int intitPosition;
private int newCheck = 100;
private int childWidth = 0;public interface OnScrollStopListner
{
/*** scroll have stoped*/
void onScrollStoped();/*** scroll have stoped, and is at left edge*/
void onScrollToLeftEdge();/*** scroll have stoped, and is at right edge*/
void onScrollToRightEdge();/*** scroll have stoped, and is at middle*/
void onScrollToMiddle();
}private OnScrollStopListner onScrollstopListner;public MyHorizontalScrollView(Context context, AttributeSet attrs)
{
super(context, attrs);
scrollerTask = new Runnable()
{
@Override
public void run()
{
int newPosition = getScrollX();
if (intitPosition - newPosition == 0)
{
if (onScrollstopListner == null)
{
return;
}
onScrollstopListner.onScrollStoped();
Rect outRect = new Rect();
getDrawingRect(outRect);
if (getScrollX() == 0)
{
onScrollstopListner.onScrollToLeftEdge();
} else if (childWidth + getPaddingLeft() + getPaddingRight() == outRect.right)
{
onScrollstopListner.onScrollToRightEdge();
} else
{
onScrollstopListner.onScrollToMiddle();
}
} else
{
intitPosition = getScrollX();
postDelayed(scrollerTask, newCheck);
}
}
};
}public void setOnScrollStopListner(OnScrollStopListner listner)
{
onScrollstopListner = listner;
}public void startScrollerTask()
{
intitPosition = getScrollX();
postDelayed(scrollerTask, newCheck);
checkTotalWidth();
}private void checkTotalWidth()
{
if (childWidth > 0)
{
return;
}
for (int i = 0; i < getChildCount(); i++)
{
childWidth += getChildAt(i).getWidth();
}
}}

接下去写xml布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/color_fff_white" ><LinearLayoutandroid:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:background="@color/color_f9"android:gravity="center_vertical" ><RelativeLayoutandroid:id="@+id/rl_news_back"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center" ><ImageViewandroid:id="@+id/img_news_back"android:layout_width="30dp"android:layout_height="wrap_content"android:visibility="gone"android:src="@drawable/back"/><ImageViewandroid:id="@+id/img_news_back_pre"android:layout_width="30dp"android:layout_height="wrap_content"android:src="@drawable/left_gray" /></RelativeLayout><com.example.custom.MyHorizontalScrollViewandroid:id="@+id/horizontalScrollView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="0.50"android:scrollbars="none" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="75dp"android:orientation="horizontal" ><com.meetme.android.horizontallistview.HorizontalListViewandroid:id="@+id/head_horizontalView"android:layout_width="880dp"android:layout_height="75dp"></com.meetme.android.horizontallistview.HorizontalListView></LinearLayout></com.example.custom.MyHorizontalScrollView><RelativeLayoutandroid:id="@+id/rl_news_enter"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center" ><ImageViewandroid:id="@+id/img_news_enter"android:layout_width="30dp"android:layout_height="wrap_content"android:src="@drawable/goenter" /><ImageViewandroid:id="@+id/img_news_enter_pre"android:layout_width="30dp"android:visibility="gone"android:layout_height="wrap_content"android:src="@drawable/right_gray" /></RelativeLayout></LinearLayout></RelativeLayout>

好了,一切都准备就绪以后,接下去就是MainActivity.java的主要代码了,也是比较简单的

1.适配器代码

//TODO  水平ListViewAdapter
class HorizontalListViewAdapter extends BaseAdapter{@Override
public int getCount() {
return mHorizontalData.size();
}@Override
public Object getItem(int position) {
return position;
}@Override
public long getItemId(int position) {
return position;
}@Override
public View getView(int position, View convertView, ViewGroup parent) {
View layout = null;
ViewHolder vh = null;
if (convertView == null)
{
vh = new ViewHolder();
layout = getLayoutInflater().inflate(R.layout.list_item_horizontallistview, null);
vh.HorizontalLv_im=(ImageView)layout.findViewById(R.id.financail_item_imageView);
vh.HorizontalLv_title=(TextView)layout.findViewById(R.id.financail_item_textView);
layout.setTag(vh);
} else
{
layout = convertView;// convertView 不为空 复用消失的行数
vh = (ViewHolder) layout.getTag();// 得到标签 口袋
}HomeData homeData = mHorizontalData.get(position);
vh.HorizontalLv_title.setText(homeData.getTitle());
vh.HorizontalLv_im.setImageResource(homeData.getImg());return layout;
}
}
//TODO 创建一个查找的类 目的是减少查找次数 无需每次都重复查找<span style="white-space:pre"></span>public static class ViewHolder<span style="white-space:pre"></span>{<span style="white-space:pre"></span>public ImageView HorizontalLv_im;<span style="white-space:pre"></span>public TextView HorizontalLv_title;<span style="white-space:pre"></span>}

其中 HomeData是自定义的一个listview数据优化类,大家可以根据listview的item需求来确定这个类的内容即可,非常简单,此处我就不贴代码了

onCreate的代码:

hlv = (HorizontalListView) findViewById(R.id.head_horizontalView);horizontalScrollView = (MyHorizontalScrollView) findViewById(R.id.horizontalScrollView1);
rl_news_back = (RelativeLayout) head_financial_news_headtwo.findViewById(R.id.rl_news_back);
rl_news_enter = (RelativeLayout) head_financial_news_headtwo.findViewById(R.id.rl_news_enter);
img_news_back = (ImageView) head_financial_news_headtwo.findViewById(R.id.img_news_back);
img_news_back_pre = (ImageView) head_financial_news_headtwo.findViewById(R.id.img_news_back_pre);
img_news_enter = (ImageView) head_financial_news_headtwo.findViewById(R.id.img_news_enter);
img_news_enter_pre = (ImageView) head_financial_news_headtwo.findViewById(R.id.img_news_enter_pre);hlva = new HorizontalListViewAdapter();
hlva.notifyDataSetChanged();
hlv.setAdapter(hlva);
<pre name="code" class="html">                clickHorizontalListview()

 
<pre name="code" class="html">private void clickHorizontalListview()
{
hlv.setOnItemClickListener(new OnItemClickListener()
{@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
tv_tilte.setText(mHorizontalData.get(position).getTitle());
select_item1 = position; //当前选择的节目item
hlva.notifyDataSetChanged();
}
});horizontalScrollView.setOnTouchListener(new OnTouchListener() {      @Override  public boolean onTouch(View v, MotionEvent event) {   //如果触动屏幕就执行   if (event.getAction() == MotionEvent.ACTION_UP){horizontalScrollView.startScrollerTask();}return false;}   });  horizontalScrollView.setOnScrollStopListner(new OnScrollStopListner(){public void onScrollToRightEdge(){img_news_back.setVisibility(View.VISIBLE);img_news_back_pre.setVisibility(View.GONE);img_news_enter.setVisibility(View.GONE);img_news_enter_pre.setVisibility(View.VISIBLE);}public void onScrollToMiddle(){img_news_back.setVisibility(View.VISIBLE);img_news_back_pre.setVisibility(View.GONE);img_news_enter.setVisibility(View.VISIBLE);img_news_enter_pre.setVisibility(View.GONE);}public void onScrollToLeftEdge(){img_news_back.setVisibility(View.GONE);img_news_back_pre.setVisibility(View.VISIBLE);img_news_enter.setVisibility(View.VISIBLE);img_news_enter_pre.setVisibility(View.GONE);}public void onScrollStoped(){}});rl_news_back.setOnClickListener(new OnClickListener()
{@Override
public void onClick(View v)
{
horizontalScrollView.smoothScrollBy(-( getWindowManager().getDefaultDisplay().getWidth()-rl_news_back.getWidth()*2),0);
horizontalScrollView.startScrollerTask();
}
});rl_news_enter.setOnClickListener(new OnClickListener()
{@Override
public void onClick(View v)
{
horizontalScrollView.startScrollerTask();
horizontalScrollView.smoothScrollBy(getWindowManager().getDefaultDisplay().getWidth()-rl_news_back.getWidth()*2,0);
}
});
}

http://chatgpt.dhexx.cn/article/pUASEmnC.shtml

相关文章

ScrollView/HorizontalScrollView常用技巧,附源码

ScrollView是ViewGroup的派生类&#xff0c;ViewGroup是View的派生类。 屏幕大小总是有限制的&#xff0c;对移动设备来说更是如此。当有很多内容需要显示的时候&#xff0c;一屏显示不完时&#xff0c;就需要使用滚动的方式。 ScrollView只能包含一个直接子vi…

Android 自定义 HorizontalScrollView 横向滑动效果

自从Gallery被谷歌废弃以后&#xff0c;Google推荐使用ViewPager和HorizontalScrollView来实现Gallery的效果。的确HorizontalScrollView可以实现Gallery的效果&#xff0c;但是HorizontalScrollView存在一个很大的问题&#xff0c;如果你仅是用来展示少量的图片&#xff0c;应…

Android ScrollView、NestedScrollView、Horizo​​ntalScrollView 等

在这篇文章中&#xff0c;我们想看看几个滚动视图的变体或子类以及它们是如何使用的。以下是我们迄今为止涵盖的变体&#xff1a; ScrollView - 超类 NestedScrollView - 子类 Horizo​​ntalScrollView - 子类 StickyScrollView - 子类和第三方库。 ParallaxScrollView - 子类…

Android横向滑动加载更多的控件的实现---HorizontalScrollSlideView

Android横向滑动加载更多的控件的实现—HorizontalScrollSlideView 需求 之前公司业务要求做一个横向滑动的&#xff0c;可以加载更多的控件&#xff0c;第一时间想到的就是 RecyclerView 来实现 &#xff0c;后面仔细想想滑动拦截不好控制等等 所以就换了个思路来实现了。思…

Android HorizontalScrollView 水平滑动 在listview上面动态添加图片

Android HorizontalScrollView 水平滑动 listview 上动态添加图片 最近遇到了个 在listview展示广告的需要动态添加图片 如图&#xff1a; 使用了 horizontalScrollView 在listview上进行添加 java代码&#xff1a; package com.baozi.bzhorizontalscrollview;impor…

HorizontalScrollView实现Gallery

从简便的方式&#xff0c;继承LinearLayout再布局了添加视图&#xff0c;使其左右滚动。 主界面Activity: package com.xmz.activity;import java.util.HashMap;import java.util.Map;import android.app.Activity;import android.os.Bundle;public class MainActivity extend…

Android中HorizontalScrollView的使用总结

HorizontalScrollView是Google推出的用来滚动查看视图的控件&#xff0c;已经替代了Gallery。 由于HorizontalScrollView继承自FrameLayout&#xff0c;这意味着你只能在它下面放置一个子控件&#xff0c;即在控件内部只能放一个字控件&#xff08;一般使用LinearLayout&#…

Android控件——HorizontalScrollView使用(一)

1. HorizontalScrollView简单使用 Gallery&#xff08;画廊&#xff09;是一个锁定中心条目并且拥有水平滚动列表的视图&#xff0c;一般用来浏览图片&#xff0c;并且可以响应事件显示信息&#xff1b;Gallery还可以和ImageSwitcher组件结合使用来实现一个通过缩略图来浏览图…

android HorizontalScrollView讲解

前言 本章内容是android.widget.HorizontalScrollView&#xff0c;译为"横向滚动条"&#xff0c;版本为Android 2.3 r1&#xff0c;翻译来自"Tina"&#xff0c;感谢"Tina"为大家带来精彩的翻译稿 &#xff01;期待你加入Android API 中文的翻译&…

Android中HorizontalScrollView的使用

由于移动设备物理显示空间一般有限&#xff0c;不可能一次性的把所有要显示的内容都显示在屏幕上。所以各大平台一般会提供一些可滚动的视图来向用户展示数据。Android平台框架中为我们提供了诸如ListView、GirdView、ScrollView等滚动视图控件&#xff0c;这几个视图控件也是我…

Android 自定义 HorizontalScrollView 打造再多图片(控件)也不怕 OOM 的横向滑动效果

转载请标明出处&#xff1a;http://blog.csdn.net/lmj623565791/article/details/38140505 自从Gallery被谷歌废弃以后&#xff0c;Google推荐使用ViewPager和HorizontalScrollView来实现Gallery的效果。的确HorizontalScrollView可以实现Gallery的效果&#xff0c;但是Horizo…

HorizontalScrollView入门技术

HorizontalScrollView是一个滚动视图,可以帮助我们实现菜单栏之类的方法,实现左滑动右滑动. 常用于做一些APP的导航条,那么我们如何进行实现呢? 首先肯定是声明布局,做一个示范: (这是在一个相对布局中做的 上面是我们的HorizontalScrollView,下面是一个可以滑动的ViewPager…

横向滑动视图HorizontalScrollView精炼详解

一、前期基础知识储备 由于移动设备物理显示空间一般有限&#xff0c;不可能一次性的把所有要显示的内容都显示在屏幕上。所以各大平台一般会提供一些可滚动的视图来向用户展示数据。Android平台框架中为我们提供了诸如ListView、GirdView、ScrollView、RecyclerView等滚动视图…

HorizontalScrollView 详解

2019独角兽企业重金招聘Python工程师标准>>> gallrey由于浪费内存问题被和谐了&#xff0c;现在一般都使用这个代替了或第三方库 类概述 用 于布局的容器&#xff0c;可以放置让用户使用滚动条查看的视图层次结构&#xff0c;允许视图结构比手机的屏幕大。Horizonta…

【Android控件】HorizontalScrollView的基础使用记录(滚动条自定义)

目录​​​​​​​ 效果图 简介 注意事项 基础属性 滚动条全部设置 滚动条是否总显示 自定义滚动条滑动背景和滚动条背景 设置滚动条的宽度 设置滚动条距离 其它常规设置 设置滚动速度 布局代码示例 总结 效果图 简介 HorizontalScrollView是水平滚动标签。垂直…

MySQL读写分离配置

介绍 MySQL主从复制是一个异步的复制过程&#xff0c;底层是基于Mysql数据库自带的二进制日志功能。就是一台或多台MySQL数据库(slave&#xff0c;即从库)从另 一台MySQL数据库(master&#xff0c;即主库)进行日志的复制然后再解析日志并应用到自身&#xff0c;最终实现从库的…

MySQL 读写分离配置实践

文章目录 一、环境准备1. 查看主从复制状态2. 查看JDK版本3. 打开root的远程连接权限4. 安装MyCat 二、配置文件1. server.xml2. schema.xml 三、启动服务1. 配置文件问题一2. 配置文件问题二 四、MyCat 9066端口和8066端口1. 9066管理端口2. 8066数据端口 五、验证读写分离1. …

mysql读写分离中间件有哪些

mysql中间件有哪些 mysql-proxy是官方提供的mysql中间件产品可以实现负载平衡&#xff0c;读写分离&#xff0c;failover等&#xff0c;但其不支持大数据量的分库分表且性能较差。下面介绍几款能代替其的mysql开源中间件产品&#xff0c;Atlas&#xff0c;cobar&#xff0c;tdd…

配置mysql读写分离

准备起码三台服务器我这里准备了 192.168.0.63 mycat 192.168.0.64 主 192.168.0.65 从 如果是在多台 Linux 系统中组建的 MyCAT 集群&#xff0c;那需要在 MyCAT Server 所在的服务器上配置对 其他 IP 和主机名的映射&#xff0c;配置方式如下&#xff1a; vi /etc/h…

MySQL读写分离原理

文章目录 一、读写分离的概念二、引入中间件MyCat三、MyCat服务端口和管理端口 一、读写分离的概念 读写分离是基于主从复制来实现的。在实际的应用环境中&#xff0c;肯定是读操作多&#xff0c;就像我们在电商平台上去购买东西&#xff0c;可能看了100个也就买了一两个。所以…