2014-01-21 90 views
0

我有一個ExpandableListView的片段,當我選擇一個項目時,我正在更改此項目(TextView)的背景和文本顏色。這工作正常。但是,當我摺疊包含當前選定項目的組時,選擇將丟失。我怎樣才能阻止這種情況發生/將選定的項目設置回它應該是什麼?ExpandableListView選擇的項目在組崩潰時丟失

我嘗試過使用onGroupExpand/CollapseListener搞亂了,但沒有任何成功。例如,我曾經嘗試這樣做:

_list.setOnGroupExpandListener(new OnGroupExpandListener() { 
    @Override 
    public void onGroupExpand(int groupPosition) { 
     _list.setItemChecked(_activePosition, true); 
    } 
}); 

注:_list_activePosition在類級別聲明。

這裏是我設置檢查項目的代碼,從我見過的例子中我認爲這是標準的做法,但請指教,如果不是我是Android新手。

HelpItemsFragment.java

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 

    final List<HelpCategory> itemList = new ArrayList<HelpCategory>(); 
    HelpCategory helpCategory; 

    //replace this with database stuff later 
    helpCategory = new HelpCategory(); 
    helpCategory.setTitle("Group 1"); 
    helpCategory.addHelpItem(new HelpItem((long) 1, "1.1", "Details for 1.1")); 
    helpCategory.addHelpItem(new HelpItem((long) 1, "1.2", "Details for 1.2")); 
    helpCategory.addHelpItem(new HelpItem((long) 1, "1.3", "Details for 1.3")); 
    itemList.add(helpCategory); 

    helpCategory = new HelpCategory(); 
    helpCategory.setTitle("Group 2"); 
    helpCategory.addHelpItem(new HelpItem((long) 2, "2.1", "Details for 2.1")); 
    helpCategory.addHelpItem(new HelpItem((long) 2, "2.2", "Details for 2.2")); 
    helpCategory.addHelpItem(new HelpItem((long) 2, "2.3", "Details for 2.3")); 
    itemList.add(helpCategory); 

    HelpExpandableListAdapter listAdapter = new HelpExpandableListAdapter(this.getActivity(), itemList); 
    _list.setAdapter(listAdapter); 

    _list.setOnChildClickListener(new OnChildClickListener() { 
     public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
      int index = parent.getFlatListPosition(ExpandableListView.getPackedPositionForChild(groupPosition, childPosition)); 
      HelpItem helpItem = itemList.get(groupPosition).getHelpItemList().get(childPosition); 
      _activePosition = index; 
      _callback.onItemSelected(null, helpItem.getTitle(), helpItem.getDetail(), _activePosition); 
      parent.setItemChecked(index, true); 
      return true; 
     } 
    }); 
+0

可以試試這個 - public View onCreateView(...)而不是public void onActivityCreated(...) – Saj

回答

0

適配器類HelpExpandableListAdapter應該像

public class HelpExpandableListAdapter extends BaseExpandableListAdapter { 

     private LayoutInflater inflater; 
     private ArrayList<CategoryGroup> mParent; 

     public ExpandableListItems(Context context, ArrayList<CategoryGroup> parent) { 
      mParent = parent; 
      inflater = LayoutInflater.from(context); 
     } 


     @Override 
     //counts the number of parent items so the list knows how many times calls getGroupView() method 
     public int getGroupCount() { 
      return mParent.size(); 
     } 

     @Override 
     //counts the number of children items so the list knows how many times calls getChildView() method 
     public int getChildrenCount(int i) { 
      return mParent.get(i).getArrayChildren().size(); 
     } 

     @Override 
     //gets the title of each parent/group 
     public Object getGroup(int i) { 
      return mParent.get(i).getTitle(); 
     } 

     @Override 
     //gets the name of each item 
     public Object getChild(int i, int i1) { 
      return mParent.get(i).getArrayChildren().get(i1); 
     } 

     @Override 
     public long getGroupId(int i) { 
      return i; 
     } 

     @Override 
     public long getChildId(int i, int i1) { 
      return i1; 
     } 

     @Override 
     public boolean hasStableIds() { 
      return true; 
     } 

     @Override 
     //in this method you must set the text to see the parent/group on the list 
     public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { 

      if (view == null) { 
       System.out.println("i: " + i + "; b: " + b); 
       view = inflater.inflate(R.layout.grouprow, viewGroup, false); 
      } 


      TextView textView = (TextView) view.findViewById(R.id.rowname); 

      //"i" is the position of the parent/group in the list 

      textView.setText(getGroup(i).toString()); 

      //return the entire view 
      return view; 
     } 

     @Override 
     //in this method you must set the text to see the children on the list 
     public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) { 
      if (view == null) { 
       view = inflater.inflate(R.layout.childrow, viewGroup, false); 
      } 

      TextView textView = (TextView) view.findViewById(R.id.grpchild); 
      //"i" is the position of the parent/group in the list and 
      //"i1" is the position of the child 
      textView.setText(mParent.get(i).getArrayChildren().get(i1)); 

      //return the entire view 
      return view; 
     } 

     @Override 
     public boolean isChildSelectable(int i, int i1) { 
      return true; 
     } 

     @Override 
     public void registerDataSetObserver(DataSetObserver observer) { 
      /* used to make the notifyDataSetChanged() method work */ 
      super.registerDataSetObserver(observer); 
     } 
    } 

Group類

public class CategoryGroup { 
    private String mTitle; 
    private ArrayList<String> mArrayChildren; 

    public String getTitle() { 
     return mTitle; 
    } 

    public void setTitle(String mTitle) { 
     this.mTitle = mTitle; 
    } 

    public ArrayList<String> getArrayChildren() { 
     return mArrayChildren; 
    } 

    public void setArrayChildren(ArrayList<String> mArrayChildren) { 
     this.mArrayChildren = new ArrayList(mArrayChildren); 
    } 
    } 

在onCreateView()

ArrayList<CategoryGroup> arrayParentsGroups = new ArrayList<CategoryGroup>(); 

    arrayParentsGroups = createGroupList(); 

    listAdapter = new HelpExpandableListAdapter(this, arrayParentsGroups); 
    _list.setAdapter(listAdapter); 

    private List createGroupList() { 
     ArrayList arrayParents = new ArrayList(); 
     for(int i = 0 ; i < 10 ; ++i) { // 10 groups........ 
      CategoryGroup parent = new CategoryGroup(); 
      parent.setTitle("Group row" + i); 

      ArrayList<String> arrayChildren = new ArrayList<String>(); 
      for (int j = 0; j < 3; j++) { 
       arrayChildren.add("Child row" + j); 
      } 
      parent.setArrayChildren(arrayChildren); 
      arrayParents.add(parent); 
     } 
     return arrayParents; 
    } 
相關問題