假设您已经以某种方式结构化了数据,如下所示:
ArrayList where
Parent {name:String, checked:boolean, children:ArrayList} and
Child {name:String}
如果是这样,您只需要完成以下两个步骤:
- 为父项渲染器和子项渲染器创建适当的布局
- 扩展BaseExpandableListAdapter来自行构建列表。
这里是我心中的一个小例子:
layout/grouprow.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:orientation="horizontal"
android:gravity="fill" android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="@+id/parentname" android:paddingLeft="5px"
android:paddingRight="5px" android:paddingTop="3px"
android:paddingBottom="3px" android:textStyle="bold" android:textSize="18px"
android:layout_gravity="fill_horizontal" android:gravity="left"
android:layout_height="wrap_content" android:layout_width="wrap_content" />
<CheckBox android:id="@+id/checkbox" android:focusable="false"
android:layout_alignParentRight="true" android:freezesText="false"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="5px" />
</RelativeLayout>
layout/childrow.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="0px">
<TextView android:id="@+id/childname" android:paddingLeft="15px"
android:paddingRight="5px" android:focusable="false" android:textSize="14px"
android:layout_marginLeft="10px" android:layout_marginRight="3px"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
MyELAdapter类: 我将其声明为MyExpandableList的内部类,这样我就可以直接访问父列表,而无需将其作为参数传递。
private class MyELAdapter extends BaseExpandableListAdapter
{
private LayoutInflater inflater;
public MyELAdapter()
{
inflater = LayoutInflater.from(MyExpandableList.this);
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parentView)
{
final Parent parent = parents.get(groupPosition);
convertView = inflater.inflate(R.layout.grouprow, parentView, false);
((TextView) convertView.findViewById(R.id.parentname)).setText(parent.getName());
CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
checkbox.setChecked(parent.isChecked());
checkbox.setOnCheckedChangeListener(new CheckUpdateListener(parent));
if (parent.isChecked())
convertView.setBackgroundResource(R.color.red);
else
convertView.setBackgroundResource(R.color.blue);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parentView)
{
final Parent parent = parents.get(groupPosition);
final Child child = parent.getChildren().get(childPosition);
convertView = inflater.inflate(R.layout.childrow, parentView, false);
((TextView) convertView.findViewById(R.id.childname)).setText(child.getName());
return convertView;
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return parents.get(groupPosition).getChildren().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition)
{
return parents.get(groupPosition).getChildren().size();
}
@Override
public Object getGroup(int groupPosition)
{
return parents.get(groupPosition);
}
@Override
public int getGroupCount()
{
return parents.size();
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public void notifyDataSetChanged()
{
super.notifyDataSetChanged();
}
@Override
public boolean isEmpty()
{
return ((parents == null) || parents.isEmpty());
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
@Override
public boolean hasStableIds()
{
return true;
}
@Override
public boolean areAllItemsEnabled()
{
return true;
}
}
你必须已经有一个公共类 MyExpandableList extends ExpandableListActivity
,它具有成员:
private ArrayList<Parent> parents;
在给该成员变量赋值/加载其父级列表后,您还应该将适配器附加到此视图:
this.setListAdapter(new MyELAdapter())
就是这样了。你现在有一个可勾选、可展开的列表,而在CheckUpdateListener
的onCheckedChanged(CompoundButton buttonView, boolean isChecked)
方法中,你可以更新父对象的选中状态。
请注意,在getGroupView方法中确定了背景颜色,所以你不必在任何地方更改它,只需在需要时调用适配器的notifyDataSetChanged()
方法即可。
更新
你可以从此链接下载示例源代码。这是一个eclipse项目,但如果你使用其他开发环境,只需简单地复制必要的源文件(java + xml)即可。
getChildView
方法,并检查父项是否需要单选或多选子项。还需要创建两个childrow.xml文件,一个用于radiobuttons,另一个用于复选框。在数据结构层面上实现这一点最清晰的方式可能是拥有一个抽象的AParent
类(具有当前Parent
的功能),并基于它创建SingleSelectParent和MultiSelectParent扩展类。其中一个将有一个新成员:selectedIndex:int,而MultiSelectParent将有一个selectedIndices:ArrayList<int>... - rekaszeruAParent
后,你需要填充相应的 childrow.xml,并根据 Child 实例的 selected 成员设置 checked/selected 值。 - rekaszeru