/**
*
* 转载请标明出处:http://blog.youkuaiyun.com/u013598111/article/details/50519404
* @author:【JunTao_sun】
*
*
时光轴实现
public class MainActivity extends Activity {
private ExpandableListView expandlistView;
private MyExpandAdapter statusAdapter;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
context = this;
expandlistView = (ExpandableListView) findViewById(R.id.expandlist);
initExpandListView();
}
/**
* 初始化
*/
private void initExpandListView() {
statusAdapter = new MyExpandAdapter(context, getListData());
expandlistView.setAdapter(statusAdapter);
// 去掉默认带的箭头
expandlistView.setGroupIndicator(null);
// 设置默认选中项
expandlistView.setSelection(0);
// 遍历所有group,将所有项设置成默认展开
int groupCount = expandlistView.getCount();
for (int i = 0; i < groupCount; i++) {
expandlistView.expandGroup(i);
}
expandlistView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
return false;
}
});
}
// @Override
// public void onBackPressed()
// {
// Intent intent = new Intent(Intent.ACTION_MAIN);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.addCategory(Intent.CATEGORY_HOME);
// startActivity(intent);
// }
@Override
public void onBackPressed()
{
//按返回键返回桌面
moveTaskToBack(true);
}
private List<ParentEntity> getListData() {
List<ParentEntity> groupList;
String[] strArray = new String[] { "1月5日", "1月2日","12月25日","12月22日","12月20日"};
String[][] childTimeArray = new String[][] {
{ "去滑雪", "吃火锅", "回到宿舍看电影", "玩微博" },
{ "买牛奶,早早餐","吃土司和面包","吃橘子","出去跑步","看电影" },
{ "送苹果,吃苹果","玩LOL","和朋友聊天","学英语" },
{ "早起哦,买早餐","玩跑跑卡丁车","做午餐","吃螃蟹","睡觉啦" },
{ "出去兜风","准备去买菜","去钓鱼","爬山","回家吃饭辣" }};
groupList = new ArrayList<ParentEntity>();
for (int i = 0; i < strArray.length; i++) {
ParentEntity parentEntity = new ParentEntity();
parentEntity.setGroupName(strArray[i]);
List<ChildEntity> childList = new ArrayList<ChildEntity>();
for (int j = 0; j < childTimeArray[i].length; j++) {
ChildEntity childEntity = new ChildEntity();
childEntity.setDoingSomThing(childTimeArray[i][j]);
childList.add(childEntity);
}
parentEntity.setChildList(childList);
groupList.add(parentEntity);
}
return groupList;
}
}
package com.zihao.adapter;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.zihao.R;
import com.zihao.entity.ChildEntity;
import com.zihao.entity.ParentEntity;
public class MyExpandAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater = null;
private List<ParentEntity> groupList;
/**
* 构造方法
*
* @param context
* @param oneList
*/
public MyExpandAdapter(Context context,
List<ParentEntity> group_list) {
this.groupList = group_list;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
/**
* 返回一级Item总数
*/
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groupList.size();
}
/**
* 返回二级Item总数
*/
@Override
public int getChildrenCount(int groupPosition) {
if (groupList.get(groupPosition).getChildList() == null) {
return 0;
} else {
return groupList.get(groupPosition).getChildList().size();
}
}
/**
* 获取一级Item内容
*/
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupList.get(groupPosition);
}
/**
* 获取二级Item内容
*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return groupList.get(groupPosition).getChildList().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
GroupViewHolder holder = new GroupViewHolder();
if (convertView == null) {
convertView = inflater.inflate(R.layout.parent_status_item, null);
}
holder.groupName = (TextView) convertView
.findViewById(R.id.one_status_name);
holder.groupName.setText(groupList.get(groupPosition).getGroupName());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder viewHolder = null;
ChildEntity entity = (ChildEntity) getChild(groupPosition,
childPosition);
if (convertView != null) {
viewHolder = (ChildViewHolder) convertView.getTag();
} else {
viewHolder = new ChildViewHolder();
convertView = inflater.inflate(R.layout.child_status_item, null);
viewHolder.dosome = (TextView) convertView
.findViewById(R.id.dosome);
}
viewHolder.dosome.setText(entity.getDoingSomThing());
convertView.setTag(viewHolder);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
private class GroupViewHolder {
TextView groupName;
}
private class ChildViewHolder {
public TextView dosome;
}
}
两个实体类:
package com.zihao.entity;
/**
* 二级Item实体类
*
* @author zihao
*
*/
public class ChildEntity {
private String dosomething;
public String getDoingSomThing() {
return dosomething;
}
public void setDoingSomThing(String dosomething) {
this.dosomething = dosomething;
}
}
package com.zihao.entity;
import java.util.List;
/**
* 一级Item实体类
*
* @author zihao
*
*/
public class ParentEntity {
private String groupName;
/** 二级Item数据列表 **/
private List<ChildEntity> childList;
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public List<ChildEntity> getChildList() {
return childList;
}
public void setChildList(List<ChildEntity> childList) {
this.childList = childList;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ededed" >
<TextView
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp"
android:background="#ff00ff00"
android:text="2016年1月4日"
android:textColor="@android:color/black" />
<View
android:id="@+id/top_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/title_text"
android:background="@color/head_line_bg" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/top_line" >
<View
android:id="@+id/group_tiao"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_marginLeft="55dp"
android:background="@color/time_line_bg" />
<TextView
android:id="@+id/time_axis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="70dp"
android:background="#aa00ff00"
android:text="time axis"
android:textColor="@android:color/white"
android:textSize="20dp" />
<ExpandableListView
android:id="@+id/expandlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/time_axis"
android:cacheColorHint="#00000000"
android:divider="@null" />
</RelativeLayout>
</RelativeLayout>
<?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:gravity="center_vertical" >
<TextView
android:id="@+id/dosome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="70dp"
android:layout_marginTop="6dp"
android:textColor="#999999" />
</LinearLayout>
<?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:gravity="center_vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="45dp"
android:layout_marginRight="5dp"
android:background="@drawable/blue"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/one_status_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>