在许多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. 利用switch case判断是今天的第多少天

    static void Main(string[] args)        {            while (true)            {                int m1 ...

  2. C#常用的命名规范

    C#常用的命名规则 Pascal 规则 每个单词开头的字母大写(如 TestCounter). Camel 规则 除了第一个单词外的其他单词的开头字母大写. 如. testCounter. Upper ...

  3. BZOJ_1778_[Usaco2010_Hol]_Dotp_驱逐猪猡_(期望动态规划+高斯消元+矩阵)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1778 炸弹从1出发,有\(\frac{P}{Q}\)的概率爆炸,如果不爆炸,等概率移动到连通的 ...

  4. 新浪微博2.5.1 for Android 去广告

    新浪微博更新到2.5.1版,就开始来广告了,伤不起啊... 亲,看到没,手机屏幕就那么一小块,还要往里面塞东西,另外是一个在后台运行的AdCenter服务. 所需要用到的工具有:apktool,JD- ...

  5. 【原】CAVLC的个人理解

    4x4数据块经过预测.变换.量化后,非零系数主要集中在低频部分,而高频部分大部分是零.数据经过zig-zag扫描后,从左->右(低频->高频),DC系数附近的系数非常大,而高频的非零系数大 ...

  6. NOIP2015 提高组(senior) 解题报告

    过了这么久才来发解题报告,蒟蒻实在惭愧 /w\ Day1 T1 [思路] 模拟 [代码] #include<iostream> #include<cstring> #inclu ...

  7. Clean Code – Chapter 6 Objects and Data Structures

    Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...

  8. 我理解的RPC

    这两天在学习公司一牛人写的RPC框架,一直都对RPC的概念很模糊,现在稍微清晰了些.   rpc定义:RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一 ...

  9. HW5.14

    public class Solution { public static void main(String[] args) { System.out.printf("%s\t%s\n&qu ...

  10. JavaScript删除数组重复元素的5个高效算法

    之前一段时间一直在准备面试, 因而博客太久没更新: 现在基本知识点都复习完毕, 接下来就分享下 面试的一些常见问题: 去正规的互联网公司笔试.面试有很大的概率会碰到 使用javascript实现数组去 ...