Files
yusheng-android/BaseModule/src/main/java/com/xscm/moduleutil/adapter/GiftRoomAdapter.java
2025-11-12 16:40:26 +08:00

220 lines
9.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xscm.moduleutil.adapter;
import static android.view.View.GONE;
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.hjq.toast.ToastUtils;
import com.xscm.moduleutil.R;
import com.xscm.moduleutil.bean.RoonGiftModel;
import com.xscm.moduleutil.event.RoomGiftClickToEvent;
import com.xscm.moduleutil.utils.ImageUtils;
import org.greenrobot.eventbus.EventBus;
import java.lang.ref.WeakReference;
import java.util.List;
public class GiftRoomAdapter extends BaseAdapter {
private final List<RoonGiftModel> mDatas;
private final LayoutInflater inflater;
private final Context mContext;
private final MyGestureDetector gestureDetector;
private final String type;
/**
* 页数下标,从0开始(当前是第几页)
*/
private final int curIndex;
/**
* 每一页显示的个数
*/
private final int pageSize = 100;
public GiftRoomAdapter(Context context, List<RoonGiftModel> mDatas, int curIndex, String type) {
this.mDatas = mDatas;
this.curIndex = curIndex;
this.mContext = context;
this.type = type;
this.gestureDetector = new MyGestureDetector(mContext);
inflater = LayoutInflater.from(mContext);
}
/**
* 先判断数据集的大小是否足够显示满本页mDatas.size() > (curIndex+1)*pageSize,
* 如果够则直接返回每一页显示的最大条目个数pageSize,
* 如果不够,则有几项返回几,(mDatas.size() - curIndex * pageSize);(也就是最后一页的时候就显示剩余item)
*/
@Override
public int getCount() {
return mDatas.size() > (curIndex + 1) * pageSize ? pageSize : (mDatas.size() - curIndex * pageSize);
}
@Override
public RoonGiftModel getItem(int position) {
return mDatas.get(position + curIndex * pageSize);
}
@Override
public long getItemId(int position) {
return position + (long) curIndex * pageSize;
}
private static class MyGestureDetector extends GestureDetector {
private static WeakReference<GiftRoomAdapter> sAdapter = new WeakReference<>(null);
private static RoonGiftModel sGiftModel;
private GiftRoomAdapter mAdapter;
private RoonGiftModel mGiftModel;
public void setGiftModel(GiftRoomAdapter adapter, RoonGiftModel gift) {
sAdapter = new WeakReference<>(adapter);
sGiftModel = gift;
}
private static final SimpleOnGestureListener sSimpleOnGestureListener = new SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
EventBus.getDefault().post(new RoomGiftClickToEvent(sAdapter.get(), sGiftModel, 1));
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
EventBus.getDefault().post(new RoomGiftClickToEvent(sAdapter.get(), sGiftModel, 2));
return true;
}
};
public MyGestureDetector(Context context) {
super(context, sSimpleOnGestureListener);
setOnDoubleTapListener(sSimpleOnGestureListener);
}
}
public void updateData(List<RoonGiftModel> newData) {
this.mDatas.clear();
// 确保新数据都不处于选中状态
for (RoonGiftModel model : newData) {
model.setChecked(false);
}
this.mDatas.addAll(newData);
}
@Override
@SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
RoonGiftModel giftModel = getItem(position);
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_gift_room, parent, false);
viewHolder = new ViewHolder();
viewHolder.tv_gift_name = (TextView) convertView.findViewById(R.id.tv_gift_name);
viewHolder.tv_gift_price = (TextView) convertView.findViewById(R.id.tv_gift_price);
viewHolder.iv_gift_pic = (ImageView) convertView.findViewById(R.id.iv_gift_pic);
viewHolder.item_layout = (ConstraintLayout) convertView.findViewById(R.id.cl_gift);
viewHolder.ivDownOn = (ImageView) convertView.findViewById(R.id.iv_down_on);
viewHolder.cl_iv_down_on = (ConstraintLayout) convertView.findViewById(R.id.cl_iv_down_on);
viewHolder.iv_gift_select= (ImageView) convertView.findViewById(R.id.iv_gift_select);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.item_layout.setOnClickListener(v -> {
// RoonGiftModel clickedModel = (RoonGiftModel) v.getTag();
if (giftModel.getIs_lock()==0) {
EventBus.getDefault().post(new RoomGiftClickToEvent(this, giftModel, 1));
}else if (giftModel.getIs_lock()==1){
ToastUtils.show("当前属于爵位礼物,请开通爵位");
}
});
if (giftModel.getIs_lock()==0){
viewHolder.iv_gift_select.setVisibility(GONE);
}else {
viewHolder.iv_gift_select.setVisibility(View.VISIBLE);
}
/*
* 在给View绑定显示的数据时计算正确的position = position + curIndex * pageSize
*/
// viewHolder.tv_gift_num.setVisibility(type.equals("1") ? View.VISIBLE : View.INVISIBLE);
// viewHolder.tv_gift_change_love_values.setVisibility(View.GONE);
//设置礼物名字
viewHolder.tv_gift_name.setText(giftModel.getGift_name());
//设置礼物价格
String surplusTxt = giftModel.getGift_price();
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(surplusTxt);
//ForegroundColorSpan 为文字前景色BackgroundColorSpan为文字背景色
ForegroundColorSpan redSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.color_FFA9A9A9));
stringBuilder.setSpan(redSpan, surplusTxt.length(), surplusTxt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//修改最后两个字体的颜色
viewHolder.tv_gift_price.setText(stringBuilder);
// viewHolder.item_layout.setTag(R.id.id_gift_tag, giftModel);
//加载礼物图片
ImageUtils.loadImageView(giftModel.getBase_image(), viewHolder.iv_gift_pic);
//设置选中后的样式
if (giftModel.isChecked()) {//被选中
viewHolder.cl_iv_down_on.setBackgroundResource(R.mipmap.room_gift_bjx);
viewHolder.ivDownOn.setVisibility(View.GONE);
} else {
viewHolder.ivDownOn.setVisibility(View.GONE);
viewHolder.cl_iv_down_on.setBackgroundResource(0);
}
//设置
// //设置礼物心动值
// if (giftModel.getCardiac().equals("0")) {
// viewHolder.tv_gift_change_love_values.setBackgroundResource(R.mipmap.room_gift_xin_dong_reduce);
// viewHolder.tv_gift_change_love_values.setText(String.format("%s", giftModel.getCardiac()));
// } else {
// viewHolder.tv_gift_change_love_values.setBackgroundResource(R.mipmap.room_gift_xin_dong_add);
// viewHolder.tv_gift_change_love_values.setText(String.format("+%s", giftModel.getCardiac()));
// }
// if (giftModel.isManghe()) {
// viewHolder.tv_gift_change_love_values.setVisibility(View.GONE);
// }
if (giftModel.getGift_bag() == 10) {
viewHolder.item_layout.setBackgroundResource(R.mipmap.gift_tkzj);
viewHolder.tv_gift_name.setText("");
viewHolder.tv_gift_name.setBackgroundResource(R.mipmap.gift_name_tkzj);
} else if (giftModel.getGift_bag() == 11) {
viewHolder.tv_gift_name.setText("");
viewHolder.item_layout.setBackgroundResource(R.mipmap.gift_syzc);
viewHolder.tv_gift_name.setBackgroundResource(R.mipmap.gift_name_syzc);
} else if (giftModel.getGift_bag() == 12) {
viewHolder.tv_gift_name.setText("");
viewHolder.item_layout.setBackgroundResource(R.mipmap.gift_sjzd);
viewHolder.tv_gift_name.setBackgroundResource(R.mipmap.gift_name_skzd);
}
return convertView;
}
static class ViewHolder {
public ConstraintLayout item_layout;
public TextView tv_gift_name, tv_gift_price, tv_gift_num;
public ImageView iv_gift_pic;
public TextView tv_gift_change_love_values;
public ImageView ivDownOn;
public ImageView iv_gift_select;
public ConstraintLayout cl_iv_down_on;
}
}