ExpandableListView - 隐藏没有子项的组的指示符

Gra*_*tzi 104 android expandablelistview android-listview

在一个ExpandableListView,有没有办法隐藏没有孩子的群组指标?

Amt*_*t87 106

试试这个>>>

对于所有项目

 getExpandableListView().setGroupIndicator(null);
Run Code Online (Sandbox Code Playgroud)

在xml中

android:groupIndicator="@null"
Run Code Online (Sandbox Code Playgroud)

  • 我相信这会为所有项目设置groupIndicator,而不仅仅是那些没有孩子的项目. (38认同)
  • 经过测试,不起作用.这个简单隐藏所有指标要么有孩子,要么没有. (4认同)
  • **myExpandableListView.setGroupIndicator(null);**适合我 (2认同)

Sar*_*fan 89

android:groupIndicator属性采用启用状态的drawable.也就是说,您可以为不同的状态设置不同的图像.

当该组没有子节点时,相应的状态为"state_empty"

请参阅以下参考链接:

这个这个

因为state_empty,你可以设置一个不混淆的不同图像,或者只是使用透明颜色来显示任何内容......

在你的有状态的drawable中添加此项目以及其他人....

<item android:state_empty="true" android:drawable="@android:color/transparent"/>
Run Code Online (Sandbox Code Playgroud)

所以,你的州名单可以是这样的:

<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>
Run Code Online (Sandbox Code Playgroud)

如果您使用的是ExpandableListActivity,则可以在onCreate中设置groupindicator,如下所示:

getExpandableListView().setGroupIndicator(getResources().getDrawable(R.drawable.my_group_statelist));
Run Code Online (Sandbox Code Playgroud)

我测试过这个工作.

  • 不工作:在Android 4.0.2(ICS)中测试过.请在此页面上查看此问题的其他回复.似乎Android认为未扩展组为空.只需将组指示器设置为透明,然后将imageView添加到组行和适配器的getGroupView方法,将其设置为所需的图标. (35认同)
  • 很难相信这个答案收到了如此多的“有用”和赏金,而据记录,出于性能原因,折叠的组被认为是空的! (2认同)

jen*_*nzz 40

根据StrayPointer的答案和博客中的代码,您可以进一步简化代码:

在你的xml中添加以下的ExpandableListView:

android:groupIndicator="@android:color/transparent"
Run Code Online (Sandbox Code Playgroud)

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

@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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

通过使用setImageResource方法,您可以使用单行完成所有操作.您的适配器中不需要三个Integer数组.对于展开和折叠状态,您也不需要XML选择器.一切都是通过Java完成的.

此外,默认情况下扩展组时,此方法还会显示正确的指示符,这与博客中的代码无关.

  • @jenzz哪里是魔术变量"指标"? (5认同)
  • 这意味着要在`BaseExpandableListAdapter`实现的`getGroupView()`方法中使用.看看这个[示例实现](http://mylifewithandroid.blogspot.co.uk/2011/06/hiding-group-indicator-for-empty-groups.html). (4认同)
  • @jenzz什么是"指标"? (4认同)

Str*_*ter 26

正如在不同的答案中所提到的,由于Android将未展开的列表组视为空,即使该组具有子项,也不会绘制图标.

这个链接为我解决了这个问题:http: //mylifewithandroid.blogspot.com/2011/06/hiding-group-indicator-for-empty-groups.html

基本上,您必须将默认drawable设置为透明,将drawable作为ImageView移动到组视图中,并在适配器中切换图像.


Mih*_*edi 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
};
Run Code Online (Sandbox Code Playgroud)

同样在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;
}
Run Code Online (Sandbox Code Playgroud)

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


CoD*_*oDe 8

这可能是另一种来自XML的方法 android:groupIndicator="@null"

参考链接:https://stackoverflow.com/a/5853520/2624806


小智 6

在XML中

android:groupIndicator="@null"
Run Code Online (Sandbox Code Playgroud)

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);
      }
}
Run Code Online (Sandbox Code Playgroud)

  • 如何使用默认绘图?它说:无法解析符号“分组” (2认同)