I have an expandlist that has 6 items, when the 3rd can expand to another list of 12.
Everything works great, expands, collapses, opens fragments... my problem is that I can't set the items checked (change its background) only in a very specific case: when I choose a child item (from the 3rd expanded list), not closing the expanded list, and choosing an item below the expand list (bigger than 2).
for example: I choose item 2, the list expanded, then I scroll down and choose the last item from the first (group) list - it works but it is check (changes bg) of the 3rd item instaed of the 6th!
I guess it has something with a recycle view, bu I can't figure it out. I googled a lot but didn't find a working example. Is it possible at all?
Please help. Tnx
mMenuAdapter = new ExpandableListAdapter (this, listDataHeader, listDataChild, expandableList);
expandableList.setAdapter (mMenuAdapter);
expandableList.setOnGroupClickListener (new ExpandableListView.OnGroupClickListener () {
@Override
public boolean onGroupClick (ExpandableListView expandableListView, View view, int i, long l) {
if (currentView != null)
currentView.setBackgroundColor (getResources ().getColor (R.color.transparent));
if (expandableListView.isGroupExpanded (2))
expandableListView.collapseGroup (2);
if (i != 2) {
currentView = view;
currentView.setBackgroundColor (getResources ().getColor (R.color.veryLightBg));
} else {
currentView = null;
return false;
}
mainMenuClicked (i); // opens the right fragment
return true;
}
});
expandableList.setOnChildClickListener (new ExpandableListView.OnChildClickListener () {
@Override
public boolean onChildClick (ExpandableListView expandableListView, View view, int i, int i1, long l) {
if (currentView != null)
currentView.setBackgroundColor (getResources ().getColor (R.color.transparent));
currentView = view;
currentView.setBackgroundColor (getResources ().getColor (R.color.veryLightBg));
childMenuClicked (i1);
return false;
}
});
The adapter:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<ExpandedMenuItem> mListDataHeader;
private HashMap<ExpandedMenuItem, List<String>> mListDataChild;
ExpandableListView expandList;
public ExpandableListAdapter(Context context, List<ExpandedMenuItem> listDataHeader, HashMap<ExpandedMenuItem, List<String>> listChildData, ExpandableListView mView) {
this.mContext = context;
this.mListDataHeader = listDataHeader;
this.mListDataChild = listChildData;
this.expandList = mView;
}
@Override
public int getGroupCount() {
return this.mListDataHeader.size();
}
public int getChildrenCount(int groupPosition) {
int childCount = 0;
if (groupPosition == 2)
childCount = this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).size();
return childCount;
}
@Override
public Object getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ExpandedMenuItem headerTitle = (ExpandedMenuItem) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.main_list, null);
}
TextView lblListHeader = convertView.findViewById(R.id.single_menu);
if (groupPosition == 2) {
if (!isExpanded)
lblListHeader.setText (mContext.getString (R.string.products_category));
else
lblListHeader.setText (mContext.getString (R.string.expand_products_category));
} else {
lblListHeader.setText (headerTitle.getName ());
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild (groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.sub_list, null);
}
TextView txtListChild = convertView.findViewById(R.id.single_menu);
txtListChild.setText(childText);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}