gpt4 book ai didi

android - 可扩展 ListView GroupView 上的复选框在 android 中滚动时自动选中和取消选中

转载 作者:太空狗 更新时间:2023-10-29 14:13:58 26 4
gpt4 key购买 nike

我之前没有注意到这一点,但当我在我的应用程序中上下滚动时,复选框会随机选中和取消选中,这让我感到很愚蠢。复选框位于 GroupView 而不是 Expandable ListView 的子元素上。我对此进行了大量研究,并意识到问题在于回收 View 以及与 onCheckedChangeListener 有关的问题。我也知道(某种程度上)我必须做什么来解决这个问题。问题是,我不确定如何实现它。这是因为所有研究都会针对使用复选框 ListView 的人进行修复,或针对可扩展 ListView 的 subview 中的复选框进行修复

Here是我用于Expandable ListView的教程

下面是我找到的与我的问题相关的其他链接,sort。他们都不能特别帮助:

Android ListView with Checkbox: automatically unchecks

Android, Checkboxes Randomly Checked/Unchecked in Expandable List

checkbox unchecked when i scroll listview in android

ListView with CheckBox Scrolling Issue

Expandable listview with selectall checkbox : group itemclick and scrolling bug

下面是一些代码,希望能对事情有所启发:

ExpandableListAdapter.java:现在根据第一个答案更新

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;

boolean checkAll_flag = false;
boolean checkItem_flag = false;

public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

final String childText = (String) getChild(groupPosition, childPosition);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.suikoden_list_item1, null);
}

TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);

txtListChild.setText(childText);
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}

@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

class ExpanableValue
{
String title;
boolean isChecked;


// setter and getter

}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {


if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.suikoden_list_group1, null);
//convertView.setTag(gholder);

//}else{
//gholder = (GroupViewHolder)convertView.getTag();
}

/////////////////////

CheckBox cb = (CheckBox)convertView
.findViewById(R.id.checkboxTick);
cb.setTag(groupPosition);
cb.setOnClickListener(this);
cb.setCheck(groupList.get(groupPosition).isChecked());
////////////////////

TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(groupList.get(groupPosition).getTitle());

return convertView;
}

public void onClick(View v) {

switch(v.getId())
{
case R.id.checkboxTick:
int pos = (Integer) arg0.findViewById(R.id.checkboxTick).getTag();
groupList.get(pos).setChecked((CheckBox)v.isChecked());
break;
}
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

SuikodenFragment.java 中的复选框:

@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
CheckBox checkBox = (CheckBox) expListView.findViewById(R.id.checkboxTick);
checkBox.setOnCheckedChangeListener(null);
if (isChecked)
// Add the tick to the box
//Log.d(TAG, "Tick the box");
checked = true;
else
// Remove the tick in the box
//Log.d(TAG, "Untick the box");
checked = false;
}

如果有人可以提出修复建议,或提供示例代码或任何其他帮助,那就太好了!提前致谢!

最佳答案

创建一个类:

class ExpanableValue
{
String title;
boolean isChecked;


// setter and getter

}

而不是 List<String>通过一个列表 ExpanableValue (或者你想怎么调用它)。

然后在getGroupView :

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {


if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.suikoden_list_group1, null);
//convertView.setTag(gholder);

//}else{
//gholder = (GroupViewHolder)convertView.getTag();
}

/////////////////////

CheckBox cb = (CheckBox)convertView
.findViewById(R.id.checkBoxID);
cb.setTag(groupPosition);
cb.setOnClickListener(this);
cb.setCheck(groupList.get(groupPosition).isChecked());
////////////////////

TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(groupList.get(groupPosition).getTitle());

return convertView;
}

然后在onClick方法中:

@Override
public void onClick(View v) {

switch(v.getId())
{
case R.id.checkBoxID:
int pos = (Integer) v.findViewById(R.id.checkBoxID).getTag();
groupList.get(pos).setChecked((CheckBox)v.isChecked());
break;
}
}

在getGroupView中最好使用ViewHolder,

关于android - 可扩展 ListView GroupView 上的复选框在 android 中滚动时自动选中和取消选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24229043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com