在许多App上都能看到时光轴的效果,比如携程等等,那么我们今天就利用ExpandableListView来实现一个时光轴效果,先来看看效果图:

效果还是挺简单的,这里我们主要是采用ExpandableListView来实现,关于ExpandableListView的详细使用参见(android开发之ExpandableListView的使用,实现类似QQ好友列表),我们这里主要介绍这个效果的实现:

先来看看主布局文件:

<RelativeLayout 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" > <TextView
android:id="@+id/title_p"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="12dp"
android:gravity="center"
android:text="2015年11月"
android:textSize="18sp" /> <View
android:id="@+id/hor_div"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/title_p"
android:background="#9F79EE" /> <View
android:id="@+id/ver_div"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_below="@id/hor_div"
android:layout_marginLeft="70dp"
android:background="#9F79EE" /> <TextView
android:id="@+id/title_c"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/hor_div"
android:layout_marginLeft="60dp"
android:paddingBottom="12dp"
android:paddingLeft="18dp"
android:paddingTop="12dp"
android:text="时光轴"
android:textSize="24sp" /> <ExpandableListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/title_c"
android:layout_marginLeft="60dp"
android:background="@null"
android:clickable="false"
android:divider="@null" >
</ExpandableListView> </RelativeLayout>

两条分割线用View来做,整体不难,不多说,看看MainActivity

public class MainActivity extends Activity {

	private List<TimeLineBean> list;
private ExpandableListView lv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initData();
initView();
} private void initView() {
lv = (ExpandableListView) this.findViewById(R.id.lv);
lv.setAdapter(new MyAdapter(MainActivity.this, list));
for (int i = 0; i < list.size(); i++) {
lv.expandGroup(i);
}
lv.setGroupIndicator(null);
lv.setOnGroupClickListener(new OnGroupClickListener() { @Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
return true;
}
});
} private void initData() {
list = new ArrayList<TimeLineBean>();
List<String> things = new ArrayList<String>();
things.add("六王毕,四海一;");
things.add("蜀山兀,阿房出。");
things.add("覆压三百余里,隔离天日。");
list.add(new TimeLineBean("11月24日", things));
things = new ArrayList<String>();
things.add("骊山北构而西折,");
things.add("直走咸阳。");
things.add("二川溶溶,流入宫墙。");
list.add(new TimeLineBean("11月23日", things));
things = new ArrayList<String>();
things.add("五步一楼,十步一阁;");
things.add("廊腰缦回,");
list.add(new TimeLineBean("11月22日", things));
things = new ArrayList<String>();
things.add("檐牙高啄;");
things.add("各抱地势,钩心斗角。");
things.add("盘盘焉,囷囷焉,蜂房水涡,");
things.add("矗不知乎几千万落!");
list.add(new TimeLineBean("11月21日", things));
things = new ArrayList<String>();
things.add("长桥卧波,未云何龙?");
things.add("複道行空,不霁何虹?");
things.add("高低冥迷,不知西东。");
list.add(new TimeLineBean("11月20日", things));
things = new ArrayList<String>();
things.add("歌台暖响,");
things.add("春光融融;");
list.add(new TimeLineBean("11月19日", things));
things = new ArrayList<String>();
things.add("舞殿冷袖,");
things.add("风雨凄凄。");
things.add("一日之内,一宫之间,");
things.add("而气候不齐。");
list.add(new TimeLineBean("11月18日", things));
}
}

在MainActivity中我们先初始化模拟数据,然后初始化View,初始化View的过程中,通过一个for循环让所有的group展开,然后再屏蔽掉group的点击事件,好了,再来看看Adapter:

public class MyAdapter extends BaseExpandableListAdapter {

	private Context context;
private List<TimeLineBean> list; public MyAdapter(Context context, List<TimeLineBean> list) {
this.context = context;
this.list = list;
} @Override
public Object getChild(int groupPosition, int childPosition) {
return list.get(groupPosition).getThings();
} @Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
ChildHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.child_item, null);
holder = new ChildHolder();
holder.tv = (TextView) convertView.findViewById(R.id.ci_thing);
convertView.setTag(holder);
} else {
holder = (ChildHolder) convertView.getTag();
}
holder.tv.setText(list.get(groupPosition).getThings().get(childPosition));
return convertView;
} @Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getThings().size();
} @Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition).getDate();
} @Override
public int getGroupCount() {
return list.size();
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.group_item, null);
holder = new GroupHolder();
holder.tv = (TextView) convertView.findViewById(R.id.gi_date);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.tv.setText(list.get(groupPosition).getDate());
return convertView;
} @Override
public boolean hasStableIds() {
return false;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
} class GroupHolder {
TextView tv;
} class ChildHolder {
TextView tv;
} }

这个Adapter也没啥讲的,大家如果有疑问可以参见android开发之ExpandableListView的使用,实现类似QQ好友列表,这里有ExpandableListView的详细介绍。最后就是两个item文件:

