android listview 不显示_Android使用ExpandableListview实现时间轴
背景
公司新项目中有一个功能要实现时间轴效果,网上也找了很多教程和Demo,但都不是我想要的,大部分用的是RecyclerView实现的,但这些都是一层的数据结构,但我需要的是两层的结构,用RecyclerView应该也可以实现这种效果,但是结合后台返回的数据,用RecyclerView实现可能有些麻烦,最终我选择了使用ExpandableListview实现两层结构的时间轴效果,下面是做好的效果图:
分析
代码很简单,和普通用ExpandableListView一样,没什么特别的,需要注意的是布局的结构。从上面的效果图可以看出,group布局中应该包含这些控件:显示年月的圆形,圆形上方的line和圆形下方的line,其他的就是child布局中的内容了,需要注意的最右侧也是一个动态数据的view,即listview之类的可以动态添加数据的,但是如果要用listview的话,这就会牵扯出来一个亘古久远的问题listview嵌套问题,我们这里等于是在ExpandableListView的child中嵌套listview,注意要想让listview完全伸展就需要重新listview的onMeasure方法,像下面这样:
public class ListViewForScrollView extends ListView { public ListViewForScrollView(Context context) { super(context); } public ListViewForScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ListViewForScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override /** * 重写该方法,达到使ListView适应ScrollView的效果 */ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }}
这是我之前一直在用的方法,但是这样的话带来的问题就是listview的item不能复用而且adapter里面的getView方法会执行很多遍,这无疑是非常浪费性能的,数据多或者布局复杂就会很卡顿,所以今天我们不再用这样的方法了,而是用LinearLayout来实现,这个在下面我们再详细说明,接着分析我们的布局结构,child中以那个小圆view开始,它上边和右边各有一个line,这样child上方的line就可以和group下方的line连接在一起,child上方的line也可以和上一个child中的小圆view连接子在一起,child布局中的小圆和line我是使用LinearLayout和margin来控制对齐的,这样右侧显示动态数据的view就可以根据内容自动填充高度,当然也可以使用其他布局的方式实现。下面是item布局的代码:
group布局:
<?xml version="1.0" encoding="utf-8"?> android:layout_width="wrap_content" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"> android:layout_marginLeft="25dp" android:gravity="center_horizontal" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> android:id="@+id/item_group_top_line" android:background="@color/mine_font_little" android:layout_width="0.5dp" android:layout_height="50dp"/> android:id="@+id/item_group_year_month" android:gravity="center" tools:text="1月\n2017" android:textColor="@android:color/white" android:background="@drawable/shape_orange_circle" android:layout_width="wrap_content" android:layout_height="wrap_content"/> android:id="@+id/item_group_bottom_line" android:background="@color/mine_font_little" android:layout_width="0.5dp" android:layout_height="match_parent"/>
child布局:
<?xml version="1.0" encoding="utf-8"?> android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools"> android:gravity="center_horizontal" android:orientation="horizontal" android:layout_marginLeft="47dp" android:layout_width="match_parent" android:layout_height="wrap_content"> android:orientation="vertical" android:minHeight="50dp" android:layout_width="match_parent" android:layout_height="wrap_content"> android:orientation="horizontal" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="0dp"> android:id="@+id/item_child_v_line" android:layout_marginLeft="5dp" android:background="@color/mine_font_little" android:layout_width="0.5dp" android:layout_height="match_parent"/> android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@+id/item_child_month_day" android:layout_marginLeft="10dp" android:layout_marginBottom="5dp" tools:text="1月9日" android:layout_gravity="bottom" android:textSize="16sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> android:id="@+id/item_child_lv" android:layout_marginLeft="5dp" android:layout_marginBottom="5dp" android:minHeight="50dp" android:orientation="vertical" android:gravity="bottom" android:layout_width="match_parent" android:layout_height="wrap_content"/> android:orientation="horizontal" android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> android:id="@+id/item_child_circle" android:background="@drawable/shape_little_orange_circle" android:layout_width="10dp" android:layout_height="10dp"/> android:id="@+id/item_child_r_line" android:background="@color/mine_font_little" android:layout_width="match_parent" android:layout_height="0.5dp"/>
在child布局中看到有个NestFullListView,这个就是上面我们说的使用LinearLayout实现的伸展ListView,通过继承LinearLayout,向布局中动态addView来实现,做了类似ListView的adapter和ViewHolder封装,使用起来特别方便,只需几行代码:
childHolder.itemChildLv.setAdapter(new NestFullListViewAdapter(R.layout.item_child_timeline_data,daylistBean.getDatalist()) { @Override public void onBind(int pos, TimeLine.RowsBean.DaylistBean.DatalistBean datalistBean, NestFullViewHolder holder) { holder.setText(R.id.item_child_sr_name,datalistBean.getName()); holder.setText(R.id.item_child_sr_progress,datalistBean.getProgress() + "%"); } });
其中的itemChildLv就是NestFullListView,给它设置一个adapter,adapter指定了数据类型,再给adapter传入item布局的id和数据,通过回调函数onBind,设置指定控件的值就可以了,是不是很方便呢!
源码地址:
https://github.com/hnxylc8818/TimeLineDemo
到这里就结束啦.
android listview 不显示_Android使用ExpandableListview实现时间轴相关推荐
- android listview分页显示,Android应用中使用ListView来分页显示刷新的内容
点击按钮刷新1.效果如下: 实例如下: 上图的添加数据按钮可以换成一个进度条 因为没有数据所以我加了一个按钮添加到数据库用于测试:一般在服务器拉去数据需要一定的时间,所以可以弄个进度条来提示用户: ...
- android listview 不显示_ListView详细介绍与使用
image 前言介绍: 关于 ListView 我们大家都应该是非常的熟悉了,在 Android 开发中是经常用到的,今天就再来回顾一下,ListView 的使用方法,和一些需要优化注意的地方,还有日 ...
- android listview添加数据_Android面经分享,失业两个月,五一节前拿到offer
秦子帅明确目标,每天进步一点点..... 作者 | 天天有道地址 | juejin.im/post/5eb01866f265da7b9c24562c 基本介绍 今天介绍一位朋友的经历: 从3月初开 ...
- android listview添加数据_Android系统列表控件
在android系统控件中,有多个控件可以展示列表数据. 一.ListView 该组件是android中最常用的一个UI组件,用于实现在屏幕上显示多个内容,以便于我们用手指进行滑动. ListView ...
- android progressbar 不显示_Android多线程技术选型最全指南(1)
码个蛋(codeegg) 第 935 次推文 作者:qing的世界 链接:https://juejin.im/post/5d1eb4acf265da1bb003de71 前言 前段时间在组内做了一下现 ...
- android内容显示不出来,android – listview不显示任何内容并隐藏数据
我相信你的问题可能是因为你试图显示的项目多于屏幕上的空间.为了抵消这种影响,您可以使用 HorizontalScrollView,让您可以水平滚动以查看所有视图. 此方向属性也不适用于Relative ...
- android toast居中显示_Android和iOS7的差异点!
之前在北京望京SOHO的mou公司做设计一直是iOS,适配Android,所以在做设计时,大部分使用的移动设备是iPhone,这样针对于中型企业: 而后来在一家智能体感设备创业型公司,目标人群是二.三 ...
- android toast居中显示_android Toast 弹出在屏幕中间位置以及自定义Toast
Toast 我想我们应该使用的都很多,一般我们使用默认设置较多,但是默认设置往往不能满足我们的需求,那我们现在来自定义下: 默认Toast: Toast.makeText(MainActivity.t ...
- Android ListView item显示时高度变化
这里说的这个高度变化并不是指每个item的高度不一样,而是只,在界面已经显示的listview的某个item的数据变多了,那么这一个item的高度刷新一下.其实也就是从新计算一下高度. 先看效果图: ...
最新文章
- Java字符串就该这样设计
- 简单的Writer和Reader
- java之servlet
- static 应用php,PHP static的一例应用
- AssemblyExecuteAdapter
- java线程池应用的好处_java高级应用:线程池全面解析
- 《你不知道的JavaScript(上)》笔记——函数作用域和块作用域
- python画五角星代码_Python使用Turtle模块绘制五星红旗代码示例
- TypeScript 终极初学者指南
- python组合数据类型实验_实验七 组合数据类型
- Redis-过期Key删除/淘汰Kry策略
- 前端编码规范之JavaScript
- GrapeCity Documents for Excel 与 Apache POI 功能对比
- Transaction And Lock--已提交读快照
- [NOIp 2012]同余方程
- 晶振封装与频率对照表
- 个人用户实现发送短信功能
- 基因组变异类型详解及区分
- Fingerprint
- Windows和Mac OS的伽马值
热门文章
- jetbrick-template 和其他模板的性能测试比较
- restlet使用_使用Restlet Framework构建联网汽车
- 高性能滚动scroll(防抖和节流)
- java 保存和读取本地文件
- 巧用 | 低成本高可用,巧用Redis
- 每日面试之Java集合
- 视觉SLAM笔记(8) 齐次坐标
- JAVA中的适配器应用_Java适配器模式详解和实际应用.md
- 2017年春季计算机试题,【2017年整理】计算机试题.doc
- sphinx 字符串转html,在Pycharm中获取Sphinx,以将我的文档字符串包含在生成的html中...