admin
2021-05-17 06a80d5c4b3a971cdc1ca3d91717ec3f6e03a443
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package com.tejia.lijin.app.ui.BrandRebate;
 
import android.content.Context;
import android.content.Intent;
 
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 android.widget.LinearLayout;
import android.widget.TextView;
 
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.tejia.lijin.app.ui.recommend.GoodsDetailActivity;
import com.tejia.lijin.app.util.Constant;
import com.wpc.library.util.common.StringUtils;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.entity.BranShopInfo;
import com.tejia.lijin.app.entity.TaoBaoGoodsBrief;
import com.tejia.lijin.app.util.GlideCircleTransform;
import com.tejia.lijin.app.util.glide.GlideRoundCenterCropTransform;
import com.tejia.lijin.app.util.glide.GlideRoundedCornersTransform;
import com.tejia.lijin.app.util.goods.GoodsDetailJumpUtil;
import com.tejia.lijin.app.util.umengCustomEvent.BrandRebateEvent;
 
import java.util.List;
 
/**
 * 品牌店铺与商品混排适配器
 */
public class BrandShoplistAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private RecyclerView mRecyclerView;
 
    private List<BranShopInfo> mList;
    private Context mContext;
 
    private View view_footer;
    private View view_header;
    private View view_banner;
 
    //Type
    private int TYPE_NORMAL = 1000;
    private int TYPE_HEADER = 1001;
    public int TYPE_FOOTER = 1002;
    public int TYPE_BANNER = 1003;
 
    public boolean isHorizontal;
    public boolean isFirst;
    boolean isSearch;
    private String type;
 
    /**
     * 品牌商品列表适配器
     *
     * @param context
     * @param list
     * @param type
     * @param isSearch
     */
    public BrandShoplistAdapter(Context context, List<BranShopInfo> 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 FooterViewHolder(view_footer);
        } else if (viewType == TYPE_HEADER) {
            return new HeaderViewHolder(view_header);
        } else if (viewType == TYPE_BANNER) {
            return new BannerViewHolder(view_banner);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_brandshoplis, parent, false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }
 
 
    }
 
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {//正文内容
            final BranShopInfo info;
            int dataPosition = position;
            if (haveHeaderView()) {
                dataPosition--;
            }
 
            if (haveBannerView()) {
                dataPosition--;
            }
 
            info = mList.get(dataPosition);
            ViewHolder viewHolder = (ViewHolder) holder;
            //店铺头像
            if (!StringUtils.isEmpty(info.getIcon())) {
                Glide.with(mContext).load(info.getIcon())
                        .placeholder(R.drawable.ic_category_default)
                        .error(R.drawable.ic_category_default)
                        .transform(new GlideCircleTransform(mContext))
                        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//保存最终图片
//                        .skipMemoryCache(true)//跳过内存缓存
                        .into(viewHolder.item_brandshoplis_img0);
            } else {
                viewHolder.item_brandshoplis_img0.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            }
 
            Glide.with(viewHolder.item_brandshoplis_bg.getContext())
                    .load(BrandFragment.navBgPicture)
                    .placeholder(R.drawable.shape_addzfb_top_hint)
                    .apply(new RequestOptions().optionalTransform(new GlideRoundedCornersTransform(viewHolder.item_brandshoplis_bg.getContext(), 7.0f, GlideRoundedCornersTransform.CornerType.TOP)))
                    .into(viewHolder.item_brandshoplis_bg);
 
 
            //店铺名字
            viewHolder.item_brandshoplis_title.setText(info.getName());
//            店铺类型 天猫/淘宝
//            viewHolder.item_brandshoplis_brand.setText(info.optInt("userType") == 0 ? "淘宝" : "天猫");
//            showImageFace(info.getUserType(), viewHolder.item_brandshoplis_brand);//更具需求 不展示店铺类型图标
 
            //点击更多
            viewHolder.item_brandshoplis_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
 
                    BrandRebateEvent.brandshopdetailgoods(mContext);//品牌-点击店铺详情商品
                    //跳转 品牌店铺详情
                    Intent intent = new Intent(mContext, BrandInfoActivity.class);
                    intent.putExtra("sid", info.getId());
                    intent.putExtra("shopname", info.getName());
//                    intent.putExtra("sid", "565018977383");
                    mContext.startActivity(intent);
                }
            });
 
            if (info.getListGoods() == null || info.getListGoods().size() == 0)
                return;
            //-------------------------
            RequestOptions myOptions = new RequestOptions()
                    .transform(new GlideRoundCenterCropTransform(mContext, 5));
 
            //店铺商品
            if (info.getListGoods().size() > 0 && info.getListGoods().get(0) != null) {//第一個商品
                final TaoBaoGoodsBrief jsonshop = info.getListGoods().get(0);
                if (!StringUtils.isEmpty(jsonshop.getPicUrl())) {
                    //店铺头像
                    Glide.with(mContext).load(jsonshop.getPicUrl())
                            .placeholder(R.drawable.ic_goods_default)
                            .error(R.drawable.ic_goods_default)
                            .apply(myOptions)
                            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//保存最终图片
//                                .skipMemoryCache(true)//跳过内存缓存
                            .into(viewHolder.item_brandshoplis_img);//商品1 圖片
                } else {
                    viewHolder.item_brandshoplis_img.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                }
                if (jsonshop.getCouponInfo() == null || StringUtils.isEmpty(jsonshop.getCouponInfo().getAmount())) {
                    viewHolder.item_brandshoplis_couponLy1.setVisibility(View.GONE);
                    viewHolder.item_brandshoplis_price1.setText("¥" + jsonshop.getZkPrice());//现价1
                } else {
                    viewHolder.item_brandshoplis_couponLy1.setVisibility(View.VISIBLE);
                    viewHolder.item_brandshoplis_coupon1.setText("¥" + jsonshop.getCouponInfo().getAmount());//优惠券1
                    viewHolder.item_brandshoplis_price1.setText("¥" + jsonshop.getCouponPrice());//商品券后價1
                }
                viewHolder.item_brandshoplis_rebate1.setText("佣 " + jsonshop.getMoneyInfo().getFanliMoney());//商品返利1
                viewHolder.item_brandshoplis_img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent= new Intent(mContext, GoodsDetailActivity.class);
                        intent.putExtra("goodsId", jsonshop.getGoodsId());
                        intent.putExtra("goodsType", jsonshop.getGoodsType());
                        intent.putExtra("title", jsonshop.getTitle());
                        intent.putExtra("from", "brand");
                        mContext.startActivity(intent);
                    }
                });
            }
            //-------------------------
            if (info.getListGoods().size() > 1 && info.getListGoods().get(1) != null) {//第二个商品
                final TaoBaoGoodsBrief jsonshop2 = info.getListGoods().get(1);
                //店铺头像
                if (!StringUtils.isEmpty(jsonshop2.getPicUrl())) {
                    Glide.with(mContext).load(jsonshop2.getPicUrl())
                            .placeholder(R.drawable.ic_goods_default)
                            .error(R.drawable.ic_goods_default)
                            .apply(myOptions)
                            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//保存最终图片
//                                .skipMemoryCache(true)//跳过内存缓存
                            .into(viewHolder.item_brandshoplis_img2);//商品2 圖片
                } else {
                    viewHolder.item_brandshoplis_img2.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                }
                if (jsonshop2.getCouponInfo() == null || StringUtils.isEmpty(jsonshop2.getCouponInfo().getAmount())) {
                    viewHolder.item_brandshoplis_couponLy2.setVisibility(View.GONE);
                    viewHolder.item_brandshoplis_price2.setText("¥" + jsonshop2.getZkPrice());//现价2
                } else {
                    viewHolder.item_brandshoplis_couponLy2.setVisibility(View.VISIBLE);
                    viewHolder.item_brandshoplis_coupon2.setText("¥" + jsonshop2.getCouponInfo().getAmount());//优惠券2
                    viewHolder.item_brandshoplis_price2.setText("¥" + jsonshop2.getCouponPrice());//商品券后價2
                }
                viewHolder.item_brandshoplis_rebate2.setText("佣 " + jsonshop2.getMoneyInfo().getFanliMoney());//商品返利1
                viewHolder.item_brandshoplis_img2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, GoodsDetailActivity.class);
                        intent.putExtra("title", jsonshop2.getTitle());
                        intent.putExtra("goodsId", jsonshop2.getGoodsId());
                        intent.putExtra("goodsType", Constant.GOODS_TYPE_TB);
 
                        intent.putExtra("from", "brand");
                        mContext.startActivity(intent);
                    }
                });
            }
            //-------------------------
            if (info.getListGoods().size() > 2 && info.getListGoods().get(2) != null) {//第三个商品
                final TaoBaoGoodsBrief jsonshop3 = info.getListGoods().get(2);
                if (!StringUtils.isEmpty(jsonshop3.getPicUrl())) {
                    Glide.with(mContext).load(jsonshop3.getPicUrl())
                            .apply(myOptions)
                            .placeholder(R.drawable.ic_goods_default)
                            .error(R.drawable.ic_goods_default)
                            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)//保存最终图片
//                                .skipMemoryCache(true)//跳过内存缓存
                            .into(viewHolder.item_brandshoplis_img3);//商品3 圖片
                } else {
                    viewHolder.item_brandshoplis_img3.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                }
                if (jsonshop3.getCouponInfo() == null || StringUtils.isEmpty(jsonshop3.getCouponInfo().getAmount())) {
                    viewHolder.item_brandshoplis_couponLy3.setVisibility(View.GONE);
                    viewHolder.item_brandshoplis_price3.setText("¥" + jsonshop3.getZkPrice());//商品原價3
                } else {
                    viewHolder.item_brandshoplis_couponLy3.setVisibility(View.VISIBLE);
                    viewHolder.item_brandshoplis_coupon3.setText("¥" + jsonshop3.getCouponInfo().getAmount());//优惠券3
                    viewHolder.item_brandshoplis_price3.setText("¥" + jsonshop3.getCouponPrice());//商品券后價3
                }
                viewHolder.item_brandshoplis_rebate3.setText("佣 " + jsonshop3.getMoneyInfo().getFanliMoney());//商品返利1
                viewHolder.item_brandshoplis_img3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent =new Intent(mContext,GoodsDetailActivity.class);
                        intent.putExtra("goodsId", jsonshop3.getGoodsId());
                        intent.putExtra("goodsType", jsonshop3.getGoodsType());
                        intent.putExtra("title", jsonshop3.getTitle());
                        intent.putExtra("from", "brand");
                        mContext.startActivity(intent);
                    }
                });
            }
        }
    }
 
 
    @Override
    public int getItemCount() {
        int count = (mList == null ? 0 : mList.size());
        if (view_footer != null) {
            count++;
        }
 
        if (view_header != null) {
            count++;
        }
        if (view_banner != null) {
            count++;
        }
        return count;
    }
 
    @Override
    public int getItemViewType(int position) {
        if (isHeaderView(position)) {
            return TYPE_HEADER;
        } else if (isFooterView(position)) {
            return TYPE_FOOTER;
        } else if (isBannerView(position)) {
            return TYPE_BANNER;
        } 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 addBannerView(View bannerView) {
        if (haveBannerView()) {
            throw new IllegalStateException("bannerview has already exists!");
        } else {
            //避免出现宽度自适应
//            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//            bannerView.setLayoutParams(params);
            view_banner = bannerView;
//            ifGridLayoutManager();
            if (haveHeaderView())
                notifyItemInserted(1);
            else
                notifyItemInserted(0);
        }
    }
 
    public void removeBannerView() {
        view_banner = null;
        notifyDataSetChanged();
    }
 
    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;
    }
 
    private boolean haveBannerView() {
        return view_banner != null;
    }
 
    public boolean haveFooterView() {
        return view_footer != null;
    }
 
    private boolean isHeaderView(int position) {
        return haveHeaderView() && position == 0;
    }
 
    private boolean isBannerView(int position) {
        if (haveHeaderView() && position == 1 && haveBannerView())
            return true;
        else if (!haveHeaderView() && position == 0 && haveBannerView())
            return true;
        return false;
    }
 
    private boolean isFooterView(int position) {
        return haveFooterView() && position >= getItemCount() - 1;
    }
 
    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView item_brandshoplis_bg;//店铺背景
        private ImageView item_brandshoplis_img0;//店鋪頭像
        private TextView item_brandshoplis_title;//店铺名字
        private ImageView item_brandshoplis_brand;//店铺类型 淘寶/天貓
        private TextView item_brandshoplis_more;//更多 跳转到店铺详情
 
        private ImageView item_brandshoplis_img;//商品1 圖片
        private LinearLayout item_brandshoplis_couponLy1;//券整体布局
        private TextView item_brandshoplis_coupon1;//优惠券1
        private TextView item_brandshoplis_price1;//商品原價1
        private TextView item_brandshoplis_rebate1;//商品返利1
 
        private ImageView item_brandshoplis_img2;//商品图片2
        private LinearLayout item_brandshoplis_couponLy2;//券整体布局
        private TextView item_brandshoplis_coupon2;//優惠券2
        private TextView item_brandshoplis_price2;//商品原价2
        private TextView item_brandshoplis_rebate2;//商品返利2
 
        private ImageView item_brandshoplis_img3;//商品图片2
        private LinearLayout item_brandshoplis_couponLy3;//券整体布局
        private TextView item_brandshoplis_coupon3;//優惠券2
        private TextView item_brandshoplis_price3;//商品原价2
        private TextView item_brandshoplis_rebate3;//商品返利2
 
        public ViewHolder(View convertView) {
            super(convertView);
            /*
            竖屏
             */
            item_brandshoplis_img0 = convertView.findViewById(R.id.item_brandshoplis_img0);
            item_brandshoplis_title = convertView.findViewById(R.id.item_brandshoplis_title);
            item_brandshoplis_brand = convertView.findViewById(R.id.item_brandshoplis_brand);
            item_brandshoplis_more = convertView.findViewById(R.id.item_brandshoplis_more);
            item_brandshoplis_bg = convertView.findViewById(R.id.iv_nav_bg);
 
 
            item_brandshoplis_img = convertView.findViewById(R.id.item_brandshoplis_img);
            item_brandshoplis_coupon1 = convertView.findViewById(R.id.item_brandshoplis_coupon1);
            item_brandshoplis_price1 = convertView.findViewById(R.id.item_brandshoplis_price1);
            item_brandshoplis_rebate1 = convertView.findViewById(R.id.item_brandshoplis_rebate1);
 
            item_brandshoplis_img2 = convertView.findViewById(R.id.item_brandshoplis_img2);
            item_brandshoplis_coupon2 = convertView.findViewById(R.id.item_brandshoplis_coupon2);
            item_brandshoplis_price2 = convertView.findViewById(R.id.item_brandshoplis_price2);
            item_brandshoplis_rebate2 = convertView.findViewById(R.id.item_brandshoplis_rebate2);
 
            item_brandshoplis_img3 = convertView.findViewById(R.id.item_brandshoplis_img3);
            item_brandshoplis_coupon3 = convertView.findViewById(R.id.item_brandshoplis_coupon3);
            item_brandshoplis_price3 = convertView.findViewById(R.id.item_brandshoplis_price3);
            item_brandshoplis_rebate3 = convertView.findViewById(R.id.item_brandshoplis_rebate3);
            item_brandshoplis_couponLy1 = convertView.findViewById(R.id.item_brandshoplis_couponLy1);
            item_brandshoplis_couponLy2 = convertView.findViewById(R.id.item_brandshoplis_couponLy2);
            item_brandshoplis_couponLy3 = convertView.findViewById(R.id.item_brandshoplis_couponLy3);
 
            /*
            横屏
             */
 
        }
    }
 
 
    class HeaderViewHolder extends RecyclerView.ViewHolder {
        public HeaderViewHolder(View convertView) {
            super(convertView);
        }
    }
 
    class FooterViewHolder extends RecyclerView.ViewHolder {
        public FooterViewHolder(View convertView) {
            super(convertView);
        }
    }
 
    class BannerViewHolder extends RecyclerView.ViewHolder {
        public BannerViewHolder(View convertView) {
            super(convertView);
        }
    }
 
    private void showImageFace(int shop, ImageView tv) {
        if (shop == 11) {//淘宝
            tv.setBackground(mContext.getResources().getDrawable(R.drawable.ic_tmall));
        } else if (shop == 10) {//天猫
            tv.setBackground(mContext.getResources().getDrawable(R.drawable.icon));
        } else if (shop == 20) {//京东
            tv.setBackground(mContext.getResources().getDrawable(R.drawable.ic_jingdong));
        } else if (shop == 30) {//拼多多
            tv.setBackground(mContext.getResources().getDrawable(R.drawable.ic_pinduoduo));
        }
    }
}