2025-10-28 16:56:13 +08:00
|
|
|
|
package com.xscm.modulemain.adapter;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
|
|
|
|
|
|
import android.util.SparseBooleanArray;
|
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
|
import android.view.View;
|
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
|
|
|
2025-10-28 16:56:13 +08:00
|
|
|
|
import com.xscm.modulemain.R;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
import com.xscm.moduleutil.bean.HeatedBean;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
|
|
|
|
*@author qx
|
|
|
|
|
|
*@data 2025/6/17
|
|
|
|
|
|
*@description: 这是发布动态选择话题的适配器,多选状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
public class MultiSelectAdapter extends RecyclerView.Adapter<MultiSelectAdapter.ViewHolder> {
|
|
|
|
|
|
private List<HeatedBean> options;
|
|
|
|
|
|
private SparseBooleanArray selectedPositions = new SparseBooleanArray();
|
|
|
|
|
|
|
|
|
|
|
|
public MultiSelectAdapter(List<HeatedBean> options) {
|
|
|
|
|
|
this.options = options;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
|
|
|
|
|
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_multi_select, parent, false);
|
|
|
|
|
|
return new ViewHolder(view);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
|
|
|
|
|
HeatedBean item = options.get(position);
|
|
|
|
|
|
|
|
|
|
|
|
boolean isSelected = selectedPositions.get(position, false);
|
|
|
|
|
|
|
|
|
|
|
|
holder.tvOption.setText(item.getTitle());
|
|
|
|
|
|
holder.checkbox.setSelected(isSelected);
|
|
|
|
|
|
holder.checkbox.setOnClickListener(v -> {
|
|
|
|
|
|
boolean newState = !isSelected;
|
|
|
|
|
|
selectedPositions.put(position, newState);
|
|
|
|
|
|
holder.checkbox.setSelected(newState);
|
|
|
|
|
|
notifyItemChanged(position); // 刷新当前项
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 点击整个 item 也可以触发选中/取消
|
|
|
|
|
|
holder.itemView.setOnClickListener(v -> {
|
|
|
|
|
|
boolean currentState = selectedPositions.get(position, false);
|
|
|
|
|
|
boolean newState = !currentState;
|
|
|
|
|
|
selectedPositions.put(position, newState);
|
|
|
|
|
|
holder.checkbox.setSelected(newState);
|
|
|
|
|
|
notifyItemChanged(position);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// holder.itemView.setOnClickListener(v -> {
|
|
|
|
|
|
// boolean isChecked = selectedPositions.get(position, false);
|
|
|
|
|
|
// if (isChecked) {
|
|
|
|
|
|
// selectedPositions.delete(position);
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// selectedPositions.put(position, true);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// notifyItemChanged(position);
|
|
|
|
|
|
// });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public int getItemCount() {
|
|
|
|
|
|
return options.size();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<HeatedBean> getSelectedItems() {
|
|
|
|
|
|
List<HeatedBean> selected = new ArrayList<>();
|
|
|
|
|
|
for (int i = 0; i < options.size(); i++) {
|
|
|
|
|
|
if (selectedPositions.get(i)) {
|
|
|
|
|
|
selected.add(options.get(i));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return selected;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static class ViewHolder extends RecyclerView.ViewHolder {
|
|
|
|
|
|
View checkbox;
|
|
|
|
|
|
TextView tvOption;
|
|
|
|
|
|
|
|
|
|
|
|
public ViewHolder(@NonNull View itemView) {
|
|
|
|
|
|
super(itemView);
|
|
|
|
|
|
checkbox = itemView.findViewById(R.id.v_checkbox);
|
|
|
|
|
|
tvOption = itemView.findViewById(R.id.tv_option);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|