У меня была такая же проблема с ExpandedListView
.Элементы управления Switch
или другой элемент Checkable
не сохранили оператор после развертывания / свертывания других компонентов группы.Я решил эту проблему, сохранив состояние в список / массив childData.
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;
}
}
Думаю, это поможет вам