package com.tejia.lijin.app.ui.gold.view;
|
|
import android.content.Context;
|
import android.content.Intent;
|
import androidx.databinding.DataBindingUtil;
|
import androidx.annotation.NonNull;
|
import androidx.recyclerview.widget.GridLayoutManager;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.databinding.AdapterGoldExchangeBinding;
|
import com.tejia.lijin.app.entity.GoldExchange;
|
import com.tejia.lijin.app.ui.gold.presenter.GoldExchangePresenter;
|
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
|
|
import java.util.List;
|
|
public class GoldExchangeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
private RecyclerView mRecyclerView;
|
GoldExchangePresenter mPresenter;
|
private List<GoldExchange> 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;
|
|
public GoldExchangeAdapter(Context context, List<GoldExchange> list, GoldExchangePresenter presenter) {
|
mContext = context;
|
mList = list;
|
mPresenter = presenter;
|
}
|
|
@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 {
|
AdapterGoldExchangeBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mContext),
|
R.layout.adapter_gold_exchange, parent, false);
|
// View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_gold_exchange, parent, false);
|
MyHolder holder = new MyHolder(binding.getRoot());
|
return holder;
|
}
|
}
|
|
@Override
|
public long getItemId(int position) {
|
return position;
|
}
|
|
@Override
|
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
|
if (!isHeaderView(position) && !isFooterView(position)) {
|
final GoldExchange 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;
|
AdapterGoldExchangeBinding binding = DataBindingUtil.getBinding(viewHolder.itemView);
|
binding.setInfo(info);
|
binding.tvRule.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
Intent intent = new Intent(mContext, ShareBrowserActivity.class);
|
intent.putExtra("url", info.getRuleLink());
|
mContext.startActivity(intent);
|
}
|
});
|
binding.flGoExchange.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (info.isNeedJump() && info.getType().equals("inviteCodePublish")) {
|
mContext.startActivity(new Intent(mContext, InviteCodeExchangeActivity.class));
|
} else {
|
mPresenter.goExchangeGoodsVerify(mContext, info.getId());
|
}
|
}
|
});
|
}
|
}
|
|
@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);
|
}
|
}
|
}
|