2016-02-25 63 views
0

如何取消選擇所有其他選擇的子視圖?我試圖在任何時間只保留一個孩子。在下面的代碼中,我可以突出顯示孩子,但我在該組中選擇的其他孩子保持突出顯示。另外,如果我選擇一個孩子並切換組,則同一位置中的下一個組孩子也會突出顯示。ExpandableListView - 如何只保持一個組孩子突出顯示?

//CHILD CLICK LISTENER 
lstRoute.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
    @Override 
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 

     for(int i = 0; i < parent.getChildCount(); i++) 
     { 
      View child = parent.getChildAt(i); 
      if(child instanceof ViewGroup) 
      { 
       //DO NOTHING 
      }else 
      { 
       child.setBackgroundColor(Color.TRANSPARENT); 
      } 
     } 
     v.setBackgroundColor(Color.GREEN); 
     return true; 
    } 
}); 

//GROUP EXPANDED 
lstRoute.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { 
    @Override 
    public void onGroupExpand(int groupPosition) { 

     //Only allow one group to be expanded at a time 
     if (lastExpandedPosition != -1 && groupPosition != lastExpandedPosition) { 
      lstRoute.collapseGroup(lastExpandedPosition); 
     } 
     lastExpandedPosition = groupPosition; 

     //Clear all selections 
     for(int i = 0; i < lstRoute.getChildCount(); i++) 
     { 
      View child = lstRoute.getChildAt(i); 
      if(child instanceof ViewGroup) { 
       child.setBackgroundColor(Color.argb(100,139,213,252)); //Light Blue 
      }else{ 
       child.setBackgroundColor(Color.TRANSPARENT); 
      } 
     } 
    } 
}); 

回答

1

ListView的回收視圖。這意味着,如果一個視圖隱藏在一個組中,它將用於新打開的組中。

一般而言:突出顯示ListView中的選擇不應在ClickListener中實施,而應在自定義Adapter中實施。

+0

非常感謝您的支持,它指出我的方向正確,但您能否提供一個如何在自定義視圖上工作的示例? – Dayan

+1

https://github.com/F43nd1r/Multitool/blob/master/app/src/main/java/com/faendir/lightning_launcher/multitool/scriptmanager/ScriptListAdapter.java雖然這是多選也允許選擇組,它應該顯示原則。 (由我代碼) – F43nd1r

+0

好東西,謝謝! – Dayan

相關問題