可扩展列表视图 - 隐藏没有子项的组指示器

116
ExpandableListView 中,有没有一种方法可以隐藏没有子项的组的组指示器?

1
澄清一下:是否有办法仅针对没有子项的群组隐藏群组指示器? - AlleyOOP
13个回答

119

尝试这个 >>>

对于所有项目

 getExpandableListView().setGroupIndicator(null);
在xml中
android:groupIndicator="@null"

43
我认为这将设置所有项目的groupIndicator,而不仅仅是那些没有子项的项目。 - ericosg
4
测试过了但不起作用。这个简单的代码可以隐藏所有有或者没有子级的指示器。 - frusso
2
我的ExpandableListView.setGroupIndicator(null);对我很有效。 - Ranjithkumar
1
它隐藏了所有的指标,而不是那些没有子级的指标。 - Sam
是的@SamiraElhami,我已经注意到了,但不幸的是我认为问题是在谈论所有指标,我没有看到“无子项”约束条件。因此,我通过提到它适用于所有人来编辑了我的答案。 - Amt87
显示剩余3条评论

88
android:groupIndicator属性需要一种启用状态的可绘制对象。也就是说,您可以为不同的状态设置不同的图像。 当组没有子项时,对应的状态是“state_empty”。 请参阅以下参考链接: 这个这个 对于state_empty,您可以设置一个不会引起混淆的不同图像,或者只需使用透明颜色来显示空白... 在您的状态可绘制对象中添加此项目以及其他项目...
<item android:state_empty="true" android:drawable="@android:color/transparent"/>

因此,您的状态列表可以如下所示:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_empty="true" android:drawable="@android:color/transparent"/>
    <item android:state_expanded="true" android:drawable="@drawable/my_icon_max" />
    <item android:drawable="@drawable/my_icon_min" />
</selector>

如果你正在使用 ExpandableListActivity,你可以在 onCreate 中设置 groupindicator 如下:

getExpandableListView().setGroupIndicator(getResources().getDrawable(R.drawable.my_group_statelist));
我已经测试过这个可以正常工作。

36
无法工作:在Android 4.0.2(ICS)中进行了测试。请参见此页面上的其他回答。似乎Android将未展开的组视为为空。只需将组指示器设置为透明,然后在组行中添加imageView,并在适配器的getGroupView方法中将其设置为所需的图标即可。 - Fraggle
1
这段代码对我也不起作用(在Android 4.4上测试过)。未展开的组被视为空(没有子项),因此图标设置为颜色/透明。 - Guicara
1
谢谢你的代码,但看起来像个笑话:为了如此简单的任务而写出如此复杂的代码? - Antonio Sesto
2
很难相信这个答案在文档中记录折叠组由于性能原因被视为空而收到了如此多的“有用”回复和赏金! - 3c71

45

根据StrayPointer的回答和博客中的代码,您甚至可以简化代码:

在您的xml文件中为ExpandableListView添加以下内容:

android:groupIndicator="@android:color/transparent"

然后在适配器中执行以下操作:

@Override
protected void bindGroupView(View view, Context paramContext, Cursor cursor, boolean paramBoolean){
    **...**

    if ( getChildrenCount( groupPosition ) == 0 ) {
       indicator.setVisibility( View.INVISIBLE );
    } else {
       indicator.setVisibility( View.VISIBLE );
       indicator.setImageResource( isExpanded ? R.drawable.list_group_expanded : R.drawable.list_group_closed );
    }
}

通过使用setImageResource方法,您可以通过一条语句完成所有操作。 在适配器中,您不需要三个整数数组。您也不需要XML选择器来设置展开和折叠状态。所有这些都可以通过Java完成。

此外,这种方法还会在默认情况下正确显示组扩展时的指示器,而该博客中的代码无法实现此功能。


5
这是用于您的BaseExpandableListAdapter实现中的getGroupView()方法。请查看此示例实现 - jenzz
5
“Indicator”是什么意思? - IBunny
他使用了 ViewHolder 设计模式。indicator 是视图持有者的变量名。 - Guicara
指示器是一个ImageView。根据状态显示您想要显示的图像。 - Ojonugwa Jude Ochalifu
指示器是指group_row xml中的explist_indicator Imageview:<?xml?> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="fill_parent"> <ImageView android:layout_height="20px" android:layout_width="20px" android:id="@+id/explist_indicator" android:src="@drawable/expander_group"/> <TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/groupname" android:paddingLeft="30px" android:textStyle="bold" android:textSize="16px"/> </LinearLayout> - Razvan_TK9692

26

13

XML 中

android:groupIndicator="@null"

ExpandableListAdapter --> getGroupView 中复制以下代码

if (this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).size() > 0){
      if (isExpanded) {
          arrowicon.setImageResource(R.drawable.group_up);
      } else {
          arrowicon.setImageResource(R.drawable.group_down);
      }
}

