2

我關注此:Programmatically collapse a group in ExpandableListView。我希望用戶一次只能擴展一個組,並平滑地滾動到正確的位置。我已經寫了實現OnGroupClickListener自定義適配器:ExpandableListview OnGroupClickListener不生效

public class CategoriesListAdapter extends BaseExpandableListAdapter implements OnGroupClickListener { 

    private ExpandableListView expListView; 
    private int lastExpandedGroupPosition = -1; 

    // I get the expListView in the constructor... 

    @Override 
    public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long id) { 

     parent.smoothScrollToPosition(groupPosition); 

     if (parent.isGroupExpanded(groupPosition)) { 
      parent.collapseGroup(groupPosition); 
     } else { 
      parent.expandGroup(groupPosition); 
     } 

     return true; 
    } 

    @Override 
    public void onGroupExpanded(int groupPosition) { 

     if (groupPosition != lastExpandedGroupPosition) { 
      expListView.collapseGroup(lastExpandedGroupPosition); 
     } 

     super.onGroupExpanded(groupPosition); 
     lastExpandedGroupPosition = groupPosition; 
    } 
} 

不幸的是,onGroupClick事件從來沒有發射。(我已經把一些日誌的方法內)。我也試過:

expListView.setOnGroupClickListener(new OnGroupClickListener() { 
    @Override 
    public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long id) { 
     // Same as above... 
    }); 
} 

相同結果爲這種替代...任何想法?

回答

3

我會建議您檢查進口第一(在你的問題未顯示),並確保您已導入android.widget.ExpandableListView.OnGroupClickListener

或更換:new OnGroupClickListener() {

new android.widget.ExpandableListView.OnGroupClickListener {

編輯:

我也注意到你在boolean onGroupClick(...)的主體處返回'true'意思是「點擊w按照處理的方式進行「,團隊永遠不會崩潰,擴大。

如果你想expand.So我建議你做這樣你應該返回false:

expListView.setOnGroupClickListener(new OnGroupClickListener() { 
      @Override 
      public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) { 
       Log.d("onGroupClick:", "worked"); 

       parent.smoothScrollToPosition(groupPosition); 

       if (parent.isGroupExpanded(groupPosition)) { 
        parent.collapseGroup(groupPosition); 
       } else { 
        parent.expandGroup(groupPosition); 
       } 

       return false; 
      } 
     }); 
+0

您可以參考[這裏](http://developer.android.com/reference/android /widget/ExpandableListView.OnGroupClickListener.html) –

+0

導入是正確的。如果我將返回值設置爲false,則在第一次點擊時我的組未被正確摺疊(我需要2次點擊),並且沒有任何事件正在被解僱。無論如何,爲什麼onGroupClickListener覆蓋適配器不起作用?最後一個:爲什麼滾動在4.3上平滑,在2.3.3上滾動太快? – Jumpa