2015-06-01 29 views
2

在我的應用程序中,我有一個動態的ExpandableListView以及一個刷新按鈕。當用戶點擊刷新按鈕時,最新的數據被下載,我呼叫notifyDataSetChanged()爲了更新UI。如何防止notifyDataSetChanged崩潰ExpandableListView中的開放組?

問題是notifyDataSetChanged()會摺疊先前打開的所有組,導致用戶的體驗不佳。有沒有辦法可以避免這種情況?

謝謝

+0

我們需要更多信息。你使用什麼適配器?如果它是自定義的,你可以發佈它的代碼? –

回答

1

這是我使用的方法。 setExpandedGroup()更新之前更新UI,然後getExpandedIds()更新後。我的idsExpand = new ArrayList<Integer>()也是Integer陣列。

public static void setExpandedGroup() { 
     if (expListView != null 
       && expListView.getExpandableListAdapter() != null) { 
      final int groupCount = expListView.getExpandableListAdapter().getGroupCount(); 
      for (int i = 0; i < groupCount; i++) { 
       if (expListView.isGroupExpanded(i)) { 
        idsExpand.add(i); 
       } 
      } 
     } 

    } 

    public static void getExpandedIds() { 
     if (idsExpand != null) { 
      final int groupCount = idsExpand.size(); 
      for (int i = 0; i < groupCount; i++) { 
       if (expListView != null) { 
        try { 
         expListView.expandGroup(idsExpand.get(i)); 
        } catch (Exception ignored) { 
        } 

       } 

      } 
     } 
    } 

希望它有幫助!

+0

你究竟在哪裏叫這些?我嘗試了類似的東西,但在getGroupView()中,isExpanded已設置爲false –

+0

您應該將其加載到Fragment/Activity中,但不是在適配器中。 –

+0

這是不正確的。當'notifyDataSetChanged()'被調用時,'ExpandableListView'和它的適配器支持重新擴展組。 –