在android系统滚动时可膨胀的ListView GroupView自动检查和取消选中复选框 [英] Checkbox on Expandable ListView GroupView auto-checking and unchecking when scrolling in android

查看:267
本文介绍了在android系统滚动时可膨胀的ListView GroupView自动检查和取消选中复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得有些愚蠢之前没有注意到这一点,但我在我的应用程序上下滚动,复选框抽查和取消。复选框都在GroupView,而不是扩展的ListView的子元素。我已经做了很多的研究,这一点,并意识到问题是回收的意见和东西做的onCheckedChangeListener。我也知道(在某种程度上)我有什么做的来解决这个问题。事情是,我不知道如何实现它。这是因为所有的研究导致修复使用复选框ListView控件或修复在扩展的ListView的ChildView复选框人

I feel rather stupid for not noticing this before but when I scroll up and down in my app, the checkboxes randomly check and uncheck. The checkboxes are on the GroupView and not the Child element of the Expandable ListView. I have done a lot of research into this and realise that the problem is recycling views and something to do with the onCheckedChangeListener. I also know (sort of) what I have to do to solve the problem. Thing is, I am not sure how to implement it. This is because all research leads to fixes for people using checkboxes ListView or fixes for checkboxes in the ChildView of an Expandable ListView

这里是我使用教程可扩展的ListView

Here is the tutorial I used for Expandable ListView

下面是我发现与我的问题,排序等环节。没有人能够帮助特别:

Below are other links I have found relating to my issue, sort. None of them can particularly help:

<一个href=\"http://stackoverflow.com/questions/2757121/android-listview-with-checkbox-automatically-unchecks\">Android用的ListView复选框:自动取消选中

<一个href=\"http://stackoverflow.com/questions/11233578/android-checkboxes-randomly-checked-unchecked-in-expandable-list\">Android,复选框随机选中/未选中的扩展列表

<一个href=\"http://stackoverflow.com/questions/10895763/checkbox-unchecked-when-i-scroll-listview-in-android\">checkbox选中android系统当我的ListView滚动

checkbox unchecked when i scroll listview in android

ListView的使用复选框滚动发行

<一个href=\"http://stackoverflow.com/questions/13432660/expandable-listview-with-selectall-checkbox-group-itemclick-and-scrolling-bug\">Expandable列表视图与全选复选框:组itemClick在和滚动错误

下面是一些code,我希望将阐明事物一些轻:

Below is some code that I hope will shed some light on things:

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

如果任何人都可以提出一个解决方法,或者提供样品code或其他任何帮助,这将是伟大的!在此先感谢!

If anyone could suggest a fix, or provide sample code or any other help that would be great! Thanks in advance!

推荐答案

创建一个类,如:

class ExpanableValue
{
  String title;
  boolean isChecked;


  // setter and getter

}

而不是列表与LT;弦乐&GT; 通过 ExpanableValue 的一个List(或你想要调用什么都)。

instead of List<String> pass one List of ExpanableValue (or what ever you want call that).

然后 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方法:

then in onClick method:

@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,

it is better to use ViewHolder in getGroupView,

这篇关于在android系统滚动时可膨胀的ListView GroupView自动检查和取消选中复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