RecyclerView适配器复选框操作



我正在使用RecyclerView和适配器来显示Classes对象数组的数据。我有一个主支架和两个子支架-项目支架和标题支架。ItemHolder有复选框。在RecyclerView中,我为对象的一个参数使用了一个复选框。如何保存?对于其余部分,我使用SharedPreferences,但在这里我不知道如何在我的"活动"中应用它。

这是我的主支架:

public abstract class MainHolder extends RecyclerView.ViewHolder {
abstract void setData(MainItem item);
public MainHolder(@NonNull View itemView) {
super(itemView);
}
}

物品持有者:

public class ItemHolder extends MainHolder{
private TextView tvTitle, tvComment, tvDate;
private CheckBox cbImportance;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM", myDateFormatSymbols);

private static DateFormatSymbols myDateFormatSymbols = new DateFormatSymbols(){
@Override
public String[] getMonths() {
return new String[]{"января", "февраля", "марта", "апреля", "мая", "июня",
"июля", "августа", "сентября", "октября", "ноября", "декабря"};
}
};
@Override
void setData(MainItem item) {
final Event event = item.getEventItem();
tvTitle.setText(event.getName());
tvComment.setText(event.getComment());
tvDate.setText(simpleDateFormat.format(event.getDate()));
cbImportance.setOnCheckedChangeListener(null);
cbImportance.setChecked(event.getImportant());
cbImportance.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
event.IsImportant = b;
}
});
}
public ItemHolder(@NonNull View itemView) {
super(itemView);
tvTitle = itemView.findViewById(R.id.tvTitle);
tvComment = itemView.findViewById(R.id.tvComment);
tvDate = itemView.findViewById(R.id.tvDate);
cbImportance = itemView.findViewById(R.id.cbIconImportant);
}
}

这是我的适配器

public class MainAdapter extends RecyclerView.Adapter<MainHolder> {
private Context context;
private ArrayList<MainItem> data;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM", myDateFormatSymbols);
private OnItemCheckedListener mOnItemChecked;
public interface OnItemCheckedListener{
void onItemChecked(int position, boolean isImportant);
}
public void setOnItemCheckedListener(OnItemCheckedListener listener){
mOnItemChecked = listener;
}


private static DateFormatSymbols myDateFormatSymbols = new DateFormatSymbols(){
@Override
public String[] getMonths() {
return new String[]{"января", "февраля", "марта", "апреля", "мая", "июня",
"июля", "августа", "сентября", "октября", "ноября", "декабря"};
}
};
public MainAdapter(Context context, ArrayList<MainItem> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public MainHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
switch (viewType){
case (Constants.ITEM_HEADER_TEXT_VIEWTYPE):
view = LayoutInflater.from(context).inflate(R.layout.group_layout, parent, false);
return new HeaderHolder(view);
case (Constants.ITEM_EVENT_TEXT_VIEWTYPE):
view = LayoutInflater.from(context).inflate(R.layout.list_item_cardview, parent, false);
return new ItemHolder(view);
default: throw new IllegalArgumentException();
}
}
@Override
public void onBindViewHolder(@NonNull final MainHolder holder, final int position) {
holder.setData(data.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (holder.getItemViewType() == Constants.ITEM_HEADER_TEXT_VIEWTYPE)
Toast.makeText(context, "Нажал на заголовок" + position, Toast.LENGTH_SHORT).show();
else
Toast.makeText(context, "Нажал на айтем" + position, Toast.LENGTH_SHORT).show();
}
});

}
@Override
public int getItemViewType(int position) {
return data.get(position).getViewType();
}

好的,我在ItemHolder中公开了复选框。在OnBindHolder中,我为复选框制作了OnCheckedChanged

@Override
public void onBindViewHolder(@NonNull final MainHolder holder, final int position) {
holder.setData(data.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (holder.getItemViewType() == Constants.ITEM_HEADER_TEXT_VIEWTYPE)
Toast.makeText(context, "Нажал на заголовок" + position, Toast.LENGTH_SHORT).show();
else
Toast.makeText(context, "Нажал на айтем" + position, Toast.LENGTH_SHORT).show();
}
});
if (holder.getItemViewType() == Constants.ITEM_EVENT_TEXT_VIEWTYPE){
((ItemHolder) holder).cbImportance.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
data.get(position).getEventItem().IsImportant = b;
if (b){
Toast.makeText(context, "IMPORTANT", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, "NON IMPORTANT", Toast.LENGTH_SHORT).show();
}
notifyDataSetChanged();
mOnItemChecked.onItemChecked(position, b);
}
});
}
}

在片段中,我写复选框的状态。

adapterEvent.setOnItemCheckedListener(new MainAdapter.OnItemCheckedListener() {
@Override
public void onItemChecked(int position, boolean isImportant) {
write(getContext(), events, PROCESSED_EVENTS);
ShowEvents(ShowMode);
}
});

相关内容

  • 没有找到相关文章

最新更新