java - 可扩展 ListView 中 Gridview 中的复选框在滚动时未选中

标签 java android listview android-adapter

我有一个可扩展的 ListView 。在它的子项中是一个 GridView 。 gridview 内部有自定义复选框。

现在的问题是,当我选中一个复选框并向下滚动,甚至关闭父 View 时,该复选框就消失了。

类似这样的事情:

滚动前检查屏幕:

Before Scroll

滚动后检查屏幕:

After Scroll

这是我的ExpandableListView适配器:

public class CreateProfileExpandableAdapter extends BaseExpandableListAdapter {

    ArrayList<CreateProfileFecetsHeaderModel> al;
    Activity activity;

    public CreateProfileExpandableAdapter(Activity activity, ArrayList<CreateProfileFecetsHeaderModel> al) {
        this.activity = activity;
        this.al = al;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return al.get(groupPosition).getChildren();
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return al.get(groupPosition).hashCode();
    }

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

        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.create_profile_child, parent, false);
        }

        ExpandableHeightGridView gridViewChildren = (ExpandableHeightGridView) v.findViewById(R.id.gridViewChildren);
        gridViewChildren.setExpanded(true);

        CreateProfileChildAdapter createProfileChildAdapter = new CreateProfileChildAdapter(activity, al.get(groupPosition).getChildren());
        gridViewChildren.setAdapter(createProfileChildAdapter);


        return v;

    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

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

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

    @Override
    public long getGroupId(int groupPosition) {
        return al.get(groupPosition).hashCode();
    }

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

        View v = convertView;

        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.create_profile_group, parent, false);
        }

        TextView textViewGroup = (TextView) v.findViewById(R.id.textViewGroup);
        textViewGroup.setText(al.get(groupPosition).getText());

        return v;

    }

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

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


}

Gridview 适配器:

public class CreateProfileChildAdapter extends BaseAdapter {

    Activity activity;
    ArrayList<CreateProfileFecetsChildModel> al;
    int index = -1;

    public CreateProfileChildAdapter(Activity activity, ArrayList<CreateProfileFecetsChildModel> al) {
        // TODO Auto-generated constructor stub
        this.activity = activity;
        this.al = al;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return al.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return al.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    private static class ViewHolder {
        CheckBox checkBoxChild;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        final ViewHolder viewHolder;
        final int pos = position;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(activity);
            convertView = inflater.inflate(R.layout.create_profile_grid_child, parent, false);
            viewHolder = new ViewHolder();

            viewHolder.checkBoxChild = (CheckBox) convertView.findViewById(R.id.checkBoxChild);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        viewHolder.checkBoxChild.setText(al.get(position).getTitle());

        viewHolder.checkBoxChild.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if (b) {
                    viewHolder.checkBoxChild.setTextColor(activity.getResources().getColor(R.color.white));
                } else {
                    viewHolder.checkBoxChild.setTextColor(activity.getResources().getColor(R.color.black));
                }
            }
        });

        return convertView;
    }

}

最佳答案

问题在于 View 被回收而没有重置其状态。想象一下,屏幕上显示 20 个项目,但一次只显示 4 个项目,这意味着 GridViewAdapter 使用四个甚至五个 View 。当滚动列表时,这些 View 轮流显示您的项目:

Views being recycled when scrolling

解决方案:

  1. 您必须将有关 View 状态的所有信息(在您的情况下:标记为选中的内容)存储在 CreateProfileFecetsHeaderModel 类型的适配器子列表条目中。
  2. 在适配器的 getChildView() 中,使用 position 从适配器的子级获取选中状态,并相应地设置持有者的 CheckBox。

关于java - 可扩展 ListView 中 Gridview 中的复选框在滚动时未选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44819350/

相关文章:

java - 我的 Java Swing 登录表单不起作用

android - Android 上 LinearLayout 中的 CSS "float:right"属性等效?

android - 如何知道操作栏中的 SearchView 何时关闭?

android - 隐藏 ListView 中的项目

java - forEach 迭代器标签

java - 如何在我的数字时钟图形用户界面上闪烁冒号?

java - Gradle 遗留多模块构建 : working around bad choices

android - 在 Android 应用程序中创建类似 Google 的即时搜索

java - Android - 在 Activity 之间保持用户记录

java - 更改 ListView 的一个元素