173 lines
6.7 KiB
Java
173 lines
6.7 KiB
Java
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.GiftPackBean;
|
||
import com.xscm.moduleutil.event.RoomGiftPackToEvent;
|
||
import com.xscm.moduleutil.utils.ImageUtils;
|
||
|
||
import org.greenrobot.eventbus.EventBus;
|
||
|
||
import java.lang.ref.WeakReference;
|
||
import java.util.List;
|
||
|
||
/**
|
||
* @Author lxj$
|
||
* @Time 2025年7月29日23:36:25$ $
|
||
* @Description 背包礼物适配器$
|
||
*/
|
||
public class GiftPackAdapter extends BaseAdapter {
|
||
private final List<GiftPackBean> 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 GiftPackAdapter(Context context, List<GiftPackBean> mDatas, int curIndex, String type) {
|
||
inflater = LayoutInflater.from(context);
|
||
this.mDatas = mDatas;
|
||
this.curIndex = curIndex;
|
||
this.mContext = context;
|
||
this.type = type;
|
||
this.gestureDetector = new MyGestureDetector(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 GiftPackBean 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<GiftPackAdapter> sAdapter = new WeakReference<>(null);
|
||
private static GiftPackBean sGiftModel;
|
||
private GiftPackAdapter mAdapter;
|
||
private GiftPackBean mGiftModel;
|
||
public void setGiftModel(GiftPackAdapter adapter, GiftPackBean gift) {
|
||
sAdapter = new WeakReference<>(adapter);
|
||
sGiftModel = gift;
|
||
}
|
||
|
||
private static final SimpleOnGestureListener sSimpleOnGestureListener = new SimpleOnGestureListener() {
|
||
@Override
|
||
public boolean onSingleTapConfirmed(MotionEvent e) {
|
||
EventBus.getDefault().post(new RoomGiftPackToEvent(sAdapter.get(), sGiftModel, 1));
|
||
return true;
|
||
}
|
||
|
||
@Override
|
||
public boolean onDoubleTap(MotionEvent e) {
|
||
EventBus.getDefault().post(new RoomGiftPackToEvent(sAdapter.get(), sGiftModel, 2));
|
||
return true;
|
||
}
|
||
};
|
||
|
||
public MyGestureDetector(Context context) {
|
||
super(context, sSimpleOnGestureListener);
|
||
setOnDoubleTapListener(sSimpleOnGestureListener);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
@Override
|
||
@SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
|
||
public View getView(int position, View convertView, ViewGroup parent) {
|
||
GiftPackAdapter.ViewHolder viewHolder;
|
||
GiftPackBean giftModel = getItem(position);
|
||
if (convertView == null) {
|
||
convertView = inflater.inflate(R.layout.item_gift_room, parent, false);
|
||
viewHolder = new GiftPackAdapter.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.integral = (TextView) convertView.findViewById(R.id.integral);
|
||
convertView.setTag(viewHolder);
|
||
} else {
|
||
viewHolder = (GiftPackAdapter.ViewHolder) convertView.getTag();
|
||
}
|
||
|
||
viewHolder.item_layout.setOnClickListener(v -> {
|
||
// RoonGiftModel clickedModel = (RoonGiftModel) v.getTag();
|
||
EventBus.getDefault().post(new RoomGiftPackToEvent(this, giftModel, 2));
|
||
|
||
});
|
||
viewHolder.integral.setVisibility(View.VISIBLE);
|
||
viewHolder.integral.setText("x"+giftModel.getNum());
|
||
//设置礼物名字
|
||
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);
|
||
}
|
||
|
||
return convertView;
|
||
}
|
||
|
||
|
||
static class ViewHolder {
|
||
public ConstraintLayout item_layout;
|
||
public TextView tv_gift_name, tv_gift_price, integral;
|
||
public ImageView iv_gift_pic;
|
||
public TextView tv_gift_change_love_values;
|
||
public ImageView ivDownOn;
|
||
public ConstraintLayout cl_iv_down_on;
|
||
}
|
||
}
|