2012-04-09 68 views
5

我有一個ExpandableListView,我想在單擊組時記錄組位置。不幸的是,下面的代碼總是返回0,就好像我在第0組中點擊一樣。Android ExpandableListView組位置始終爲0

exList.setOnGroupClickListener(new OnGroupClickListener() { 

    @Override 
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
      groupPosition = ExpandableListView.getPackedPositionGroup(id); 

      Log.i("group position", groupPosition + ""); 
      return false; 
    } 

    }); 

我也對團體和孩子的其中工程權longclicklistener:

exList.setOnItemLongClickListener(new OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
      if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
       groupPosition = ExpandableListView.getPackedPositionGroup(id); 
... 
} 

任何想法?

+0

您是否使用ExpandableListView的自定義適配器? – Cata 2012-04-09 06:49:44

+0

是的。我的其他聽衆工作正確 – erdomester 2012-04-09 07:04:15

+0

好的,請確保您的getItem和getItemId方法返回有效的值,而不是0 ..(在您的自定義適配器),你也可以看看這個適配器http://developer.android.com/resources/ samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html – Cata 2012-04-09 07:15:13

回答

0

使用監聽器OnGroupExpandListener,onGroupExpand方法的參數是組在ExpandableListView的位置。

就像是:

listView = (ExpandableListView) findViewById(R.id.expandableListView); 
listView.setOnGroupExpandListener(new OnGroupExpandListener() { 
    @Override 
    public void onGroupExpand(int groupPosition) { 
     Log.d(TAG, "pos " + groupPosition); 
    } 
}); 
0
@Override 
public Object getGroup(int groupPosition) { 
    Log.i(TAG, "* getGroup : groupPosition =" + groupPosition); 

    return categories[groupPosition]; 
} 

當你伸出BaseExpandableListAdapter,您將收到上述重寫方法。上面的註銷放置清楚地顯示點擊的組號。

在我的代碼中,類別表示我將作爲組傳遞給Expandable列表的數據集。

您將有另一個覆蓋方法調用;

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
... 
} 

用這種方法你可以調用如下;

if(getGroup(groupPosition).toString().equals("GroupName")){ 
    //Do what even you like in for the clicked group 
} 

這是一個工作代碼,希望您能理解它。

1

您的可展開列表視圖可能位於滾動視圖內。你不能嵌套滾動。

+0

謝謝!我需要這個。我無法理解我做錯了什麼。我是在這種情況下。 – sunlover3 2018-01-12 15:42:01

+1

很高興幫助...! :) – navalkishoreb 2018-01-31 08:15:39

3

確保你有這樣的佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 


    <ExpandableListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

你不能有一個ExpandableListView成滾動型。