效果图:
主布局xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f7f7f7" >
<RelativeLayout
android:id="@+id/date_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/time_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="2014年10月"
android:textColor="@android:color/black" />
</RelativeLayout>
<View
android:id="@+id/top_line"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/date_layout"
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/courses_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="70dp"
android:layout_marginTop="10dp"
android:text="时光轴"
android:textColor="@android:color/black"
android:textSize="22dp" />
<ExpandableListView
android:id="@+id/expandlist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/courses_title"
android:cacheColorHint="#00000000"
android:divider="@null" />
</RelativeLayout>
</RelativeLayout>
item布局child_status_item
<?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/two_complete_time"
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>
item布局group_status_item
<?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_gravity="center_vertical"
android:layout_marginLeft="45dp"
android:layout_marginRight="5dp"
android:background="@drawable/icons_ok"
android:contentDescription="@string/app_name" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:orientation="vertical" >
<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>
</LinearLayout>
kotlin GroupStatusEntity 实体类
package com.example.ppg.timeline.entity
/**
* Created by ppg on 2017/12/18.
* 二级Item数据列表
*/
class GroupStatusEntity( var groupName:String, var childList:List<ChildStatusEntity>)
kotlin ChildStatusEntity实体类
package com.example.ppg.timeline.entity
/**
* Created by ppg on 2017/12/18.
* /** 预计完成时间 **/
* /** 是否已完成 **/
*/
data class ChildStatusEntity( var completeTime: String, var isfinished: Boolean)
adapter
package com.example.ppg.timeline.adapter;
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.example.ppg.timeline.R;
import com.example.ppg.timeline.entity.ChildStatusEntity;
import com.example.ppg.timeline.entity.GroupStatusEntity;
import java.util.List;
/**
* Created by ppg on 2017/12/18.
*/
public class StatusExpandAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater = null;
private List<GroupStatusEntity> groupList;
/**
* 构造方法
*
* @param context
* @param oneList
*/
public StatusExpandAdapter(Context context,
List<GroupStatusEntity> 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.group_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;
ChildStatusEntity entity = (ChildStatusEntity) getChild(groupPosition,
childPosition);
if (convertView != null) {
viewHolder = (ChildViewHolder) convertView.getTag();
} else {
viewHolder = new ChildViewHolder();
convertView = inflater.inflate(R.layout.child_status_item, null);
viewHolder.twoStatusTime = (TextView) convertView
.findViewById(R.id.two_complete_time);
}
viewHolder.twoStatusTime.setText(entity.getCompleteTime());
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 twoStatusTime;
}
}
主函数调用,MainAcvitity.java
package com.example.ppg.timeline;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import com.example.ppg.timeline.adapter.StatusExpandAdapter;
import com.example.ppg.timeline.entity.ChildStatusEntity;
import com.example.ppg.timeline.entity.GroupStatusEntity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ExpandableListView expandlistView;
private StatusExpandAdapter statusAdapter;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
expandlistView = (ExpandableListView) findViewById(R.id.expandlist);
initExpandListView();
}
/**
* 初始化可拓展列表
*/
private void initExpandListView() {
statusAdapter = new StatusExpandAdapter(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 ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO Auto-generated method stub
return true;
}
});
}
private List<GroupStatusEntity> getListData() {
List<GroupStatusEntity> groupList;
String[] strArray = new String[] { "12月18日", "12月17日", "12月16日" , "12月15日", "12月14日"};
String[][] childTimeArray = new String[][] {
{ "画江湖之换世门生" },
{ "天下黑白可争", "世间义理难断" },{ "唯留君之明月空悬"},
{ "纵横交错兮天下之局" }, { "谁能参悟兮世事如棋" } };
groupList = new ArrayList<GroupStatusEntity>();
for (int i = 0; i < strArray.length; i++) {
// GroupStatusEntity groupStatusEntity = new GroupStatusEntity(strArray[i],);
// groupStatusEntity.setGroupName(strArray[i]);
List<ChildStatusEntity> childList = new ArrayList<ChildStatusEntity>();
for (int j = 0; j < childTimeArray[i].length; j++) {
ChildStatusEntity childStatusEntity = new ChildStatusEntity(childTimeArray[i][j],true);
// childStatusEntity.setCompleteTime(childTimeArray[i][j]);
// childStatusEntity.setIsfinished(true);
childList.add(childStatusEntity);
}
GroupStatusEntity groupStatusEntity = new GroupStatusEntity(strArray[i],childList);
// groupStatusEntity.setChildList(childList);
groupList.add(groupStatusEntity);
}
return groupList;
}
}