package com.tejia.lijin.app.ui.timeLimitedSpike;
|
|
import android.annotation.SuppressLint;
|
import android.content.Context;
|
import android.content.Intent;
|
import androidx.databinding.DataBindingUtil;
|
import android.graphics.Color;
|
import android.graphics.Paint;
|
import android.graphics.drawable.GradientDrawable;
|
import androidx.annotation.NonNull;
|
import androidx.recyclerview.widget.GridLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.text.Spannable;
|
import android.text.SpannableString;
|
import android.text.Spanned;
|
import android.text.style.RelativeSizeSpan;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
|
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
|
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
|
import com.wpc.library.util.common.DimenUtils;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.databinding.AdapterSpikegoodslistBinding;
|
import com.tejia.lijin.app.entity.TaoBaoGoodsBrief;
|
import com.tejia.lijin.app.util.ImageUtil;
|
import com.tejia.lijin.app.util.SetLabels;
|
import com.tejia.lijin.app.util.goods.GoodsDetailJumpUtil;
|
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.util.List;
|
|
/**
|
* 限时秒杀 商品数据
|
*/
|
public class SpikeGoodsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
private RecyclerView mRecyclerView;
|
private List<TaoBaoGoodsBrief> mList;
|
private Context mContext;
|
|
private View VIEW_FOOTER;
|
private View VIEW_HEADER;
|
|
//Type
|
private int TYPE_NORMAL = 1000;
|
private int TYPE_HEADER = 1001;
|
private int TYPE_FOOTER = 1002;
|
private String mFrom;
|
private DisplayImageOptions options;
|
|
public SpikeGoodsListAdapter(Context context, List<TaoBaoGoodsBrief> list, String from) {
|
mContext = context;
|
mList = list;
|
this.mFrom = from;
|
this.options = new DisplayImageOptions.Builder()
|
.showImageForEmptyUri(R.drawable.ic_goods_default)
|
.showImageOnFail(R.drawable.ic_goods_default)
|
.showImageOnLoading(R.drawable.ic_goods_default)
|
.resetViewBeforeLoading(true).cacheInMemory(true)
|
.cacheOnDisk(true).imageScaleType(ImageScaleType.EXACTLY)
|
.considerExifParams(true)
|
.displayer(new RoundedBitmapDisplayer(DimenUtils
|
.dip2px(mContext, 5))).build();
|
}
|
|
|
@NonNull
|
@Override
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
if (viewType == TYPE_FOOTER) {
|
return new MyHolder(VIEW_FOOTER);
|
} else if (viewType == TYPE_HEADER) {
|
return new MyHolder(VIEW_HEADER);
|
} else {
|
AdapterSpikegoodslistBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext),
|
R.layout.adapter_spikegoodslist, parent, false);
|
// View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_gold_exchange, parent, false);
|
MyHolder holder = new MyHolder(binding.getRoot());
|
return holder;
|
}
|
}
|
|
@SuppressLint("SetTextI18n")
|
@Override
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
if (!isHeaderView(position) && !isFooterView(position)) {
|
final TaoBaoGoodsBrief info;
|
if (haveHeaderView()) {
|
info = mList.get(position - 1);
|
} else if (haveFooterView() && ((haveHeaderView() && position == mList.size() + 1) || !haveHeaderView() && position == mList.size())) {
|
return;
|
} else {
|
info = mList.get(position);
|
}
|
MyHolder viewHolder = (MyHolder) holder;
|
AdapterSpikegoodslistBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
|
//进度条
|
binding.tvSurplusNum.setText("余" + (Integer.parseInt(info.getCouponInfo().getLeftCount())));
|
BigDecimal apply = new BigDecimal((Integer.parseInt(info.getCouponInfo().getTotalCount()) - Integer.parseInt(info.getCouponInfo().getLeftCount())));
|
BigDecimal total = new BigDecimal(Integer.parseInt(info.getCouponInfo().getTotalCount()));
|
BigDecimal scale = apply.divide(total, 2, RoundingMode.HALF_UP);
|
BigDecimal value = scale.multiply(new BigDecimal(100));
|
binding.pbSelfbuyAndReduction.setProgress(value.intValue());
|
// binding.tvShareRedPacket.setText(info.getOtherInfo().getReduceHongBao().getName() + info.getOtherInfo().getReduceHongBao().getMoney());
|
binding.llTab.setVisibility(info.getPictureTag() == null ? View.GONE : View.VISIBLE);
|
if (info.getPictureTag() != null) {
|
GradientDrawable drawable = new GradientDrawable();
|
drawable.setCornerRadii(new float[]{DimenUtils.dip2px(mContext, 5),
|
DimenUtils.dip2px(mContext, 5), 0, 0, 0, 0, 0, 0});
|
drawable.setColor(Color.parseColor(info.getPictureTag().getBottomColor()));
|
binding.llTab.setBackgroundDrawable(drawable);
|
binding.tvTab.setText(info.getPictureTag().getContent());
|
binding.tvTab.setTextColor(Color.parseColor(info.getPictureTag().getColor()));
|
}
|
//标签
|
if (info.getLabels() != null && info.getLabels().size() > 0) {
|
binding.llLabels.setVisibility(View.VISIBLE);
|
binding.llLabels.removeAllViews();
|
SetLabels.addLabel(mContext, binding.llLabels, info.getLabels());
|
} else {
|
binding.llLabels.setVisibility(View.GONE);
|
}
|
|
ImageUtil.showImageFace(mContext, info.getTitle(), info.getShopType(), binding.tvMovieTitle1);
|
|
binding.tvNormalPrice1.setText("¥" + info.getZkPrice());
|
binding.tvNormalPrice1.setPaintFlags(binding.tvNormalPrice1.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
|
// 图片
|
// Glide.with(mContext).load(info.getPictUrl()).
|
// placeholder(R.drawable.ic_goods_default).error(R.drawable.ic_goods_default).
|
// transform(new GlideRoundTransform(mContext, 5)).into(holder.iv_pic1);
|
ImageLoader.getInstance().displayImage(info.getPicUrl(), binding.ivMovieCover1, options);
|
binding.llShop1.setVisibility(View.GONE);
|
binding.tvPrice1.setTextColor(mContext.getResources().getColor(R.color.theme));
|
if (info.getMoneyInfo().getMoneyType() == 1) {
|
binding.tvFavourable1.setBackgroundResource(R.drawable.shape_goods_list_price_fan);
|
binding.tvFavourable1.setText("返 " + info.getMoneyInfo().getFanliMoney());
|
} else {
|
binding.tvFavourable1.setBackgroundResource(R.drawable.shape_goods_list_price_reward);
|
binding.tvFavourable1.setText("奖 " + info.getMoneyInfo().getFanliMoney());
|
}
|
String disCount;
|
if (info.isHasCoupon()) {
|
binding.llCouponBg1.setVisibility(View.VISIBLE);
|
disCount = "¥" + info.getCouponPrice();
|
binding.tvCouponPrice1.setText("¥ " + info.getCouponInfo().getAmount());
|
} else {
|
disCount = "¥" + info.getZkPrice();
|
binding.llCouponBg1.setVisibility(View.GONE);
|
}
|
|
final int pos = disCount.indexOf(".");
|
Spannable span = new SpannableString(disCount);
|
span.setSpan(new RelativeSizeSpan(1.4f), 1, pos > 1 ? pos : disCount.length(),
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
// span.setSpan(new StyleSpan(Typeface.BOLD), 1, disCount.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
binding.tvPrice1.setText(span);
|
|
binding.tvSaleNum1.setTextColor((info.getSalesType() == 1 || info.getSalesType() == 4) ?
|
mContext.getResources().getColor(R.color.gray5) : info.getSalesType() == 2 ?
|
mContext.getResources().getColor(R.color.goods_sale_num_text_color_orange) :
|
mContext.getResources().getColor(R.color.goods_sale_num_text_color_blue));
|
binding.tvSaleNum1.setText(info.getSalesType() == 1 ? "月销 " + info.getSalesCount() :
|
info.getSalesType() == 2 ? "2小时销量 " + info.getSalesCount() : info.getSalesType() == 3 ?
|
"今日销量 " + info.getSalesCount() : "总销量 " + info.getSalesCount());
|
// binding.iv holder.iv_close.setVisibility(info.isRecommend() ? View.VISIBLE : View.GONE);
|
//显示最高返
|
|
binding.llHorizontalItem.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
Intent intent= GoodsDetailJumpUtil.getGoodsDetailIntent(mContext,info.getGoodsType());
|
intent.putExtra("from", mFrom);
|
intent.putExtra("title", info.getTitle());
|
intent.putExtra("id", info.getGoodsId());
|
mContext.startActivity(intent);
|
}
|
});
|
}
|
}
|
|
@Override
|
public int getItemCount() {
|
int count = (mList == null ? 0 : mList.size());
|
if (VIEW_FOOTER != null) {
|
count++;
|
}
|
|
if (VIEW_HEADER != null) {
|
count++;
|
}
|
return count;
|
}
|
|
@Override
|
public int getItemViewType(int position) {
|
if (isHeaderView(position)) {
|
return TYPE_HEADER;
|
} else if (isFooterView(position)) {
|
return TYPE_FOOTER;
|
} else {
|
return TYPE_NORMAL;
|
}
|
}
|
|
@Override
|
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
|
try {
|
if (mRecyclerView == null && mRecyclerView != recyclerView) {
|
mRecyclerView = recyclerView;
|
}
|
ifGridLayoutManager();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public void addHeaderView(View headerView) {
|
if (haveHeaderView()) {
|
throw new IllegalStateException("hearview has already exists!");
|
} else {
|
//避免出现宽度自适应
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
headerView.setLayoutParams(params);
|
VIEW_HEADER = headerView;
|
// ifGridLayoutManager();
|
notifyItemInserted(0);
|
}
|
|
}
|
|
public void addFooterView(View footerView) {
|
if (haveFooterView()) {
|
throw new IllegalStateException("footerView has already exists!");
|
} else {
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
footerView.setLayoutParams(params);
|
VIEW_FOOTER = footerView;
|
ifGridLayoutManager();
|
notifyItemInserted(getItemCount() - 1);
|
}
|
}
|
|
//
|
private void ifGridLayoutManager() {
|
if (mRecyclerView == null) return;
|
final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
|
if (layoutManager instanceof GridLayoutManager) {
|
((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
|
@Override
|
public int getSpanSize(int position) {
|
if (isHeaderView(position) || isFooterView(position)) {
|
return ((GridLayoutManager) layoutManager).getSpanCount();
|
} else {
|
return 1;
|
}
|
}
|
});
|
}
|
}
|
|
private boolean haveHeaderView() {
|
return VIEW_HEADER != null;
|
}
|
|
public boolean haveFooterView() {
|
return VIEW_FOOTER != null;
|
}
|
|
private boolean isHeaderView(int position) {
|
return haveHeaderView() && position == 0;
|
}
|
|
private boolean isFooterView(int position) {
|
return haveFooterView() && position >= getItemCount() - 1;
|
}
|
|
class MyHolder extends RecyclerView.ViewHolder {
|
public MyHolder(View itemView) {
|
super(itemView);
|
}
|
}
|
}
|