条目状态错乱问题 (这个和listView RecyclerView是一样)就是条目中有 有状态的组件 如:checkBox radioButton等
数据集: javaBean
public class Bean implements Serializable {
private String topTitle;
private List<String> lvel1Titles;
public String getTopTitle() {
return topTitle;
}
public void setTopTitle(String topTitle) {
this.topTitle = topTitle;
}
public List<String> getLvel1Titles() {
return lvel1Titles;
}
public void setLvel1Titles(List<String> lvel1Titles) {
this.lvel1Titles = lvel1Titles;
}
}
public class MyAdapter extends BaseExpandableListAdapter {
Map<String, Boolean> maps = new HashMap<>(); //converView复用导致的问题
Map<String, CheckBox> checkBoxMap = new HashMap<>(); //单选处理
private List<Bean> datas; //数据集
CallBack mCallBack;
public MyAdapter(List<Bean> datas,CallBack callBack) {
this.datas = datas;
this.mCallBack = callBack; //创建适配器的时候把这个回调接口赋值
}
@Override
public int getGroupCount() {
return datas.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return datas.get(groupPosition).getLvel1Titles().size();
}
@Override
public Object getGroup(int groupPosition) {
return datas.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return datas.get(groupPosition).getLvel1Titles().get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_top, parent, false);
groupViewHolder = new GroupViewHolder();
groupViewHolder.topTitle = convertView.findViewById(R.id.tv_top);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.topTitle.setText(datas.get(groupPosition).getTopTitle());
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final ChildViewHodler childViewHodler;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_level1, parent, false);
childViewHodler = new ChildViewHodler();
childViewHodler.level1CheckBox = convertView.findViewById(R.id.cb_level1);
convertView.setTag(childViewHodler);
} else {
childViewHodler = (ChildViewHodler) convertView.getTag();
}
childViewHodler.level1CheckBox.setText(groupPosition + ":" + datas.get(groupPosition).getLvel1Titles().get(childPosition));
childViewHodler.level1CheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
for ( Map.Entry<String, CheckBox> entry:checkBoxMap.entrySet()) {
CheckBox value = entry.getValue();
value.setChecked(false);
}
checkBoxMap.clear(); //用完之后清一下状态
if (isChecked) {
maps.put(groupPosition + "" + childPosition, true); //只有groupPosition和childPosition才能确定一个子条目
checkBoxMap.put(groupPosition + "" + childPosition, childViewHodler.level1CheckBox);
// 把 checkBox传给接口 (使用页面实现 getCheckBox拿到选中的 checkBox然后就可以获取值了)
mCallBack.getCheckBox(childViewHodler.level1CheckBox);
} else {
maps.remove(groupPosition + "" + childPosition);
}
}
});
if (maps != null && maps.containsKey(groupPosition + "" + childPosition)) {
childViewHodler.level1CheckBox.setChecked(true);
} else {
childViewHodler.level1CheckBox.setChecked(false);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
static class GroupViewHolder {
TextView topTitle;
}
static class ChildViewHodler {
CheckBox level1CheckBox;
}
/**
* 回调接口处理 子条目选中的的CheckBox的text
*/
interface CallBack {
void getCheckBox(CheckBox checkBox);
}
}
页面中使用
public class MainActivity extends AppCompatActivity implements MyAdapter.CallBack {
private ExpandableListView expandableListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = (ExpandableListView) findViewById(R.id.elv);
List<Bean> datas = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Bean bean = new Bean();
bean.setTopTitle("上层目录"+i);
List<String> lev1s = new ArrayList<>();
for (int j = 0; j < 5; j++) {
lev1s.add("子条目"+j);
}
bean.setLvel1Titles(lev1s);
datas.add(bean);
}
MyAdapter adapter = new MyAdapter(datas,this);
expandableListView.setAdapter(adapter);
}
@Override
public void getCheckBox(CheckBox checkBox) {
Toast.makeText(this, ""+checkBox.getText(), Toast.LENGTH_SHORT).show();
}
}
写在最后 布局文件就不贴出来了 通过适配器的静态内部类 大家应该很清楚了。 这个功能对大家来说 可能是分分钟就搞出来了,我这里贴出来只是为了以后自己方便查阅。