Android常用开源项目(三十七)

来源:互联网 发布:微信数据采集软件 编辑:程序博客网 时间:2024/05/21 21:35

Android开发之使用ExpandableListView实现时间轴

1.首先看下运行效果吧

Android开发之使用ExpandableListView实现时间轴

运行效果图

2.代码结构图

Android开发之使用ExpandableListView实现时间轴

代码结构图

3.具体实现步骤

<1>首先定义一级实体类GroupStatusEntity.java

Android开发之使用ExpandableListView实现时间轴

<2>定义二级实体类ChildStatusEntity.java

Android开发之使用ExpandableListView实现时间轴

<3>定义ExpandableListView的适配器类StatusExpandAdapter.java

public class StatusExpandAdapter extends BaseExpandableListAdapter {

private LayoutInflater inflater = null;

private List<GroupStatusEntity> groupList;

/**

* 构造方法

*/

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;

}

}

<4>下面给出资源类的文件实现

首先是一级实体类的布局文件group_status_item.xml

Android开发之使用ExpandableListView实现时间轴

其次是二级实体类的布局文件child_status_item.xml

Android开发之使用ExpandableListView实现时间轴

其次是实现drawable文件中的资源文件list_line.xml

Android开发之使用ExpandableListView实现时间轴

原创粉丝点击