package com.tejia.lijin.app.ui.BrandRebate;
|
|
import android.content.Intent;
|
import androidx.recyclerview.widget.GridLayoutManager;
|
import androidx.appcompat.widget.LinearLayoutCompat;
|
import androidx.recyclerview.widget.RecyclerView;
|
import android.view.LayoutInflater;
|
import android.view.View;
|
import android.view.ViewGroup;
|
import android.widget.CheckBox;
|
import android.widget.FrameLayout;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
|
import com.app.hubert.guide.util.ScreenUtils;
|
import com.bumptech.glide.Glide;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.util.GlideCircleTransform;
|
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
class BrandFootprintInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
|
private RecyclerView mRecyclerView;
|
|
private List<History> mList;
|
private BrandFootprintInfoActivity 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;
|
|
public boolean isHorizontal;
|
public boolean isFirst;
|
boolean isSearch;
|
private String type;
|
|
private boolean mSelectedMode = false;
|
private boolean mdisplay = false;
|
private Map<String, String> maps = new HashMap<>();
|
|
/**
|
* 店铺足迹列表适配器
|
*
|
* @param context
|
* @param list
|
* @param type
|
* @param isSearch
|
*/
|
public BrandFootprintInfoAdapter(BrandFootprintInfoActivity context, List<History> list, String type, boolean isSearch) {
|
this.isSearch = isSearch;
|
this.mList = list;
|
this.mContext = context;
|
this.type = type;
|
}
|
|
@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_brandfootprintinfo, parent, false);
|
ViewHolder holder = new ViewHolder(view);
|
return holder;
|
}
|
}
|
|
@Override
|
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
|
if (!isHeaderView(position) && !isFooterView(position)) {
|
final History 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);
|
}
|
ViewHolder viewHolder = (ViewHolder) holder;
|
|
// 图片
|
Glide.with(mContext).load(info.getIcon()).centerCrop().placeholder(R.drawable.ic_category_default)
|
.error(R.drawable.ic_category_default).transform(new GlideCircleTransform(mContext)).into(viewHolder.item_brandfootprintinfo_img);
|
viewHolder.item_brandfootprintinfo_title.setText(info.getName());
|
if (info.getSelected()) {//选中
|
viewHolder.item_brandfootprintinfo_cb.setChecked(true);
|
maps.put(position + "", info.getId() + "");
|
} else {//取消
|
viewHolder.item_brandfootprintinfo_cb.setChecked(false);
|
if ((info.getId() + "").equals(maps.get(position + ""))) {
|
maps.remove(position + "");
|
}
|
}
|
//全选按钮 显示
|
if (mSelectedMode) {
|
viewHolder.fl_brandfootprintinfo_selelct.setVisibility(View.VISIBLE);
|
viewHolder.item_brandfootprintinfo_item.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (((ViewHolder) holder).item_brandfootprintinfo_cb.isChecked()) {
|
((ViewHolder) holder).item_brandfootprintinfo_cb.setChecked(false);
|
info.setSelected(false);
|
if ((info.getId() + "").equals(maps.get(position + ""))) {
|
maps.remove(position + "");
|
}
|
if (maps.size() < mList.size()) {
|
mContext.brandfootprintinfo_cb.setChecked(false);
|
}
|
|
} else {
|
((ViewHolder) holder).item_brandfootprintinfo_cb.setChecked(true);
|
info.setSelected(true);
|
maps.put(position + "", info.getId() + "");
|
if (maps.size() == mList.size()) {
|
mContext.brandfootprintinfo_cb.setChecked(true);
|
}
|
}
|
}
|
});
|
|
viewHolder.v_cover.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
if (((ViewHolder) holder).item_brandfootprintinfo_cb.isChecked()) {
|
((ViewHolder) holder).item_brandfootprintinfo_cb.setChecked(false);
|
info.setSelected(false);
|
if ((info.getId() + "").equals(maps.get(position + ""))) {
|
maps.remove(position + "");
|
}
|
if (maps.size() < mList.size()) {
|
mContext.brandfootprintinfo_cb.setChecked(false);
|
}
|
|
} else {
|
((ViewHolder) holder).item_brandfootprintinfo_cb.setChecked(true);
|
info.setSelected(true);
|
maps.put(position + "", info.getId() + "");
|
if (maps.size() == mList.size()) {
|
mContext.brandfootprintinfo_cb.setChecked(true);
|
}
|
}
|
}
|
});
|
|
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) viewHolder.item_brandfootprintinfo_line.getLayoutParams();
|
// lp.leftMargin = 0;
|
lp.leftMargin = ScreenUtils.dp2px(mContext, 112);
|
viewHolder.item_brandfootprintinfo_line.setLayoutParams(lp);
|
|
}
|
//全选按钮隐藏
|
else {
|
viewHolder.fl_brandfootprintinfo_selelct.setVisibility(View.GONE);
|
viewHolder.item_brandfootprintinfo_item.setOnClickListener(new View.OnClickListener() {
|
@Override
|
public void onClick(View v) {
|
// Intent intent = new Intent(mContext, ShareBrowserActivity.class);
|
// intent.putExtra("title", info.getShopName());
|
// intent.putExtra("url", info.getShopLink());
|
// mContext.startActivity(intent);
|
//跳转 品牌店铺详情
|
Intent intent = new Intent(mContext, BrandInfoActivity.class);
|
intent.putExtra("sid", info.getId() + "");
|
intent.putExtra("shopname", info.getName());
|
mContext.startActivity(intent);
|
}
|
});
|
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) viewHolder.item_brandfootprintinfo_line.getLayoutParams();
|
// lp.leftMargin = 0;
|
lp.leftMargin = ScreenUtils.dp2px(mContext, 75);
|
viewHolder.item_brandfootprintinfo_line.setLayoutParams(lp);
|
}
|
}
|
}
|
|
|
@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;
|
}
|
}
|
});
|
}
|
}
|
|
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 ViewHolder extends RecyclerView.ViewHolder {
|
private LinearLayoutCompat item_brandfootprintinfo_item;
|
private CheckBox item_brandfootprintinfo_cb;//选择
|
private ImageView item_brandfootprintinfo_img;// 頭像
|
private TextView item_brandfootprintinfo_title;//店铺名字
|
private View item_brandfootprintinfo_line;
|
private FrameLayout fl_brandfootprintinfo_selelct;
|
private View v_cover;
|
|
public ViewHolder(View convertView) {
|
super(convertView);
|
/*
|
竖屏
|
*/
|
item_brandfootprintinfo_item = convertView.findViewById(R.id.item_brandfootprintinfo_item);
|
item_brandfootprintinfo_cb = convertView.findViewById(R.id.item_brandfootprintinfo_cb);
|
item_brandfootprintinfo_img = convertView.findViewById(R.id.item_brandfootprintinfo_img);
|
item_brandfootprintinfo_title = convertView.findViewById(R.id.item_brandfootprintinfo_title);
|
item_brandfootprintinfo_line = convertView.findViewById(R.id.item_brandfootprintinfo_line);
|
fl_brandfootprintinfo_selelct = convertView.findViewById(R.id.fl_brandfootprintinfo_selelct);
|
v_cover = convertView.findViewById(R.id.v_cover);
|
|
}
|
|
}
|
|
public boolean isSelectedMode() {
|
return mSelectedMode;
|
}
|
|
//设置 是否显示 全选按钮
|
public void setSelectedMode(boolean selectedMode) {
|
this.mSelectedMode = selectedMode;
|
// for (int i = 0; i < mList.size(); i++)
|
// mCBFlag.put(i, false);
|
// if (!this.mSelectedMode) { // false编辑完成 隐藏CheckBox 时 清空所有选中
|
// for (int i = 0; i < mList.size(); i++) {
|
// mList.get(i).setSelected(false);
|
// }
|
// }
|
notifyDataSetChanged();
|
}
|
|
/**
|
* 获取选中的数据
|
*
|
* @return
|
*/
|
public Map<String, String> getMaps() {
|
return maps;
|
}
|
|
public boolean isMdisplay() {
|
return mdisplay;
|
}
|
|
public void setMdisplay(boolean mdisplay) { //全选or全取消
|
this.mdisplay = mdisplay;
|
for (int i = 0; i < mList.size(); i++) {
|
mList.get(i).setSelected(mdisplay ? true : false);
|
if (!mdisplay) { // false 取消
|
if ((mList.get(i).getId() + "").equals(maps.get(i + "") + "")) {
|
maps.remove(i + "");
|
}
|
} else {//true 选择
|
maps.put(i + "", mList.get(i).getId() + "");
|
}
|
}
|
notifyDataSetChanged();
|
}
|
}
|