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

news/2024/7/3 0:42:39

    今日空闲花了点时间对以前自主实现的项目功能进行改进和优化, 其实一些界面的小功能有时候没实现过,也没经验类似项目功能经验,反而耗费的时间会更多。下面我所描述的界面功能就是我在对用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" >

    <LinearLayout
        android: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" >

        <RelativeLayout
            android:id="@+id/rl_news_back"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center" >

            <ImageView
                android:id="@+id/img_news_back"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:src="@drawable/back"/>
            
            <ImageView
                android:id="@+id/img_news_back_pre"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:src="@drawable/left_gray" />
            
        </RelativeLayout>

        <com.example.custom.MyHorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.50"
            android:scrollbars="none" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="75dp"
                android:orientation="horizontal" >

                <com.meetme.android.horizontallistview.HorizontalListView
                    android:id="@+id/head_horizontalView"
                    android:layout_width="880dp"
                    android:layout_height="75dp">
                </com.meetme.android.horizontallistview.HorizontalListView>
            </LinearLayout>
        </com.example.custom.MyHorizontalScrollView>

        <RelativeLayout
            android:id="@+id/rl_news_enter"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center" >

            <ImageView
                android:id="@+id/img_news_enter"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:src="@drawable/goenter" />
            
            <ImageView
                android: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://lihuaxi.xjx100.cn/news/51972.html

相关文章

A_A03_005 STM32程序DAPLINK下载

目录 一、资料下载 二、相关链接 三、交流学习 四、常用单片机系统板 五、DAPLINK下载器 六、STM32程序DAPLINK下载 流程 七、注意事项 一、资料下载 网盘链接 戳它跳转 提取码&#xff1a;oqnj 二、相关链接 DAPLINK驱动安装 WIN10系统驱动免安装 MDK5下载与安装…

Java面试问题整理笔记-JVM基础知识

JVM1.内存区域1.1程序计数器(线程私有)1.2虚拟机栈(线程私有)1.3本地方法区(线程私有)1.4堆&#xff08;Heap-线程共享&#xff09; -运行时数据区1.4.1 逃逸机制1.5方法区/永久代&#xff08;线程共享&#xff09;2.类加载2.1加载2.2验证2.3准备2.4解析2.5初始化2.6类加载机制…

DID系列3--DID 生态系统

资料来源&#xff1a;极易被忽视的DID&#xff0c;是通往Web3的护照 - 碳链价值来自W3C的DID规范是被广泛接受的标准&#xff0c;确保身份系统可以在不同的网络和平台上互操作。下面是DID架构的概述。DID是互联网上的一个地址&#xff0c;某人可以直接拥有和控制。它可以用来寻…

Map集合

一、概述 1、Map集合是一种双列集合&#xff0c;每个元素包含两个数据 2、Map集合的每个元素格式&#xff1a;Key value 3、Map集合也被称为键值对集合 Map集合是键值对集合 Map集合非常适合做购物车这种业务场景 适应最对的Map集合时HashMap 二、Map集合体系的特点 1…

qsort函数模拟实现(利用冒泡函数实现)

文章目录1.qsort函数介绍2.模拟实现qsort函数2.1冒泡排序2.2使用回调函数&#xff0c;模拟实现qsort&#xff08;采用冒泡的方式&#xff09;3.一道排序题3.1解析1.qsort函数介绍 下面我们来看代码 int int_cmp(const void* p1, const void* p2) {return(*(int*)p1 - *(int*)p…

【数据结构与算法01】 算法的复杂度

文章目录时间复杂度的概念时间复杂度&#xff1a;常[1]、对[logn]、幂[n^2]、 指[2^n]、阶[n!]例题❗易错提醒空间复杂度&#x1f354;算法原地工作&#xff1a;算法所需的内存空间为常量例题时间复杂度的概念 例1&#xff1a;假设n3000n3000n3000 i2998,print("I love Yo…

head first java3

QA 为啥所有东西都在类中&#xff1a;Java面向对象&#xff0c;类是对象的蓝图&#xff0c;Java绝大多数都是对象 每个类都需要加一个main吗&#xff1a;一个程序中&#xff0c;一个就够 int x2;while(x) 对吗&#xff1a;错误&#xff0c;while中间是Boolean类型。boolean xtr…

java基于springboot+Vue+nodejs的高校运动会报名管理系统 element

运动是伴随人类一生的一种行为和活动&#xff0c;只有不断的运动才能够彰显生命的意义&#xff0c;尤其是当代的学生&#xff0c;课业繁重往往忽略了体育锻炼&#xff0c;为了能够提高学子们对体育运动的积极性&#xff0c;基本所有的高校每年都会定期的举办运动会。传统的运动…