I implemented ExpandableListView with checkboxes for groups and child items. Here is my code.
filter_group_item.xml (The same in filter_child_item.xml)
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="0dp"
android:layout_height="wrap_content"
style="@style/MainTheme.BodyText"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:layout_weight="9"/>
<CheckBox
android:id="@+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:focusable="false"/>
</LinearLayout>
ExpandableListAdapter
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> titles;
private HashMap<String, List<String>> items;
private HashMap<String, Boolean> allItems;
public ExpandableListAdapter(Context context, List<String> titles, HashMap<String,
List<String>> items, HashMap<String, Boolean> allItems) {
this.context = context;
this.titles = titles;
this.items = items;
this.allItems = allItems;
}
public List<String> getCheckedItems() {
List<String> checkedItems = new ArrayList<>();
for (String item : allItems.keySet()) {
if (allItems.get(item)) {
checkedItems.add(item);
}
}
return checkedItems;
}
@Override
public int getGroupCount() {
return titles.size();
}
@Override
public int getChildrenCount(int groupPos) {
return items.get(titles.get(groupPos)).size();
}
@Override
public Object getGroup(int groupPos) {
return titles.get(groupPos);
}
@Override
public Object getChild(int groupPos, int childPos) {
return items.get(titles.get(groupPos)).get(childPos);
}
@Override
public long getGroupId(int groupPos) {
return groupPos;
}
@Override
public long getChildId(int groupPos, int childPos) {
return childPos;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPos, boolean b, View view, ViewGroup viewGroup) {
final String item = (String) getGroup(groupPos);
Boolean isChecked = allItems.get(item);
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.filter_group_item, null);
}
TextView textView = ButterKnife.findById(view, R.id.text);
textView.setText(item);
textView.setTypeface(null, Typeface.BOLD);
CheckBox checkBox = ButterKnife.findById(view, R.id.check_box);
checkBox.setChecked(isChecked);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
Boolean isItemChecked = allItems.get(item);
allItems.put(item, !isItemChecked);
}
});
return view;
}
@Override
public View getChildView(int groupPos, int childPos, boolean b, View view, ViewGroup viewGroup) {
final String item = (String) getChild(groupPos, childPos);
Boolean isChecked = allItems.get(item);
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.filter_child_item, null);
}
TextView textView = ButterKnife.findById(view, R.id.text);
textView.setText(item);
CheckBox checkBox = ButterKnife.findById(view, R.id.check_box);
checkBox.setChecked(isChecked);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
Boolean isItemChecked = allItems.get(item);
allItems.put(item, !isItemChecked);
}
});
return view;
}
@Override
public boolean isChildSelectable(int groupPos, int childPos) {
return true;
}
}
Problem is that it doesn't work correctly. When I try to play with it some checkboxes unchecked or checked automatically. When I collapse groups by order, and expand them - on this moment checkboxes change their state. Maybe problem is with childId or groupId. But I don't know how to fix it.
Aucun commentaire:
Enregistrer un commentaire