在Android中滚动时,可扩展ListView GroupView上的复选框自动检查和取消选中

时间:2014-06-15 11:14:10

标签: android listview checkbox expandablelistview expandablelistadapter

我之前没有注意到这一点我感到相当愚蠢但是当我在我的应用程序中上下滚动时,复选框随机选中并取消选中。复选框位于GroupView上,而不是Expandable ListView的Child元素。我已经对此做了大量研究,并意识到问题在于回收视图以及与onCheckedChangeListener有关。我也知道(有点)我必须做些什么才能解决这个问题。事实是,我不知道如何实现它。这是因为所有研究都会导致使用复选框ListView修复人员或修复可扩展ListView的ChildView中的复选框

Here是我用于Expandable ListView的教程

以下是我发现的与我的问题相关的其他链接,排序。他们都没有特别的帮助:

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 中的CheckBox:

@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;
}

如果有人可以建议修复,或提供示例代码或任何其他帮助,这将是伟大的!提前谢谢!

1 个答案:

答案 0 :(得分:1)

创建一个类,如:

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,