使用 HorizontalScrollerView 完成 水平可滑动的分类栏效果
来源:互联网 发布:数据库毕业论文题目 编辑:程序博客网 时间:2024/06/06 01:06
在分类中我们时常要用到这种效果
特点:
1.每行第一个和后面的不一样,第一个没有边框
2.除了每行第一个,其他的都可以点击,且有颜色的变换
3.每一行都可以单独的水平滑动
4.有多行,每行相互独立
其实它的实现很简单,下面让我们开始:
还是那句话,不要想一口气吃成一个大胖子,我们先来完成一个~
第一步:布局里写一个HorizontalScrollView,里面包含一个水平的线性布局
<?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="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.horizontalscrollerview.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:background="#ed3f3f" android:gravity="center" android:text="HorizontalScrollView的使用" android:textColor="#ffffff" android:textSize="18sp" /> <!--HorizontalScrollView,里面包含一个水平线性--> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="50dp" android:layout_gravity="center_vertical" android:background="#ffffff" android:scrollbars="none"> <!--水平线性布局--> <LinearLayout android:id="@+id/hscrollerview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginTop="5dp" android:orientation="horizontal"></LinearLayout> </HorizontalScrollView></LinearLayout>
第二步:实例化它的直接子孩子--线性布局
LinearLayout hscrollerview= (LinearLayout)findViewById(R.id.hscrollerview);
第三步:准备数据--> 这里我们以String数组的形式直接提供
(实际开发中一般都会从网络来获取数据)
private String[] titles1 = {"类型", "爱情", "喜剧", "动画", "剧情", "恐怖", "惊悚", "科幻", "动作", "悬疑", "冒险", "战争", "奇幻", "运动", "家庭", "古装", "武侠", "西部", "历史", "传记", "情色", "歌舞", "短片", "纪录片", "其他"};private String[] titles2 = {"地区", "大陆", "美国", "韩国", "日本", "中国", "中国香港", "中国台湾", "泰国", "印度", "法国", "英国", "澳大利亚", "其他"};private String[] titles3 = {"年代", "2017以后", "2017", "2016", "2015", "2014", "2013", "2012", "2011", "90年代", "80年代", "70年代", "其他"};
第四步:设置数据
setData(hscrollerview);
private void setData(LinearLayout hscrollerview) { //① 创建item布局,并添加到水平线性布局中 //由于第一个和其他的item不一样所以我们分别添加,相应的为其添加两种不同的item布局 //第一种item布局不要加边框背景 //第二种item布局要加边框背景 // ② 先添加第一个数据 view0 = View.inflate(mContext, R.layout.item_hscollerview0, null); tv0 = (TextView) view0.findViewById(R.id.tv0); tv0.setText(titles1[0]); //添加进去 hscrollerview.addView(view0); // ③ 再循环添加后面的数据 for (int i = 1; i < titles1.length; i++) { View view = View.inflate(mContext,R.layout.item_hscollerview1, null); TextView tv1 = (TextView) view.findViewById(R.id.tv1); //创建好TextView后就立马给它设置点击事件的监听 setListener(tv1); tv1.setText(titles1[i]); //添加进去 hscrollerview.addView(view); } }
private void setListener(final TextView tv1) { tv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(mContext, tv1.getText(), Toast.LENGTH_SHORT).show(); } }); }对应的第一种item的布局(背景没有边框):item_hscollerview0.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="horizontal"> <TextView android:id="@+id/tv0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="2dp" android:gravity="center_vertical" android:text="类型" android:textColor="#99000000" android:textSize="16sp" /></LinearLayout>对应的第一种item的布局(背景添加了边框):item_hscollerview1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="horizontal"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@drawable/tvbg_mvfind_sector" android:clickable="true" android:text="爱情" android:textColor="#000000" android:textSize="15sp" /></LinearLayout>对应的选择器:tvbg_mvfind_sector.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/hscroll_shape_unchecked" android:state_pressed="false" /> <item android:drawable="@drawable/hscroll_shape_checked" android:state_pressed="true" /></selector>对应的两个shape:hscroll_shape_unchecked.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#fff" /> <corners android:radius="35dp" /> <stroke android:width="1dp" android:color="#33000000" android:dashWidth="1dp" /> <padding android:bottom="3dp" android:left="18dp" android:right="18dp" android:top="3dp" /></shape>hscroll_shape_checked.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#ed3f3f" /> <corners android:radius="35dp" /> <stroke android:width="1dp" android:color="#11000000" android:dashWidth="1dp" /> <padding android:bottom="3dp" android:left="18dp" android:right="18dp" android:top="3dp" /></shape>
好了这样第一个就做好了
效果如下图:
下面开始做第二、三个:
布局上:我们只要再复制两个HorizontalScrollerView,改下id
代码上:同样的初始化
LinearLayout hscrollerview1 = (LinearLayout) findViewById(R.id.hscrollerview1); LinearLayout hscrollerview2 = (LinearLayout) findViewById(R.id.hscrollerview2); LinearLayout hscrollerview3 = (LinearLayout) findViewById(R.id.hscrollerview3);
然后我们把setData()方法多添加一个参数,抽取为一个公共的方法:
private void setData(LinearLayout hscrollerview, String[] titles) { view0 = View.inflate(mContext, R.layout.item_hscollerview0, null); tv0 = (TextView) view0.findViewById(R.id.tv0); tv0.setText(titles[0]); hscrollerview.addView(view0); for (int i = 1; i < titles.length; i++) { View view = View.inflate(mContext, R.layout.item_hscollerview1, null); TextView tv1 = (TextView) view.findViewById(R.id.tv1); setListener(tv1); tv1.setText(titles[i]); hscrollerview.addView(view); } }
最后调用方法:
setData(hscrollerview1, titles1);setData(hscrollerview2, titles2);setData(hscrollerview3, titles3);
完成~
再看下最终效果:
7 0
- 使用 HorizontalScrollerView 完成 水平可滑动的分类栏效果
- Android--ListView的item水平滑动效果
- 使用ViewPager和GridView配合,实现GridView横向水平滑动的效果。
- Android可滑动的开关效果
- 水平垂直都可滑动的HorizontalVerticalViewPager实现
- 仿QQ身边的人的水平滑动效果
- 类似今日头条网易新闻导航栏水平滑动的效果
- 纯css一页显示多个块的水平滑动效果
- 使用Fragment,建立一个没有滑动效果的切换,可配合抽屉使用
- 带文字的水平垂直seekbar,也可当普通seekbar使用,可以解决垂直滑动冲突。
- 美团分类导航按钮效果使用ViewPager+GridView实现左右滑动查看更多分类的功能
- 利用三方PagerBottomTabStrip完成底部的page滑动效果
- ListView的水平滑动
- dede超链接滑动分类效果
- 两种页面滑动门的效果,具备可扩展性
- wpf实现仿苹果水平滑动效果
- windows phone水平滑动翻页动画效果
- UI--单行文本水平触摸滑动效果
- 【物联网(IoT)开发】物联网及NodeRed 技术讲解及动手实践活动分享
- 左旋转字符串
- 数据结构实验之查找六:顺序查找
- 进程通信总结
- KMP算法
- 使用 HorizontalScrollerView 完成 水平可滑动的分类栏效果
- linux中oracle执行sql文件
- 哈尔滨理工大学软件学院ACM程序设计全国邀请赛(网络同步赛) D. Pairs FFT
- 顺序查找(34)
- mysql 5.7.16免安装版配置
- LR-微信订车压力测试
- CSDN爬虫(六)——动态网页爬取的两种策略
- I/O流java
- C++流控制 部分