80 lines
2.4 KiB
Java
80 lines
2.4 KiB
Java
package com.xscm.modulemain.adapter;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import com.chad.library.adapter.base.BaseQuickAdapter;
|
|
import com.chad.library.adapter.base.BaseViewHolder;
|
|
import com.xscm.modulemain.R;
|
|
import com.xscm.moduleutil.bean.AlbumBean;
|
|
import com.xscm.moduleutil.utils.ImageUtils;
|
|
|
|
|
|
/**
|
|
*@author qx
|
|
*@data 2025/6/6
|
|
*@description:相册单选弹框适配器
|
|
*/
|
|
public class SingleChoiceAdapter extends BaseQuickAdapter<AlbumBean, BaseViewHolder> {
|
|
/**
|
|
* -- GETTER --
|
|
* 获取当前选中位置
|
|
*/
|
|
private int selectedPosition = -1; // 默认未选中
|
|
|
|
public SingleChoiceAdapter() {
|
|
super(R.layout.item_multi_select_t);
|
|
}
|
|
|
|
|
|
@Override
|
|
protected void convert(BaseViewHolder helper, AlbumBean item) {
|
|
int position1 = helper.getAdapterPosition();
|
|
|
|
boolean isSelected = (position1 == selectedPosition);
|
|
TextView tv_album_name1 = helper.getView(R.id.tv_album_name1);
|
|
tv_album_name1.setText(item.getName());
|
|
helper.setText(R.id.tv_album_count1, "浏览数:"+item.getRead_num());
|
|
|
|
ImageUtils.loadHeadCC(item.getImage(), helper.getView(R.id.iv_photo1));
|
|
helper.getView(R.id.v_checkbox).setSelected(isSelected); // 使用 CheckBox.setChecked()
|
|
|
|
// 设置点击事件
|
|
helper.itemView.setOnClickListener(v -> {
|
|
int pos = helper.getAdapterPosition();
|
|
if (pos != RecyclerView.NO_POSITION) {
|
|
if (selectedPosition == pos) {
|
|
selectedPosition = -1; // 如果已选中,则取消
|
|
} else {
|
|
selectedPosition = pos; // 更新为新的选中项
|
|
}
|
|
}
|
|
notifyDataSetChanged();
|
|
});
|
|
|
|
helper.getView(R.id.v_checkbox).setOnClickListener(v -> {
|
|
int pos = helper.getAdapterPosition();
|
|
if (pos != RecyclerView.NO_POSITION) {
|
|
if (selectedPosition == pos) {
|
|
selectedPosition = -1;
|
|
} else {
|
|
selectedPosition = pos;
|
|
}
|
|
}
|
|
notifyDataSetChanged();
|
|
});
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* 获取当前选中的条目
|
|
*/
|
|
public AlbumBean getSelectedItem() {
|
|
if (selectedPosition == -1 || selectedPosition >= getData().size()) return null;
|
|
return getData().get(selectedPosition);
|
|
}
|
|
|
|
} |