0

我正在尝试实现一种场景,在该场景中,用户将在组中的许多选项中选择一个选项。见下图:

在此处输入图像描述

在上图中,在名为Sauce的组下,有 3 个选项。用户只需要检查其中的一个选项。例如,如果用户之前选择了“热”。之后,他点击“轻度”,然后检查必须从“热”中消失并出现在“轻度”上。我希望你明白这一点。

要实现这种方法,我需要参考a group's childviewsor individual checks以便我可以切换我的复选标记。我现在真的不知道该怎么办......请帮助我。谢谢..

子布局.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>

子 OnClick 侦听器

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;
                    }
                });
4

1 回答 1

2

我使用 hashmap 来存储在getChildView. 在我的自定义适配器中,我声明:

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;
    }

子 OnClick 侦听器

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;
                    }
                });
于 2013-10-05T10:57:25.490 回答