admin
2021-06-07 01e23be6118d68d38a71d186296d440eadcaa197
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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();
    }
}