package com.tejia.lijin.app.ui.recommend;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.widget.FrameLayout;
|
import android.widget.ImageView;
|
|
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
import com.bumptech.glide.request.RequestOptions;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.entity.TaoBaoGoodsBrief;
|
import com.tejia.lijin.app.util.GlideRoundTransform;
|
import com.tejia.lijin.app.util.goods.GoodsDetailListUtil;
|
import com.tejia.lijin.app.util.ui.GoodsRightViewHolder;
|
import com.tejia.lijin.app.util.ui.HomeUIUtil;
|
|
import java.util.List;
|
|
import androidx.recyclerview.widget.GridLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
|
import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;
|
|
/**
|
* Created by weikou2015 on 2017/12/5.
|
* 首页推荐商品
|
*/
|
|
public class RecommendGoodsAdapter 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;
|
public int TYPE_FOOTER = 1002;
|
|
private String mFrom;
|
int goodsType = 1;
|
|
public RecommendGoodsAdapter(Context context, List<TaoBaoGoodsBrief> list, String from) {
|
this.mList = list;
|
this.mContext = context;
|
this.mFrom = from;
|
}
|
|
|
@Override
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
if (viewType == TYPE_FOOTER) {
|
return new ViewHolder(VIEW_FOOTER);
|
} else if (viewType == TYPE_HEADER) {
|
return new ViewHolder(VIEW_HEADER);
|
} else {
|
View view = LayoutInflater.from(mContext).inflate(R.layout.item_goods, parent, false);
|
ViewHolder holder = new ViewHolder(view);
|
return holder;
|
}
|
}
|
|
@Override
|
public void onBindViewHolder(RecyclerView.ViewHolder holder1, int position) {
|
if (!isHeaderView(position) && !isFooterView(position)) {
|
final TaoBaoGoodsBrief info;
|
boolean showShadow = false;
|
if (haveHeaderView()) {
|
info = mList.get(position - 1);
|
if (position == 1) {
|
showShadow = true;
|
}
|
} else {
|
info = mList.get(position);
|
if (position == 0) {
|
showShadow = true;
|
}
|
}
|
|
|
ViewHolder viewHolder = (ViewHolder) holder1;
|
if (showShadow && "jingxuan".equalsIgnoreCase(mFrom)) {
|
viewHolder.fl_shadow.setVisibility(View.VISIBLE);
|
viewHolder.view_shadow.setBackground(HomeUIUtil.getHomeTopBg(mContext));
|
} else {
|
viewHolder.fl_shadow.setVisibility(View.GONE);
|
}
|
|
|
//商品详情填充
|
GoodsDetailListUtil.setGoodsDetail(mContext, info, viewHolder.goodsDetail);
|
// 图片
|
try {
|
Glide.with(mContext).load(info.getPicUrl()).apply(new RequestOptions().centerCrop()).transform(new GlideRoundTransform(mContext, 5)).transition(withCrossFade()).placeholder(R.drawable.ic_goods_default).diskCacheStrategy(DiskCacheStrategy.ALL).into(viewHolder.iv_pic);
|
} catch (IllegalArgumentException e) {
|
}
|
|
|
viewHolder.view.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
Intent intent =new Intent(mContext,GoodsDetailActivity.class);
|
intent.putExtra("goodsId", info.getGoodsId());
|
intent.putExtra("goodsType", info.getGoodsType());
|
intent.putExtra("title", info.getTitle());
|
mContext.startActivity(intent);
|
}
|
});
|
}
|
|
}
|
|
|
protected void setGoodsType(int goodsType) {
|
this.goodsType = goodsType;
|
}
|
|
@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();
|
}
|
}
|
|
private View getLayout(int layoutId) {
|
return LayoutInflater.from(mContext).inflate(layoutId, null);
|
}
|
|
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;
|
}
|
}
|
});
|
}
|
}
|
|
public 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 ViewHolder extends RecyclerView.ViewHolder {
|
|
View view;
|
//首页背景
|
FrameLayout fl_shadow;
|
View view_shadow;
|
|
/*********内容*********/
|
ImageView iv_pic;
|
|
|
GoodsRightViewHolder goodsDetail;
|
|
public ViewHolder(View convertView) {
|
super(convertView);
|
/*
|
竖屏
|
*/
|
view = convertView;
|
goodsDetail = new GoodsRightViewHolder(convertView);
|
iv_pic = convertView
|
.findViewById(R.id.iv_pic);
|
|
|
fl_shadow = convertView
|
.findViewById(R.id.fl_shadow);
|
view_shadow = convertView
|
.findViewById(R.id.view_shadow);
|
|
}
|
}
|
|
}
|