108 lines
3.8 KiB
Java
108 lines
3.8 KiB
Java
package com.xscm.moduleutil.adapter;
|
|
|
|
import android.view.ViewGroup;
|
|
import android.view.WindowManager;
|
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
|
|
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
|
|
import com.chad.library.adapter.base.BaseViewHolder;
|
|
import com.xscm.moduleutil.R;
|
|
import com.xscm.moduleutil.bean.RechargeBean;
|
|
import com.xscm.moduleutil.utils.SystemUtils;
|
|
|
|
import java.util.List;
|
|
|
|
/**
|
|
* 充值adapter
|
|
*/
|
|
public class BalanceRechargeAdapter extends BaseMultiItemQuickAdapter<RechargeBean, BaseViewHolder> {
|
|
private int selectedPosition = -1;
|
|
private OnRechargeItemClickListener listener;
|
|
private InputBoxVisibilityListener inputBoxVisibilityListener;
|
|
|
|
public static final int ITEM_TYPE_NORMAL = 0;
|
|
public static final int ITEM_TYPE_FOOTER = 1;
|
|
|
|
public BalanceRechargeAdapter(@NonNull List<RechargeBean> data) {
|
|
super(data);
|
|
// 初始化 item 类型
|
|
addItemType(ITEM_TYPE_NORMAL, R.layout.rv_item_balance_recharge);
|
|
addItemType(ITEM_TYPE_FOOTER, R.layout.rv_item_footer);
|
|
|
|
|
|
}
|
|
@Override
|
|
public int getItemViewType(int position) {
|
|
RechargeBean item = getData().get(position);
|
|
return item.getItemViewType(); // 使用 bean 中的 itemViewType
|
|
}
|
|
@Override
|
|
protected void convert(BaseViewHolder helper, RechargeBean item) {
|
|
int type = helper.getItemViewType();
|
|
// ConstraintLayout constraintLayout = helper.getView(R.id.cl_item);
|
|
// ViewGroup.LayoutParams layoutParams =constraintLayout.getLayoutParams();
|
|
// layoutParams.width = ( com.blankj.utilcode.util.ScreenUtils.getScreenWidth()-32-24)/3-24; // 使用你定义的getWidth方法
|
|
// constraintLayout.setLayoutParams(layoutParams);
|
|
if (type == ITEM_TYPE_NORMAL) {
|
|
// 正常 item 显示逻辑
|
|
helper.setText(R.id.tv_gold_num, item.getCoins());
|
|
helper.setText(R.id.tv_money, String.format("¥%s", item.getMoney()));
|
|
if (selectedPosition == helper.getAdapterPosition()) {
|
|
helper.setBackgroundRes(R.id.cl_item, com.xscm.moduleutil.R.drawable.bg_10_white_sele);
|
|
} else {
|
|
helper.setBackgroundRes(R.id.cl_item, com.xscm.moduleutil.R.drawable.bg_r10_white);
|
|
}
|
|
|
|
helper.getView(R.id.cl_item).setOnClickListener(v -> {
|
|
selectedPosition = helper.getAdapterPosition();
|
|
if (listener != null) {
|
|
listener.onClick(item);
|
|
}
|
|
notifyDataSetChanged();
|
|
|
|
// 隐藏输入框
|
|
if (inputBoxVisibilityListener != null) {
|
|
inputBoxVisibilityListener.onInputBoxVisibilityChanged(false);
|
|
}
|
|
});
|
|
} else if (type == ITEM_TYPE_FOOTER) {
|
|
helper.setText(R.id.tv_gold_num, "自定义");
|
|
helper.getView(R.id.tv_gold_num).setOnClickListener(v -> {
|
|
clearSelect(); // 清除所有选中状态
|
|
notifyDataSetChanged();
|
|
|
|
// 显示输入框
|
|
if (inputBoxVisibilityListener != null) {
|
|
inputBoxVisibilityListener.onInputBoxVisibilityChanged(true);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
public interface OnRechargeItemClickListener {
|
|
void onClick(RechargeBean rechargeBean);
|
|
}
|
|
|
|
public void setListener(OnRechargeItemClickListener listener) {
|
|
this.listener = listener;
|
|
}
|
|
|
|
public interface InputBoxVisibilityListener {
|
|
void onInputBoxVisibilityChanged(boolean isVisible);
|
|
}
|
|
|
|
public void setInputBoxVisibilityListener(InputBoxVisibilityListener listener) {
|
|
this.inputBoxVisibilityListener = listener;
|
|
}
|
|
|
|
/**
|
|
* 取消所有选中
|
|
*/
|
|
public void clearSelect() {
|
|
selectedPosition = -1;
|
|
notifyDataSetChanged();
|
|
}
|
|
}
|