no9*_*no9 9 checkbox android itemrenderer radio-button expandablelistview
我试图弄清楚如何构建一个包含(许多)的视图:
...
...
关键是父母必须是可以检查的,并且基于孩子必须更改图标.一些人可以指出我正确的方向,因为从我发现什么似乎没有什么对我有用.
rek*_*eru 17
我假设,你的数据以某种方式构建,例如:ArrayList where
如果是这样,你只需做两件事:
这是一个关于我的想法的小样本:
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">
<!-- 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>
Run Code Online (Sandbox Code Playgroud)
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">
<!-- 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>
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
您必须已经拥有一个MyExpandableList extends ExpandableListActivity拥有成员的公共类:
private ArrayList<Parent> parents;
Run Code Online (Sandbox Code Playgroud)
为此成员分配值/加载父项列表后,还应将适配器附加到此视图:
this.setListAdapter(new MyELAdapter());
Run Code Online (Sandbox Code Playgroud)
就是这样.你有一个可检查的,可扩展的名单,并在CheckUpdateListener的onCheckedChanged(CompoundButton buttonView, boolean isChecked)方法,你可以更新你的父对象的选中状态.
请注意,背景颜色是在getGroupView方法中确定的,因此您无需在任何地方更改它,只需在notifyDataSetChanged()需要时调用适配器的方法.
更新
您可以从此链接下载示例源代码.这是一个eclipse项目,但如果你正在使用其他开发人员环境,只需复制必要的源文件(java + xml).
| 归档时间: |
|
| 查看次数: |
11923 次 |
| 最近记录: |