android使用 ScrollerView 实现 可上下滚动的分类栏实例
作者:李诗雨 发布时间:2022-11-03 16:38:57
标签:ScrollerView,上下滚动
如果不考虑更深层的性能问题,我个人认为ScrollerView还是很好用的。而且单用ScrollerView就可以实现分类型的RecyclerView或ListView所能实现的效果。
下面我单单从效果展示方面考虑,使用ScrollerView实现如下图所示的可滚动的多条目分类,只是为了跟大家一起分享一下新思路。(平时:若从复用性等方面考虑,这显然是存在瑕疵的~)
特点描述:
1.可上下滚动
2.有类似于网格布局的样式
3.子条目具有点击事件
刚看到这个效果时,首先想到的是使用分类型的RecyclerView 或者 ListView ,里面再嵌套GridView来实现。
但转而又一想ScrollerView也可以滚动,只要往里面循环添加子item不就可以了吗。
实现的逻辑大致如下:
具体的实现如下:
第一步:布局里写一个ScrollerView,里面添加一个竖直的线性布局
第二步:实例化垂直线性布局
allhonor_hscroll = (LinearLayout) findViewById(R.id.allhonor_hscroll);
第三步:联网请求获取数据
setAllHonorData();
/**
* 使用okhttp
*/
public void setAllHonorData() {
OkHttpUtils
.get()
.url(url)
.build()
.execute(new StringCallback() {
@Override
public void onError(okhttp3.Call call, Exception e, int id) {
Log.e("TAG", "111");
Log.e("TAG", "onError" + e.getMessage());
}
@Override
public void onResponse(String response, int id) {
Log.e("TAG", "222");
Log.e("TAG", "onRespons" + response);
//联网成功后使用fastjson来解析数据
processData(response);
}
@Override
public void onBefore(Request request, int id) {
}
@Override
public void onAfter(int id) {
}
});
}
/**
* 使用fastjson进行解析
*
* @param json
*/
private void processData(String json) {
//使用GsonFormat生成对应的bean类
com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(json);
String data = jsonObject.getString("data");
List<AllHonorBean.HornorBean> hornorsList = JSON.parseArray(data, AllHonorBean.HornorBean.class);
//测试是否解析数据成功
// String strTest = hornorsList.get(0).getRegion();
// Log.e("TAG", strTest);
//第四步:装配数据,使用两层for循环
}
第四步:设置两种item的布局
第一种item布局:item_allhornors0.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="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<TextView
android:id="@+id/tv_allhornors_big"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#11000000"
android:gravity="center_vertical"
android:paddingBottom="12dp"
android:paddingLeft="13dp"
android:paddingTop="12dp"
android:text="热门"
android:textColor="#000000"
android:textSize="14sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#11000000" />
</LinearLayout>
第二种item布局:item_allhornors1.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="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_allhornors_sn0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:paddingBottom="12sp"
android:paddingTop="12sp"
android:text="你好你好"
android:textColor="#000000"
android:textSize="13sp" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#11000000" />
<!--注意这里的text文本一定要为空,不要设置任何内容-->
<TextView
android:id="@+id/tv_allhornors_sn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:gravity="center"
android:paddingBottom="12sp"
android:paddingTop="12sp"
android:text=""
android:textColor="#000000"
android:textSize="13sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#11000000" />
</LinearLayout>
第五步:装配数据
if (hornorsList != null && hornorsList.size() > 0) {
//-->外层
for (int i = 0; i < hornorsList.size(); i++) {
//创建并添加第一种item布局
View globalView = View.inflate(this, R.layout.item_allhornors0, null);
TextView tv_allhornors_big = (TextView) globalView.findViewById(R.id.tv_allhornors_big);
AllHonorBean.HornorBean hornorsListBean = hornorsList.get(i);
String region = hornorsListBean.getRegion();
//外层for中直接装配数据
tv_allhornors_big.setText(region);
//将布局添加进去
allhonor_hscroll.addView(globalView);
List<AllHonorBean.HornorBean.FestivalsBean> festivalsList = hornorsListBean.getFestivals();
//-->内层,每次装两个数据
for (int j = 0; j < festivalsList.size(); j = j + 2) {
//创建并添加第二种item布局
View smallView = View.inflate(this, R.layout.item_allhornors1, null);
final TextView tv_sn0 = (TextView) smallView.findViewById(R.id.tv_allhornors_sn0);
TextView tv_sn1 = (TextView) smallView.findViewById(R.id.tv_allhornors_sn1);
//顺带在这里就直接添加点击事件的监听
if (j < festivalsList.size() - 1) {
setListener(tv_sn0, tv_sn1);
}
//装配左边的数据
honorName0 = festivalsList.get(j).getFestivalName();
tv_sn0.setText(honorName0);
//判读越界否
if (j < festivalsList.size() - 1) {
//装配右边的数据
honorName1 = festivalsList.get(j + 1).getFestivalName();
tv_sn1.setText(honorName1);
}
//添加进去
allhonor_hscroll.addView(smallView);
}
}
}
点击事件的监听:
private void setListener(final TextView tv_sn0, final TextView tv_sn1) {
//给左边的TextView 设置监听
tv_sn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "" + tv_sn0.getText(), Toast.LENGTH_SHORT).show();
}
});
//给右边的TextView 设置监听
tv_sn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "" + tv_sn1.getText(), Toast.LENGTH_SHORT).show();
}
});
}
完成~
再看一眼最后效果:
来源:http://blog.csdn.net/cjm2484836553/article/details/53455641


