首页 > 移动开发 > ANDROID > ExpandableListView
ExpandableListView是ListView的子类,在普通的ListView上面进行扩张,把应用中的列表项分为几组,每组里又可以包含多个列表项
实现ExpandableListView的几种方法
扩展BaseExpandableListAdapter实现 ExpandableListView
使用Simple ExpandableListView将两个List包装成 ExpandableListView
使用SimpleCursorTreeAdapter将Cursor数据包装成 SimpleCursorTreeAdapter
常用的xml属性
android:childDivider
android:childIndicator
android:groupIndicator
举个例子: 扩展BaseExpandableListAdapter实现 ExpandableListView
那么关键就是,实现四个方法
getGroupCount:返回包含的组列表项的数量
int[] logos = new int[]
{
R.drawable.p,
R.drawable.z,
R.drawable.t
};
private String[] at = new String[]
{ "第一种", "第二种", "第三种"};
private String[][] arms = new String[][]
{
{ "11", "12", "13", "14" },
{ "21", "22", "23", "24" },
{ "31", "32" , "33" }
};
@Override
public int getGroupCount()
{
return at.length;
}
getGroupView:返回的View对象将作为组件列表项
@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent)
{
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setOrientation(0);
ImageView logo = new ImageView(MainActivity.this);
logo.setImageResource(logos[groupPosition]);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
getChildrenCount:返回特定组子列表项的数量
@Override
public int getChildrenCount(int groupPosition)
{
return arms[groupPosition].length;
}
getChildView:返回的View对象将作为特定组,特定位置的子列表项
@Override
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent)
{
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition)
.toString());
return textView;
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69917874/viewspace-2693270/ ,如需转载,请注明出处,否则将追究法律责任。