1
如何使用默认的Drawable?它说:“无法解析符号'group-up'”。 - StNickolay

12

在您的代码中,只需使用自定义 XML 用于组列表,并在其中放置 ImageView 来作为 GroupIndicator

并在您的 ExpandableListAdapter 中添加以下数组。

private static final int[] EMPTY_STATE_SET = {};
private static final int[] GROUP_EXPANDED_STATE_SET = { android.R.attr.state_expanded };
private static final int[][] GROUP_STATE_SETS = { EMPTY_STATE_SET, // 0
GROUP_EXPANDED_STATE_SET // 1
};

同样在ExpandableListAdapter的方法中添加以下内容

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
{ 
    if (convertView == null) 
    {
        LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.row_group_list, null);
    }

    //Image view which you put in row_group_list.xml
    View ind = convertView.findViewById(R.id.iv_navigation);
    if (ind != null)
    {
        ImageView indicator = (ImageView) ind;
        if (getChildrenCount(groupPosition) == 0) 
        {
            indicator.setVisibility(View.INVISIBLE);
        } 
        else 
        {
            indicator.setVisibility(View.VISIBLE);
            int stateSetIndex = (isExpanded ? 1 : 0);
            Drawable drawable = indicator.getDrawable();
            drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
        }
    }

    return convertView;
}

参考: http://mylifewithandroid.blogspot.in/2011/06/hiding-group-indicator-for-empty-groups.html


4
建议您我的解决方案:
1)清除默认的groupIndicator:
<ExpandableListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"       
    android:layout_width="wrap_content"
    android:layout_height="240dp"        
    android:layout_gravity="start"
    android:background="#cccc"  
    android:groupIndicator="@android:color/transparent"     
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"        
    android:dividerHeight="0dp"  
     />

2) 在ExpandableAdapter中:

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = new TextView(context);
    }
    ((TextView) convertView).setText(groupItem.get(groupPosition));     
    ((TextView) convertView).setHeight(groupHeight);
    ((TextView) convertView).setTextSize(groupTextSize);

    //create groupIndicator using TextView drawable
    if (getChildrenCount(groupPosition)>0) {
        Drawable zzz ;
        if (isExpanded) {
            zzz = context.getResources().getDrawable(R.drawable.arrowup);
        } else {
            zzz = context.getResources().getDrawable(R.drawable.arrowdown);
        }                               
        zzz.setBounds(0, 0, groupHeight, groupHeight);
        ((TextView) convertView).setCompoundDrawables(null, null,zzz, null);
    }       
    convertView.setTag(groupItem.get(groupPosition));       

    return convertView;
}

@https://stackoverflow.com/users/4090796/evgeny-karavashkin 什么是 "arrowup"? - StNickolay

2

我想改进Mihir Trivedi的答案。您可以将其放置在MyExpandableListAdapter类中的getGroupView()中。

    View ind = convertView.findViewById(R.id.group_indicator);
    View ind2 = convertView.findViewById(R.id.group_indicator2);
    if (ind != null)
    {
        ImageView indicator = (ImageView) ind;
        if (getChildrenCount(groupPosition) == 0)
        {
            indicator.setVisibility(View.INVISIBLE);
        }
        else
        {
            indicator.setVisibility(View.VISIBLE);
            int stateSetIndex = (isExpanded ? 1 : 0);

            /*toggles down button to change upwards when list has expanded*/
            if(stateSetIndex == 1){
                ind.setVisibility(View.INVISIBLE);
                ind2.setVisibility(View.VISIBLE);
                Drawable drawable = indicator.getDrawable();
                drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
            }
            else if(stateSetIndex == 0){
                ind.setVisibility(View.VISIBLE);
                ind2.setVisibility(View.INVISIBLE);
                Drawable drawable = indicator.getDrawable();
                drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
            }
        }
    }

关于布局视图,这是我的group_items.xml的外观

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/group_heading"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="20dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:textSize="15sp"
    android:textStyle="bold"/>

<ImageView
    android:id="@+id/group_indicator"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/arrow_down_float"
    android:layout_alignParentRight="true"
    android:paddingRight="20dp"
    android:paddingTop="20dp"/>

<ImageView
    android:id="@+id/group_indicator2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/arrow_up_float"
    android:layout_alignParentRight="true"
    android:visibility="gone"
    android:paddingRight="20dp"
    android:paddingTop="20dp"/>

希望这有所帮助...记得点赞。

1
使用这个,对我来说完全有效。
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/group_indicator_expanded" android:state_empty="false" android:state_expanded="true"/>
<item android:drawable="@drawable/group_indicator" android:state_empty="true"/>
<item android:drawable="@drawable/group_indicator"/>

</selector>

你能把你的可展开列表 ExpandableListView 代码也发一下吗?我的无法正常工作(组指示器在有子项和无子项的组中都出现了)。 - user153275

1

你尝试过更改 ExpandableListView 的属性 android:groupIndicator="@null" 吗?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接