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;
}
});
可以試試這個 - public View onCreateView(...)而不是public void onActivityCreated(...) – Saj