ExpandableListView 可以实现双层列表效果。而且代码量不多,比较简单实用。
private Map> dataset = new HashMap>();
private ArrayList parentList;
private ArrayList childList;
public void initDatas() {
String mac;
parentList = new ArrayList();
parentList.add("343");
parentList.add("979");
initDataSet();
}
public void initDataSet() {
dataset.clear();
for (String info : parentList) {
childList = new ArrayList();
childList.add("17771861240");
childList.add("17771861241");
dataset.put(info, childList);
}
}
//初始化数据,每个parent对应一个childList,所以数据用HashMap 保存。主要就是Adapter 中的代码实现。
private SwipeLayout currentExpandedSwipeLayout;
BaseExpandableListAdapter expandAdapter = new BaseExpandableListAdapter() {
@Override
public int getGroupCount() {
return dataset.size();
}
@Override
public int getChildrenCount(int groupPosition) {
if (dataset.get(parentList.get(groupPosition)) == null) {
return 0;
}
return dataset.get(parentList.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return parentList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return dataset.get(parentList.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
//实现GroupView ,isExpanded GroupView 是否展开,因为在ExpandableListView 中展开的图标是默认在左边,所以我们要去掉默认的,然后自己定义一个,根据IsExpanded 来设置图标。setGroupIndicator(null) ,去掉默认的。
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(AccountManagerActivity.this).inflate(R.layout.group_layout, null);
}
convertView.setTag(R.layout.group_layout, groupPosition);
convertView.setTag(R.layout.expandlist_item, -1);
TextView text = (TextView) convertView.findViewById(R.id.group_title);
text.setText(parentList.get(groupPosition));
ImageView img = (ImageView) convertView.findViewById(R.id.group_img);
if (isExpanded) {
img.setBackgroundResource(R.drawable.icon_detail_down_button);
} else {
img.setBackgroundResource(R.drawable.icon_detail_pull_button);
}
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(AccountManagerActivity.this).inflate(R.layout.expandlist_item, null);
}
convertView.setTag(R.layout.group_layout, groupPosition);
convertView.setTag(R.layout.expandlist_item, childPosition);
TextView tv_num = (TextView) convertView.findViewById(R.id.tv_num);
TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
Button btn_delete = (Button) convertView.findViewById(R.id.ib_delete);
final SwipeLayout child = (SwipeLayout) convertView.findViewById(R.id.child);
tv_num.setText(getText(R.string.normal_user).toString() + (childPosition + 1));
final String phone = dataset.get(parentList.get(groupPosition)).get(childPosition);
tv_name.setText(phone);
//设置SwipeLayout 的模式。
child.setShowMode(SwipeLayout.ShowMode.PullOut);
//设置SwipeLayout 的模式。
child.setShowMode(SwipeLayout.ShowMode.PullOut);
//设置需要滑动出现的布局,要在XML 中给该布局加上TAG
child.addDrag(SwipeLayout.DragEdge.Right, child.findViewWithTag("button"));
child.addSwipeListener(new SwipeLayout.SwipeListener() {
@Override
public void onStartOpen(SwipeLayout layout) {
if (currentExpandedSwipeLayout != null && currentExpandedSwipeLayout != layout)
currentExpandedSwipeLayout.close(true);
}
@Override
public void onOpen(SwipeLayout layout) {
currentExpandedSwipeLayout = layout;
}
@Override
public void onStartClose(SwipeLayout layout) {
}
@Override
public void onClose(SwipeLayout layout) {
}
@Override
public void onUpdate(SwipeLayout layout, int leftOffset, int topOffset) {
}
@Override
public void onHandRelease(SwipeLayout layout, float xvel, float yvel) {
}
});
}
网友评论