2013-03-21 59 views
3

ExpandableListView中是否可以初始選擇一個子項目,以便包含的組被擴展並且列表滾動到該子項的位置?ExpandableListView中的子選擇

我認爲setSelectedChild會做的伎倆,但它不會給任何結果。

我用下面的代碼段進行測試:

public class MainActivity extends Activity { 

private static final String TITLE = "title"; 

@SuppressWarnings("serial") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    ExpandableListView listView = new ExpandableListView(this); 
    SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, 
     new ArrayList<Map<String,String>>() { 
      { 
       this.add(new HashMap<String, String>() { 
        { 
         this.put(TITLE, "Group 1"); 
        } 
       }); 
       this.add(new HashMap<String, String>() { 
        { 
         this.put(TITLE, "Group 2"); 
        } 
       }); 
       this.add(new HashMap<String, String>() { 
        { 
         this.put(TITLE, "Group 3"); 
        } 
       }); 
      } 
     }, 
     android.R.layout.simple_expandable_list_item_1, 
     new String[] { TITLE }, 
     new int[] { android.R.id.text1 }, 
     new ArrayList<List<? extends Map<String,String>>>() { 
      { 
       this.add(new ArrayList<Map<String,String>>() { 
        { 
         this.add(new HashMap<String, String>() { 
          { 
           this.put(TITLE, "Child 1-1"); 
          } 
         }); 
         this.add(new HashMap<String, String>() { 
          { 
           this.put(TITLE, "Child 1-2"); 
          } 
         }); 
        } 
       }); 
       this.add(new ArrayList<Map<String,String>>() { 
        { 
         this.add(new HashMap<String, String>() { 
          { 
           this.put(TITLE, "Child 2-1"); 
          } 
         }); 
         this.add(new HashMap<String, String>() { 
          { 
           this.put(TITLE, "Child 2-2"); 
          } 
         }); 
        } 
       }); 
       this.add(new ArrayList<Map<String,String>>() { 
        { 
         this.add(new HashMap<String, String>() { 
          { 
           this.put(TITLE, "Child 3-1"); 
          } 
         }); 
         this.add(new HashMap<String, String>() { 
          { 
           this.put(TITLE, "Child 3-2"); 
          } 
         }); 
        } 
       }); 
      } 
     }, 
     android.R.layout.simple_expandable_list_item_2, 
     new String[] { TITLE }, 
     new int[] { android.R.id.text1 } 
    ); 
    listView.setAdapter(adapter); 
    setContentView(listView); 

    listView.setSelectedChild(1, 1, true); 
} 
} 

回答

7

setSelectedChild()之前listView.expandGroup()添加呼叫。

setSelectedChild()shouldExpandGroup參數設置爲true似乎只有在至少有一個組在第一個位置展開時才起作用。

+0

就是這樣。謝謝 – fiddler 2013-03-21 12:29:24

+0

完美的解決方案.... – karn 2014-01-22 06:56:12

+1

爲什麼文檔沒有告訴'setSelectedChild()中的shouldExpandGroup參數設置爲true,只有在至少有一個組在第一個位置展開時才起作用。 – nmxprime 2014-06-05 08:57:03

0

在ExpandableListView是有可能首先選擇一個子項,以便含組展開和列表滾動到這個孩子的位置:

//select child and open it's group 
ExpListView.setSelectedChild(groupPos, childPos, true); 
//scroll to selected child 
ExpListView.smoothScrollToPosition(ExpListView.getFlatListPosition(ExpListView.getPackedPositionForChild(groupPos, childPos)));