0

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

在此处输入图像描述

问题是,当我点击一个孩子时,它不会隐藏/取消隐藏被点击孩子的复选标记图像。

我的代码如下:

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="medium Text"
        android:layout_alignParentLeft="true" 

        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:visibility="invisible"
    android:src="@drawable/checkmark" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/childname"
    android:layout_marginRight="53dp"
    android:layout_toLeftOf="@+id/checkmark"
    android:src="@drawable/splitthehalfbutton" />

</RelativeLayout>

在 childclick 监听器上

@Override
public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

    CreatePizzaAdapter adapter = (CreatePizzaAdapter) parent.getExpandableListAdapter();

    for (int i = 0; i < adapter.getChildernCount().get(groupPosition); i++) {

        ImageView checkbox = (ImageView) v.findViewById(R.id.checkmark);

        if(i == childPosition)//toggle hidden
        {
            if(checkbox.getVisibility() == View.INVISIBLE)
                    checkbox.setVisibility(View.VISIBLE);
            else
                checkbox.setVisibility(View.INVISIBLE);
        }
        else//keep others hidden
        {
            checkbox.setVisibility(View.INVISIBLE);
        }
    }

    return true;
}

我不知道我做错了什么...请帮助我..谢谢。如果您需要任何其他代码,请告诉我。

4

1 回答 1

0

尝试这个:

@Override
public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id)
{
  int iCount;
  int iIdx;
  Object item;

  CreatePizzaAdapter adapter = (CreatePizzaAdapter) parent.getExpandableListAdapter();

  iCount = adapter.getChildrenCount(groupPosition);
  for ( iIdx = 0; iIdx < iCount; ++iIdx )
  {
    item = adapter.getChild(groupPosition, iIdx);
    if ( item != null )
    {
      // Here you would cast item to the appropriate type for this row

      if ( iIdx == childPosition )
      {
        // Here you would toggle checked state in the data for this item
      }
      else
      {
        // Here you would clear checked state in the data for this item
      }
    }
  }
  parent.invalidateViews(); // Update the list view
}
于 2013-10-05T12:52:31.743 回答