我在片段上有一个ExpandableListView,当我选择一个项目时,我正在更改此项目的背景和文本颜色(TextView)。这很好用。但是,当我折叠包含当前所选项目的组时,选择将丢失。如何阻止这种情况发生/将所选项目恢复到原来的状态?
我尝试过乱搞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 :(得分: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);
}
}
小组课程
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;
}