package com.tejia.lijin.app.ui.recommend;
|
|
import android.content.Context;
|
import androidx.recyclerview.widget.GridLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.widget.ImageView;
|
|
import com.bumptech.glide.Glide;
|
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
|
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.entity.SpecialOffer2;
|
|
import java.util.List;
|
|
/**
|
* Created by weikou2015 on 2017/12/5.
|
*/
|
|
public class RecommendImgAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
private RecyclerView mRecyclerView;
|
|
private List<SpecialOffer2> mList;
|
private Context mContext;
|
|
private View VIEW_FOOTER;
|
private View VIEW_HEADER;
|
|
//Type
|
private int TYPE_HEADER = 1001;
|
private int TYPE_FOOTER = 1002;
|
private int TYPE_SIMPLE_IMG = 1004;
|
|
private DisplayImageOptions options;
|
|
public RecommendImgAdapter2(Context context, List<SpecialOffer2> list) {
|
this.mList = list;
|
this.mContext = context;
|
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 FadeInBitmapDisplayer(300)).build();
|
}
|
|
@Override
|
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
if (viewType == TYPE_FOOTER) {
|
return new MyHolder(VIEW_FOOTER);
|
} else if (viewType == TYPE_HEADER) {
|
return new MyHolder(VIEW_HEADER);
|
} else {
|
View view = LayoutInflater.from(mContext).inflate(R.layout.item_recommend_activity, parent, false);
|
MyHolder holder = new MyHolder(view);
|
return holder;
|
}
|
}
|
|
@Override
|
public void onBindViewHolder(RecyclerView.ViewHolder cHolder, int position) {
|
MyHolder holder = (MyHolder) cHolder;
|
if (!isHeaderView(position) && !isFooterView(position)) {
|
SpecialOffer2 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);
|
}
|
Glide.with(mContext).load(info.getPicture()).into(holder.iv_activity);
|
}
|
}
|
|
@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_SIMPLE_IMG;
|
}
|
}
|
|
@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 if (position <= 1) {
|
return ((GridLayoutManager) layoutManager).getSpanCount() / 2;
|
} else {
|
return ((GridLayoutManager) layoutManager).getSpanCount() / 3;
|
}
|
}
|
});
|
}
|
}
|
|
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;
|
}
|
|
|
public class MyHolder extends RecyclerView.ViewHolder {
|
ImageView iv_activity;
|
|
public MyHolder(View view) {
|
super(view);
|
iv_activity = view.findViewById(R.id.iv_activity);
|
}
|
}
|
}
|