[英]Check box state change automatically in expandable list {Android}
嗨,我正在处理“可扩展列表”,但是我遇到的问题是,当我选中一个复选框并扩展该列表或向上/向下滚动时,复选框状态会自动更改,例如,如果我在滚动列表中,最后一个或第二个被选中,而当前将被取消选中。 我进行了很多研究,但没有发现任何有用的东西,我知道这是大多数开发人员面临的非常普遍的问题,因此如果您有建议的话。 谢谢
这是我的适配器类:
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
List<ChildDataBean> mListDataChild;
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;
public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
@Override
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public View getChildView(int listPosition, final int expandedListPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition, expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_child, null);
}
TextView ChildText = (TextView) convertView.findViewById(R.id.child_text);
ChildText.setText(expandedListText);
CheckBox ChildChecked = (CheckBox)convertView.findViewById(R.id.child_check_box);
if (ChildChecked.isChecked())
{
ChildChecked.setChecked(true);
}
return convertView;
}
@Override
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}
@Override
public Object getGroup(int listPosition) {
return this.expandableListTitle.get(listPosition);
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_parent, null);
}
TextView ParentText = (TextView) convertView.findViewById(R.id.parent_text);
ParentText.setTypeface(null, Typeface.BOLD);
ParentText.setText(listTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
请在getChildView方法中传递null,例如:
checkbox.setonclicklisner(null);
请尝试,如果(ChildChecked.isChecked()){ChildChecked.setChecked(true);}否则{ChildChecked.setChecked(false);},这可能会解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.