2013-08-03 25 views
2

我正在使用可擴展列表視圖,用戶可以選擇數據。Android默認組指標ExpandableListView

用戶可以選擇組或孩子。爲了便於2之間的區別,我有2個選項單選按鈕組:

  • 選擇子:正常擴展列表(可能擴大 組,然後選擇孩子)
  • 選擇組:所有組已摺疊,並且用戶無法展開組

我需要的是在第二種情況下隱藏組指示符,然後在選擇第一個選項時將其恢復。

這裏是我的代碼:

rgLink.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if(checkedId == R.id.rb_link_subject){ //Here is the child mode 
       mLinkType = LINK_SUBJECT; 
       //elvSubject.setGroupIndicator(/*Here I need the default group indicator*/); 
      } 
      else{         //Here is the group mode 
       collapseAllChildren(); 
       //The line below hide the group indicator 
       elvSubject.setGroupIndicator(null); 
       mLinkType = LINK_CATEGORY; 
      } 
     } 
    }); 

我還使用,爲集團項目:

style="?android:attr/listSeparatorTextViewStyle" 

所以基本上,我只需要一個行代碼來恢復默認分組指示。我怎樣才能做到這一點 ?

回答

9

您可以從當前主題的屬性:

//obtain expandableListViewStyle from theme 
TypedArray expandableListViewStyle = context.getTheme().obtainStyledAttributes(new int[]{android.R.attr.expandableListViewStyle}); 
//obtain attr from style 
TypedArray groupIndicator = context.getTheme().obtainStyledAttributes(expandableListViewStyle.getResourceId(0,0),new int[]{android.R.attr.groupIndicator}); 
elvSubject.setGroupIndicator(groupIndicator.getDrawable(0)); 
expandableListViewStyle.recycle(); 
groupIndicator.recycle(); 
+0

這正是我一直在尋找。那麼我可以在需要時恢復組指標。 – FR073N

+0

你能解釋一下,你是怎麼知道組指標風格是0指數的?有沒有網站或文件? – FR073N

+2

'obtainStyledAttributes'返回一個'TypedArray',其中包含'attrs' [(link)](http://developer.android.com/reference/android/content/res/Resources.Theme。 HTML#obtainStyledAttributes(INT []))。在這兩種情況下,我們傳遞長度爲1的'attrs'數組,因此結果包含1個元素,並且此元素的索引爲0(它是數組中的索引,而不是資源ID)。 – esentsov