我试着去弄清楚如何构建一个包含(很多)的一个观点:ANDROID - ExpandableListView
- PARENT1(可检查的,可扩展)
- CHILD1(单选按钮)
- CHILD2(单选按钮)
...
- PARENT2(可检查的,EXP andable)
- CHILD1(可检查的)
- CHILD2(可检查的)
...
的一点是,父母必须是可检查,并根据孩子有更改图标。有些人可以指引我走向正确的方向,因为从我发现的东西看来什么都不适合我。
我试着去弄清楚如何构建一个包含(很多)的一个观点:ANDROID - ExpandableListView
...
...
的一点是,父母必须是可检查,并根据孩子有更改图标。有些人可以指引我走向正确的方向,因为从我发现的东西看来什么都不适合我。
我认为,你有你的数据在某种程度上结构,如: ArrayList的地方
如果是的话,你只需要做两两件事:
下面是关于什么是我心中的小样本:
布局/ 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">
<!-- PARENT -->
<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>
布局/ 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">
<!-- CHILD -->
<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()
方法即可。
更新
你可以从this link下载示例源代码。这是一个eclipse项目,但是如果你使用其他开发环境,只需简单地复制必要的源文件(java + xml)即可。
thanx rekaszeru。既然我是一个begginer,你会不会喜欢分享一个工作样本? – no9 2011-04-13 08:21:38
当然,我只需要几分钟就可以建立一个。这将是有用的,但如果我知道你从哪里获得数据(服务器或本地)。 – rekaszeru 2011-04-13 08:53:00
如果可以的话,请建立一个非常简单的例子(只是一些测试数据与一堆数组)。我正在为紧凑框架树视图设计XML数据。我将不得不自己格式化数据,但在这个阶段我不知道如何。现在我对消耗性清单本身的结构感兴趣。复选框,单选按钮等 – no9 2011-04-13 09:41:39