group_item.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:orientation="horizontal" > <View
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginTop="22dp"
android:background="@drawable/circle" /> <TextView
android:id="@+id/gi_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingLeft="12dp"
android:paddingTop="18dp"
android:text="11月24号"
android:textSize="22sp" /> </LinearLayout>

child_item.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:orientation="vertical" > <TextView
android:id="@+id/ci_thing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="22dp"
android:paddingTop="8dp"
android:text="11月24号"
android:textColor="#999999"
android:textSize="16sp" /> </LinearLayout>

每个group_item的前面有一个红色的实心球,这个球我们用shape来绘制,关于shape的使用参见android开发之shape详解

circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" > <corners android:radius="10dp" /> <size
android:height="10dp"
android:width="10dp" /> <solid android:color="#FF6A6A" /> </shape>

好了,这个东西貌似没难度,其实就是ExpandableListView的使用。

Demo下载http://download.csdn.net/detail/u012702547/9297507

使用ExpandableListView实现一个时光轴的更多相关文章

  1. 关于ExpandableListView的一个小例子

    喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到an ...

  2. android自定义View之仿通讯录侧边栏滑动,实现A-Z字母检索

    我们的手机通讯录一般都有这样的效果,如下图: OK,这种效果大家都见得多了,基本上所有的android手机通讯录都有这样的效果.那我们今天就来看看这个效果该怎么实现. 一.概述 1.页面功能分析 整体 ...

  3. 时光轴三之 ExpandableListView版时光轴效果

    上两篇讲到了用listView和recyclerView来实现时光轴,这一篇我们用ExpandableListView来实现时光轴,废话不多说,直接来代码. 还是先activity_main.xml ...

  4. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

  5. Android中使用ExpandableListView实现好友分组

    一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源 ...

  6. Android常用控件之GridView与ExpandableListView的用法

    概述 1.GridView:与ListView相比,可以显示多列,xml布局时其属性numColumns可以设置显示的列数. 2.ExpandableListView:与ListView相比,可以让每 ...

  7. Android之ExpandableListView

    ExpandableListView可以用来表现多层级的listView,本文主要是ExpandableListView的一个简单实现 布局文件 <LinearLayout xmlns:andr ...

  8. Android ExpandableListView使用+获取SIM卡状态信息

    ExpandableListView 是一个可以实现下拉列表的控件,大家可能都用过QQ,QQ中的好友列表就是用ExpandableListView实现的,不过它是自定义的适配器.本篇 博客除了要介绍E ...

  9. Android 高级UI设计笔记01:使用ExpandableListView组件(ListView的扩展)

    1.ExpandableListView是一个用来显示二级节点的ListView. 比如如下效果的界面: 2.使用ExpandableListView步骤 (1)要给ExpandableListVie ...

随机推荐

  1. 【DataStructure In Python】Python模拟链表

    最近一直在学习Python和Perl这两门语言,两者共同点很多,也有不多.希望通过这样的模拟练习可以让自己更熟悉语言,虽然很多时候觉得这样用Python或者Perl并没有体现这两者的真正价值. #! ...

  2. Hadoop中两表JOIN的处理方法

    Dong的这篇博客我觉得把原理写的很详细,同时介绍了一些优化办法,利用二次排序或者布隆过滤器,但在之前实践中我并没有在join中用二者来优化,因为我不是作join优化的,而是做单纯的倾斜处理,做joi ...

  3. java文件IO操作

    package com.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream ...

  4. java里的静态变量是放在了堆内存还是栈内存?

    堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令) 2.jvm只有一个堆区(heap)被所有线程共享,堆中不存放基本类型和对象引用,只存放对 ...

  5. Reflector+Reflexil 相结合实现对DLL文件修改

    在工作过程中,我们有可能遇到这样的问题:公司发给客户的软件包突然报错了,但是你知道哪里报错了,而这个代码已经编译成DLL文件了,源代码不在自己这里.怎么办呢?还好现在有Reflexil插件,这个插件只 ...

  6. 巧架个人BT服务器

    BT的流行比之当年的江湖,传奇等的流行有过之而无不及.现在我们常说的话题是:“今天你BT了吗?”哇K!兄弟们你们怎么吐了?!假如你还不知道BT是什么,那似乎证明你已经到了归隐Internet的时候了, ...

  7. 【 D3.js 选择集与数据详解 — 1 】 使用datum()绑定数据

    选择集和数据的关系是 D3 最重要的基础,在[入门 - 第 7 章]时进行过些许讲解,对于要掌握好 D3 是远远不够的.故此开设一个新的分类,专门讨论选择集与数据的关系,包括数据绑定的使用和工作原理, ...

  8. 使用Apache 的lib进行List、Set、数组之间的转换(转)

    使用Apache Jakarta Commons Collections: import org.apache.commons.collections.CollectionUtils; String[ ...

  9. zzzz

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Management; u ...

  10. Frame 处理

    # -*- coding:utf-8 -*- """ 通过 id 或 name 识别处理 fram 框架 """ from selenium ...