我正在尝试实现一种方案,其中用户将在组中的许多选项中选择一个选项。见下图:
在上图中,在名为 Sauce 的组中,有3个选项。用户只需检查其中的1个选项。例如如果用户先前选择了“热门”。之后,他点击“温和”然后检查必须从“热”消失并出现在“温和”。我希望你明白这一点。
要实现此方法,我需要引用a group's childviews
或individual checks
,以便我可以切换我的复选标记。我真的不知道该做什么......请帮助我。感谢..
childlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dip" >
<TextView
android:id="@+id/childname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_alignParentLeft="true"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/checkmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/childname"
android:layout_alignParentRight="true"
android:layout_marginRight="22dp"
android:background="@android:color/transparent"
android:src="@drawable/checkmark" />
</RelativeLayout>
Child OnClick Listener
list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//how to get reference to other children here to toggle checkmark of current as well as previously tapped child's checkmark?
return false;
}
});
答案 0 :(得分:2)
在getChildView
中创建所有子项时,我使用了一个hashmap来存储所有子项的引用。在我的自定义适配器中,我声明:
private HashMap<String, View> childviewReferences = new HashMap<String, View>();
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childStr = (String) getChild(groupPosition, childPosition);
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//context.getLayoutInflater();
convertView = inflator.inflate(R.layout.childlayout, null);
TextView textview = (TextView) convertView.findViewById(R.id.childname);
textview.setText(childStr);
childviewReferences.put(Integer.toString(groupPosition) + Integer.toString(childPosition), convertView);
return convertView;
}
Child OnClick Listener
list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
CreatePizzaAdapter adapter = (CreatePizzaAdapter) parent.getExpandableListAdapter();
View conv = adapter.getChildviewReferences().get(Integer.toString(groupPosition) + Integer.toString(childPosition));
//now you can find any view and child and do anything
return true;
}
});