2025-10-20 10:16:44 +08:00
|
|
|
|
package com.xscm.moduleutil.adapter;
|
|
|
|
|
|
|
|
|
|
|
|
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.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) {
|
2025-10-24 17:52:11 +08:00
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
this.mDatas = mDatas;
|
|
|
|
|
|
this.curIndex = curIndex;
|
|
|
|
|
|
this.mContext = context;
|
|
|
|
|
|
this.type = type;
|
|
|
|
|
|
this.gestureDetector = new MyGestureDetector(mContext);
|
2025-10-24 17:52:11 +08:00
|
|
|
|
inflater = LayoutInflater.from(mContext);
|
2025-10-20 10:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 先判断数据集的大小是否足够显示满本页?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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-24 17:52:11 +08:00
|
|
|
|
public void updateData(List<RoonGiftModel> newData) {
|
|
|
|
|
|
this.mDatas.clear();
|
|
|
|
|
|
// 确保新数据都不处于选中状态
|
|
|
|
|
|
for (RoonGiftModel model : newData) {
|
|
|
|
|
|
model.setChecked(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
this.mDatas.addAll(newData);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
// private static class MyGestureDetector extends GestureDetector {
|
|
|
|
|
|
// private GiftRoomAdapter mAdapter;
|
|
|
|
|
|
// private RoonGiftModel mGiftModel;
|
|
|
|
|
|
//
|
|
|
|
|
|
// public MyGestureDetector(Context context) {
|
|
|
|
|
|
// super(context, new SimpleOnGestureListener() {
|
|
|
|
|
|
// @Override
|
|
|
|
|
|
// public boolean onSingleTapConfirmed(MotionEvent e) {
|
|
|
|
|
|
// if (mAdapter != null && mGiftModel != null) {
|
|
|
|
|
|
// EventBus.getDefault().post(new RoomGiftClickToEvent(mAdapter, mGiftModel, 1));
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
// @Override
|
|
|
|
|
|
// public boolean onDoubleTap(MotionEvent e) {
|
|
|
|
|
|
// if (mAdapter != null && mGiftModel != null) {
|
|
|
|
|
|
// EventBus.getDefault().post(new RoomGiftClickToEvent(mAdapter, mGiftModel, 2));
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// });
|
|
|
|
|
|
// setOnDoubleTapListener(getListener());
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
// public void setGiftModel(GiftRoomAdapter adapter, RoonGiftModel giftModel) {
|
|
|
|
|
|
// this.mAdapter = adapter;
|
|
|
|
|
|
// this.mGiftModel = giftModel;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
|
|
|
|
|
|
|
convertView.setTag(viewHolder);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
viewHolder = (ViewHolder) convertView.getTag();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
viewHolder.item_layout.setOnClickListener(v -> {
|
|
|
|
|
|
// RoonGiftModel clickedModel = (RoonGiftModel) v.getTag();
|
|
|
|
|
|
EventBus.getDefault().post(new RoomGiftClickToEvent(this, giftModel, 1));
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* 在给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);
|
|
|
|
|
|
// }
|
2025-10-24 17:52:11 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2025-10-20 10:16:44 +08:00
|
|
|
|
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 ConstraintLayout cl_iv_down_on;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|