您的位置:首页 > 其它

Expand 二级目录集合实现的

2017-05-03 21:01 176 查看
public class MainActivity extends Activity {

private ExpandableListView pand;
private ArrayList<Bean> beans;
private ArrayList<childBean> childB;
private MyExpand expand;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pand = (ExpandableListView) findViewById(R.id.pand);

// 添加集合
beans = new ArrayList<Bean>();
childB = new ArrayList<childBean>();

childBean ch1 = new childBean(R.drawable.ic_launcher, "一点儿都不好玩儿");
childBean ch2 = new childBean(R.drawable.ic_launcher, "你在骗人");
childBean ch3 = new childBean(R.drawable.ic_launcher, "我要回家");
childB.add(ch3);
childB.add(ch2);
childB.add(ch1);

Bean bean1 = new Bean("我的好友", childB);
Bean bean2 = new Bean("我的好友", childB);
Bean bean3 = new Bean("我的好友", childB);
Bean bean4 = new Bean("我的好友", childB);
Bean bean5 = new Bean("我的好友", childB);
beans.add(bean5);
beans.add(bean4);
beans.add(bean3);
beans.add(bean2);
beans.add(bean1);

expand = new MyExpand();
pand.setAdapter(expand);

}

class MyExpand extends BaseExpandableListAdapter {

private TextView group;
private TextView child;

@Override
public int getGroupCount() {
return beans.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return beans.get(groupPosition).getList().size();
}

@Override
public Object getGroup(int groupPosition) {
return beans.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return beans.get(groupPosition).getList().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;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {

if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.group,
null);
group = (TextView) convertView.findViewById(R.id.group);
}
group.setText(beans.get(groupPosition).getName().toString());

return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(MainActivity.this, R.layout.child,
null);
child = (TextView) convertView.findViewById(R.id.child);
}
child.setText(beans.get(groupPosition).getList().get(childPosition)
.getText().toString());
return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: