2010-11-10 60 views

回答

84

android:groupIndicator屬性需要啓用狀態drawable。也就是說,您可以爲不同的狀態設置不同的圖像。

在組一直沒有孩子,相應的狀態是「state_empty」

看到這些參考鏈接:

thisthis

state_empty,您可以設置不同的圖像這是不混淆,或者,只是使用透明的顏色來顯示沒有什麼...

添加此項目在你的有狀態drawable與其他...

​​3210

所以,你statelist可以是這樣的:

getExpandableListView().setGroupIndicator(getResources().getDrawable(R.drawable.my_group_statelist)); 

我:

<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測試了這個工作。

+31

不工作:測試Android 4.0.2(ICS)。請參閱此頁面上對此問題的其他回覆。看起來Android似乎將未擴展的組視爲空白。只需將組指示器設置爲透明,然後將imageView添加到組行以及適配器的getGroupView方法中,將其設置爲所需的圖標。 – Fraggle 2011-12-28 16:35:08

+0

@Fraggle:當我在2.2上測試時,它工作。 – 2011-12-28 18:24:56

+0

對我來說不適用於2.2 – morgwai 2013-08-07 11:57:59

33

基於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完成。

此外,此方法還會顯示正確的指標,當某個組默認展開時,哪些內容與博客中的代碼無效。

+0

我可以在哪裏應用此代碼? – Karthik 2013-09-26 06:28:22

+3

這意味着在BaseExpandableListAdapter實現的'getGroupView()'方法中使用。看看這個[示例實現](http://mylifewithandroid.blogspot.co.uk/2011/06/hiding-group-indicator-for-empty-groups.html)。 – jenzz 2013-09-26 11:34:43

+0

+1爲你的答案@Jeans我已經通過鏈接thnx ... – Karthik 2013-09-28 01:47:13

86

試試這個>>>

所有項目

getExpandableListView().setGroupIndicator(null); 

在XML

android:groupIndicator="@null" 
+28

我相信會爲所有項目設置groupIndicator,而不是沒有子項目。 – ericosg 2013-12-06 13:28:48

+4

經過測試,不起作用。這個簡單的隱藏所有指標或者是否有孩子。 – frusso 2015-02-14 21:11:52

+2

** myExpandableListView.setGroupIndicator(null); **適用於我 – 2016-04-20 09:28:12

0

使用此它是完全爲我工作。

<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> 
+0

你可以發佈你的ExpandableListView代碼嗎?這不適合我(group_indicator出現在有和沒有孩子的團體中)。 – 2013-12-03 22:25:01

+1

我已經看過這段代碼很多次了,不工作。 – Joseph 2014-04-17 17:02:51

9

在你的代碼,只需使用自定義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

2

建議你我的解決方案:

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

簡單地說,你創建一個高度= 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)

0

你有沒有試圖改變ExpandableListView的屬性android:groupIndicator="@null"

+0

並非適用於所有人羣。 – Ich 2016-01-05 02:02:36

-2

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); 
     } 
+2

這只是刪除所有項目:列表是空的 - >我很傷心:( – Ich 2016-01-05 02:04:44

+0

你有沒有添加計數器檢查?if(getChildrenCount(groupPosition)== 0){ – 2017-03-30 21:49:16

0

只是想改進Mihir Trivedi的答案。您可以在getGroupView把這個()這是MyExpandableListAdapter類

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"/> 

希望這有助於......記得留下一個upvote