【问题标题】:Hide/Unhide Imageview of children in a group on child click Expandablelistview隐藏/取消隐藏子级单击 Expandablelistview 组中子级的 Imageview
【发布时间】:2013-10-05 12:29:28
【问题描述】:

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

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

我的代码如下:

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

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

【问题讨论】:

  • 为什么需要遍历项目。 v 不是已经有被点击的行了吗?
  • 也许我现在明白你想要做什么了。让我知道这是否正确。如果点击了未选中的展开项目,您希望选中该项目并取消选中组中的所有其他项目。
  • @JimRhodes 是的,你是对的。

标签: android hide android-imageview expandablelistview children


【解决方案1】:

试试这个:

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

【讨论】:

  • 这只是点击行中图像的切换状态。我还需要在该行的其他兄弟姐妹上保留隐藏复选框图像。你知道我怎样才能做到这一点吗?
  • @anum90 v 是被点击的行。您的循环在循环的每次迭代中获取同一行的复选框。因此,当i 不等于childPosition 时,您将使其不可见。
  • 哇宾果游戏!我太愚蠢了,我只是在同一个视图上循环,这就是为什么我不能改变其他人的状态。非常感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多