在展开式列表中自动选中状态更改{Android}

时间:2017-02-16 06:02:53

标签: 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;
    }

}

2 个答案:

答案 0 :(得分:0)

请在getChildView方法中传递null,如:

  checkbox.setonclicklisner(null);

答案 1 :(得分:-1)

尝试,if(ChildChecked.isChecked()){ChildChecked.setChecked(true);} else {ChildChecked.setChecked(false);}这可能会解决您的问题。