自定义适配器 - CheckedTextView

CustomAdapter - CheckedTextView

我对 CheckTextView 中复选框的 "remembering" 状态有疑问。 现在当我点击时,它改变了状态但是在不同的 CheckBox 上。

class CustomAdapterSmartwatch extends ArrayAdapter<String> {


static class ViewHolder {
    protected CheckedTextView cTextView;
}

ViewHolder holder;


public CustomAdapterSmartwatch(Context context, ArrayList<String> variables) {
    super(context,R.layout.row_smartwatch_resources,variables);
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
        convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
        holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.cTextView.setText(getItem(position));

    holder.cTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (holder.cTextView.isChecked()) {
                holder.cTextView.setChecked(false);
            } else {
                holder.cTextView.setChecked(true);
            }
        }
    });

    return convertView;
}

}

现在我正在尝试使用 Holder,但我听说可以使用 SparseBooleanArray。谁能解释一下它是如何工作的?

编辑 1 好的,我有一个由 CustomAdapter 控制的 ListView。该适配器仅包含一项 (CheckedTextView)。 问题是滚动这些复选框后不记得它们的状态。 我看到你的回答和解释,但现在点击任何 "CheckedTextView",他们的复选框的状态没有改变。

编辑 2

    @NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        holder = new ViewHolder();
        LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
        convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
        holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.cTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("MainActivity","Position = " + position);
            CheckedTextView checkedText = (CheckedTextView) v;
            checkedState.put(position, !checkedText.isChecked());

            Log.d("MainActivity","State = " + checkedState.get(position));

        }
    });

    holder.cTextView.setText(getItem(position));
    holder.cTextView.setChecked(checkedState.get(position));

    return convertView;
}

编辑 3

class CustomAdapterSmartwatch extends ArrayAdapter<String> {

public SparseBooleanArray checkedState = new SparseBooleanArray();
private CheckedTextView cTextView;


public CustomAdapterSmartwatch(Context context, ArrayList<String> variables) {
    super(context,R.layout.row_smartwatch_resources,variables);
}

@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
    convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
    cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);

    cTextView.setText(getItem(position));
    cTextView.setChecked(checkedState.get(position));

    cTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("MainActivity","Position = " + position);
            CheckedTextView checkedText = (CheckedTextView) v;
            checkedText.toggle();

            if (checkedText.isChecked())
                checkedState.put(position,true);
            else
                checkedState.delete(position);
            Log.d("MainActivity","State = " + checkedState.get(position));

        }
    });
    return convertView;
}

}

您必须为每个 CheckTextView 保留一个布尔标志。这可以是数组、列表或 SparseBooleanArray。你选择的数据结构应该是你适配器的字段:

class CustomAdapterSmartwatch extends ArrayAdapter<String> {
    private SparseBooleanArray checkedState = new SparseBooleanArray();

    // ...
}

您不想将其放入容器中,因为容器仅在可见行中退役。另一方面,您希望为每一行保持选中状态,无论可见与否。

现在,当用户点击 CheckedTextView:

时,您设置存储的选中状态
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ...

    holder.cTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckedTextView checkedText = (CheckedTextView)v;
            checkedState.put(position, checkedText.isChecked());
        }
    });
}

并且每当您膨胀视图或重新使用持有人的视图时,您都会恢复选中状态:

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ...

    holder.cTextView.setText(getItem(position));
    holder.cTextView.setChecked(checkedState.get(position));

    // ...
}

用以下更新替换您的代码:

class CustomAdapterSmartwatch extends ArrayAdapter<CustomAdapterSmartwatch.MyPojo> {

    class MyPojo {
        boolean checked;
        String data;

        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public String getData() {
            return data;
        }

        public void setData(String data) {
            this.data = data;
        }
    }

    static class ViewHolder {
        protected CheckedTextView cTextView;
    }

    ViewHolder holder;


    public CustomAdapterSmartwatch(Context context, ArrayList<MyPojo> variables) {
        super(context, R.layout.row_smartwatch_resources, variables);
    }

    @Nullable
    @Override
    public MyPojo getItem(int position) {
        return super.getItem(position);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
            convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
            holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final MyPojo item = getItem(position);

        holder.cTextView.setText(item.getData());
        holder.cTextView.setChecked(item.isChecked());

        holder.cTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean nextStatus = !item.isChecked();
                item.setChecked(nextStatus);
                holder.cTextView.setChecked(nextStatus);
            }
        });

        return convertView;
    }
}

您可以简单地在 onClick() 方法本身中使用视图。

holder.cTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v instanceOf CheckedTextView) {
                CheckedTextView c = (CheckedTextView) v;
                c.setChecked(!c.isChecked());
            }
        }
    });