【问题标题】:ExpandableListView - hide indicator for groups with no childrenExpandableListView - 为没有孩子的组隐藏指示器
【发布时间】:2011-05-07 21:39:27
【问题描述】:

ExpandableListView 中,有没有办法为没有孩子的组隐藏组指示符?

【问题讨论】:

  • 澄清一下:有没有办法只为没有孩子的组隐藏组指示符?

标签: android android-listview expandablelistview


【解决方案1】:

即使小组有孩子,此处发布的答案也会使您的小组指示器消失。

要解决这个问题,请覆盖组指示器。

首先,将您的组指示符设置为空:

    ExpandableListView.setGroupIndicator(null);

然后,转到包含标题的 xml,并添加一个带有可绘制对象的 ImageView。

之后,转到您的 ExpandableListAdapter.java,然后在“getGroupView”部分执行以下操作:

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

    TextView lblListHeader = convertView
            .findViewById(R.id.lblListHeader); // this is your header

    ImageView groupIndicator = convertView.findViewById(R.id.group_indicator); // this is the ImageView included in your xml to override the indicator

    if (getChildrenCount(groupPosition) == 0) {
        convertView.setVisibility(View.GONE);
        lblListHeader.setVisibility(View.GONE);
        convertView.setPadding(0,0,0,0); //try not to include padding on your xml, and instead define the padding here.

    } else {
        convertView.setVisibility(View.VISIBLE);
        convertView.setPadding(8,8,8,0);
        lblListHeader.setVisibility(View.VISIBLE);
        lblListHeader.setTypeface(font);
        lblListHeader.setText(headerTitle);
        if (isExpanded) { //this is the part where we define our group indicator based on the expanded status of the adapter
            groupIndicator.setImageResource(R.drawable.group_indicator_expanded);
        } else {
            groupIndicator.setImageResource(R.drawable.group_indicator_empty);
        }
    }

    return convertView;
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 如何使用默认可绘制对象?它说:无法解析符号'group-up'
    【解决方案3】:

    只是想改进 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"/>
    

    希望对您有所帮助...记得点个赞

    【讨论】:

      【解决方案4】:

      试试这个>>>

      适用于所有项目

       getExpandableListView().setGroupIndicator(null);
      

      在xml中

      android:groupIndicator="@null"
      

      【讨论】:

      • 我相信这将为所有项目设置 groupIndicator 而不仅仅是那些没有孩子的项目。
      • 经过测试,无法正常工作。这个简单的隐藏所有指标要么有孩子,要么没有。
      • myExpandableListView.setGroupIndicator(null); 适合我
      • @ericosg 有没有办法隐藏没有孩子的组的指示器??
      • 它隐藏了所有的指标,而不是没有孩子的指标。
      【解决方案5】:

      convertView.setVisibility(View.GONE) 应该可以解决问题。

      @Override
      public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
          DistanceHolder holder;
          if (convertView == null) {
              convertView = LayoutInflater.from(context).inflate(R.layout.list_search_distance_header, parent, false);
              if (getChildrenCount(groupPosition)==0) {
                  convertView.setVisibility(View.GONE);
              }
      

      【讨论】:

      • 这只是删除了所有项目:列表为空 --> 我很伤心 :(
      • 您添加了计数器检查吗? if (getChildrenCount(groupPosition)==0) {
      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

        android:groupIndicator 属性采用状态启用的可绘制对象。即可以为不同的状态设置不同的图像。

        当组没有孩子时,对应的状态是'state_empty'

        查看这些参考链接:

        thisthis

        对于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));
        

        我已经测试过它可以正常工作。

        【讨论】:

        • 不工作:在 Android 4.0.2 (ICS) 中测试。请参阅此页面上对此问题的其他回复。似乎Android认为未扩展的组为空。只需将组指示器设置为透明,然后将 imageView 添加到您的组行并在适配器的 getGroupView 方法中,将其设置为您想要的图标。
        • 此代码对我也不起作用(在 Android 4.4 上测试)。未扩展组被视为空(无子),因此图标设置为彩色/透明。
        • 感谢代码,但它看起来像个玩笑:这么简单的任务需要这么复杂的代码?
        • 很难相信这个答案收到了这么多“有用”和赏金,而据记录,出于性能原因,折叠的组被认为是空的!
        【解决方案8】:

        这可能是 XML 的另一种方式,设置android:groupIndicator="@null"

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

        【讨论】:

          【解决方案9】:

          您只需为隐藏组标题创建一个 height=0 的新 xml 布局。 例如,它是 'group_list_item_empty.xml'

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

          那么你的正常组头布局是'your_group_list_item_file.xml'

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                        android:layout_width="match_parent"
                        android:layout_height="48dp"
                        android:orientation="horizontal">
              ...your xml layout define...
          </LinearLayout>
          

          最后,您只需更新适配器类中的 getGroupView 方法:

          public class MyExpandableListAdapter extends BaseExpandableListAdapter{   
          
              //Your code here ...
          
              @Override
              public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
                  if (Your condition to hide the group header){
                      if (convertView == null || convertView instanceof LinearLayout) {
                          LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                          convertView = mInflater.inflate(R.layout.group_list_item_empty, null);
                      }           
                      return convertView;
                  }else{      
                      if (convertView == null || convertView instanceof RelativeLayout) {
                          LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                          convertView = mInflater.inflate(R.layout.your_group_list_item_file, null);              
                      }   
                      //Your code here ...
                      return convertView;
                  }
              }
          }
          

          重要:布局文件的根标签(隐藏和普通)必须不同(如上例,LinearLayout 和RelativeLayout)

          【讨论】:

            【解决方案10】:

            建议你我的解决方案:

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

            【讨论】:

            【解决方案11】:

            在您的代码中,只需将自定义 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

            【讨论】:

              【解决方案12】:

              根据 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 完成的。

              此外,这种方法还可以在默认情况下扩展组时显示正确的指示符,哪些内容不适用于博客中的代码。

              【讨论】:

              • 这意味着在您的BaseExpandableListAdapter 实现的getGroupView() 方法中使用。看看这个example implementation
              • @jenzz 什么是“指标”?
              • 他使用ViewHolder 模式。 indicator 是视图持有者的变量名。
              • indicator 是一个Imageview。根据状态要显示的图像
              • indicator 指 group_row xml 中的 explist_indicator Imageview:
              【解决方案13】:

              使用它对我来说非常有用。

              <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 代码吗?这对我不起作用(group_indicator 出现在有和没有孩子的组中)。
              【解决方案14】:

              正如另一个答案中提到的,由于 Android 将未展开的列表组视为空,因此即使该组有子项,也不会绘制图标。

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

              基本上,您必须将默认可绘制对象设置为透明,将可绘制对象作为 ImageView 移动到您的组视图中,并在适配器中切换图像。

              【讨论】:

                猜你喜欢
                • 2012-08-24
                • 1970-01-01
                • 1970-01-01
                • 2014-06-24
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-06-06
                相关资源
                最近更新 更多