1.布局文件中使用:<ExpandableListView
android:id="@+id/el"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ExpandableListView>
2.代码中使用
el = (ExpandableListView) findViewById(R.id.el);
el.setAdapter(new Myadapter());
3.adapter
private class Myadapter extends BaseExpandableListAdapter {
// 设置组的个数
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 8;
}
// 设置每组有多少孩子
// groupPosition : 组的位置标示
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition+1;
}
// 设置组的样式
//groupPosition : 组的位置
//isExpanded :是否可以扩展
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView textView = new TextView(getApplicationContext());
textView.setText(" 第"+groupPosition+"组");
return textView;
}
// 设置组的孩子的样式
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = new TextView(getApplicationContext());
textView.setText("我是第"+groupPosition+"组的第"+childPosition+"个孩子");
textView.setTextColor(Color.RED);
return textView;
}
// 获取组对应的数据
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
// 获取组的孩子对应的数据
// groupPosition : 组的位置
// childPosition : 孩子的位置
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
// 获取组的id
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
// 获取组的孩子的id
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
// 判断id是否稳定,判断有没有设置id,有true,没有false
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
// 设置孩子是否可以点击 true:可以,false:不可以
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
可扩展的listview
最新推荐文章于 2024-11-03 00:04:55 发布