118 lines
4.2 KiB
Java
118 lines
4.2 KiB
Java
package com.xscm.modulemain.adapter;
|
||
|
||
import android.view.View;
|
||
import android.widget.ImageView;
|
||
import android.widget.TextView;
|
||
|
||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||
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.RedPacketInfo;
|
||
|
||
import java.util.Map;
|
||
import java.util.concurrent.ConcurrentHashMap;
|
||
|
||
/**
|
||
* 红包的列表适配器
|
||
*/
|
||
public class RedBagAdapter extends BaseQuickAdapter<RedPacketInfo, BaseViewHolder> {
|
||
private Map<Integer,View> viewMap;
|
||
public RedBagAdapter() {
|
||
super(R.layout.item_red_bag);
|
||
viewMap=new ConcurrentHashMap<>();
|
||
}
|
||
|
||
@Override
|
||
protected void convert(BaseViewHolder helper, RedPacketInfo item) {
|
||
viewMap.put(helper.getLayoutPosition(),helper.itemView);
|
||
ImageView iv_red_bag= helper.getView(R.id.iv_red_bag);
|
||
if (item.getIs_qiang()==1){
|
||
iv_red_bag.setImageResource(com.xscm.moduleutil.R.mipmap.red_bj_h);
|
||
helper.setText(R.id.tv_djs, "已领取");
|
||
}else {
|
||
iv_red_bag.setImageResource(com.xscm.moduleutil.R.mipmap.red);
|
||
|
||
if (item.getType()==2){
|
||
helper.setVisible(R.id.im_list_bj,true);
|
||
}else {
|
||
helper.setVisible(R.id.im_list_bj,false);
|
||
}
|
||
|
||
// 显示倒计时
|
||
if (item.getCountdown() > 0) {
|
||
helper.setVisible(R.id.tv_djs, true);
|
||
long minutes = item.getCountdown() / 60;
|
||
long seconds = item.getCountdown() % 60;
|
||
String timeFormat = String.format("%02d:%02d", minutes, seconds);
|
||
helper.setText(R.id.tv_djs, timeFormat);
|
||
} else {
|
||
helper.setVisible(R.id.tv_djs, false);
|
||
}
|
||
}
|
||
|
||
helper.setText(R.id.tv_user_name, item.getNickname());
|
||
|
||
}
|
||
|
||
/**
|
||
* 更新指定位置的倒计时显示,不刷新整个item
|
||
* @param position 列表位置
|
||
* @param countdown 倒计时时间(秒)
|
||
*/
|
||
public void updateCountdown(int position, long countdown) {
|
||
if (position >= 0 && position < getData().size()) {
|
||
// 获取对应位置的item view
|
||
View view = viewMap.get(position);
|
||
if (view != null) {
|
||
// 直接更新倒计时文本,不刷新整个item
|
||
TextView tvDjs = view.findViewById(R.id.tv_djs);
|
||
if (tvDjs != null) {
|
||
// 格式化倒计时显示
|
||
long minutes = countdown / 60;
|
||
long seconds = countdown % 60;
|
||
String timeFormat = String.format("%02d:%02d", minutes, seconds);
|
||
tvDjs.setText(timeFormat);
|
||
|
||
// 控制倒计时显示状态
|
||
if (countdown > 0) {
|
||
tvDjs.setVisibility(View.VISIBLE);
|
||
} else {
|
||
tvDjs.setVisibility(View.GONE);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 根据位置获取item view
|
||
* @param position 位置
|
||
* @return item对应的view
|
||
*/
|
||
private View getViewByPosition(int position) {
|
||
RecyclerView recyclerView = getRecyclerView();
|
||
if (recyclerView != null) {
|
||
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
|
||
if (layoutManager != null) {
|
||
// 检查 position 是否在可见范围内
|
||
if (layoutManager instanceof LinearLayoutManager) {
|
||
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
|
||
int firstVisiblePosition = linearLayoutManager.findFirstVisibleItemPosition();
|
||
int lastVisiblePosition = linearLayoutManager.findLastVisibleItemPosition();
|
||
|
||
// 确保 position 在可见范围内
|
||
if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
|
||
return layoutManager.findViewByPosition(position);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
|
||
}
|