猜你喜欢
- 本文为大家介绍了FTP上传下载队列窗口的实现方法,供大家参考,具体内容如下1、首先看一下队列窗口的界面2、看一下上传队列窗口的界面3、看一下
- 一. BigInteger类1. 简介在之前给大家讲解8种基本类型时就说过,不同的数据类型,有不同的取值范围,我们再通过下表回顾一下:类型所
- 被kafka-client和springkafka版本坑上周刚刚欢天喜地的在linux上部了kafka,这周打算用spring-boot框架
- 本文实例讲述了C#执行SQL事务用法。分享给大家供大家参考。具体分析如下:1.通过存储过程。2.通过C#中提供的Transaction。这里
- android电话管理器(TelephonyManger)实例:TelephonyManger是管理电话状态、网络信息的服务类。添加权限:&
- 今天,写了个小代码。抓取首页中的极客头条。效果如图:分享给新手朋友。要点:1.使用ApacheHttpClient库实现GET请求。2.异步
- 发展历史Gradle 的依赖管理是一个从开始接触 Android 开发就一直伴随着我们的问题(作者是Andro
- 之前写轮播条或者指示器的时候都是UI图里面直接有,这样的效果并不好,给用户的体验比较差,所以闲暇之余自己写了个指示器,可以展现出一个优雅的效
- 一、创建Config配置中心项目1.添加依赖 <dependency> <groupId>org.sp
- 网上关于下拉刷新的文章也不少,不过都太长了。恰好发现了官方的下拉刷新库,而且效果还是不错的,简洁美观,用得也挺方便。下面是效果图:我的好友原
- 本文实例为大家分享了Unity实现场景漫游相机的具体代码,供大家参考,具体内容如下前言拿到场景后总喜欢在场景里面玩一段时间,那这个脚本就是你
- 简介Exchanger是java 5引入的并发类,Exchanger顾名思义就是用来做交换的。这里主要是两个线程之间交换持有的对象。当Exc
- 背景相信大家对Android Studio已经不陌生了,Android Studio是Google于2013 I/O大会针对Android开
- 可以理解当我们要调用一个方法时,我们会把指定的数值,传递给方法中的参数,这样方法中的参数就拥有了这个指定的值,可以使用该值,在方法中运算了。
- 本文实例讲述了Java基于外观模式实现美食天下食谱功能。分享给大家供大家参考,具体如下:一、模式定义外观模式,是软件工程师常用的一种软件设计
- 大顶堆每个结点的值都大于或等于其左右孩子结点的值小顶堆每个结点的值都小于或等于其左右孩子结点的值对比图实现代码public class He
- 1.springboot使用log4j2springboot使用的common-logging,底层兼容各种日志框架如,log4j2,slf
- 一、前言程序中经常会用到TabControl控件,默认的控件样式很普通。而且样式或功能不一定符合我们的要求。比如:我们需要TabContro
- 本文实例讲述了C#简单实现显示中文格式星期几的方法。分享给大家供大家参考,具体如下:1.DateTime.Now.ToString(&quo
- 近几天又温习了一下SpringMVC的运行机制以及原理我理解的springmvc,是设计模式MVC中C层,也就是Controller(控制)