J'ai eu le même problème avec le ExpandedListView
. Le Switch
ou un autre contrôle Checkable
n'a pas enregistré d'instruction après l'expansion/réduction d'autres composants de groupe. J'ai résolu ce problème en enregistrant l'état dans childData list/array.
public class ExpandPrefAdapter extends BaseExpandableListAdapter {
private static final String TAG = ExpandPrefAdapter.class.getName();
private List<String> mGroupTitle = new ArrayList<>();
private Context mContext;
private List<List<ExpandableListItem>> mChildData = new ArrayList<>();
public ExpandPrefAdapter(Context context, final List<String> groupTitle, final
List<List<ExpandableListItem>> childData) {
mContext = context;
mGroupTitle = groupTitle;
mChildData = childData;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = inflater.inflate(R.layout.content_pref_entry, parent, false);
final int groupPos = listPosition;
final int childPos = expandedListPosition;
ExpandableListItem item = (ExpandableListItem) getChild(listPosition, expandedListPosition);
TextView textViewPrefTitle = (TextView) rootView.findViewById(R.id.text_view_pref_title);
textViewPrefTitle.setText(item.getTitle());
final Switch switchView = (Switch) rootView.findViewById(R.id.switch_pref);
switchView.setChecked(mChildData.get(groupPos).get(childPos).isChecked());
switchView.setOnCheckedChangeListener((v, b) -> mChildData.get(groupPos).get(childPos).setChecked(b));
return rootView;
}
@Override
public int getChildrenCount(int listPosition) {
return mChildData.get(listPosition).size();
}
@Override
public Object getChild(int listPosition, int expListPosition) {
return mChildData.get(listPosition).get(expListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public Object getGroup(int listPosition) {
return mGroupTitle.get(listPosition);
}
@Override
public int getGroupCount() {
return mGroupTitle.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String groupTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.content_pref_group, null);
}
TextView textViewGroup = (TextView) convertView
.findViewById(R.id.text_view_pref_group_title);
textViewGroup.setText(groupTitle);
Log.wtf(TAG, "getGroupView(): listPosition: " + listPosition + " isExpanded: " + isExpanded);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return false;
}
}
Je suppose que cela vous aidera
BTW, avec un CheckBox et auditeurs, cela ne fonctionne pas non plus. On dirait que ExpandableListView garde l'état des cases à cocher par position sans garder la position du groupe trop – fedj