admin
2021-05-15 2aead6275fdd1bbbd778abc0e85663a2578fab06
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package com.tejia.lijin.app.ui.message;
 
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
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.ali.auth.third.core.util.StringUtil;
import com.bumptech.glide.Glide;
import com.tejia.lijin.app.util.JumpActivityUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.wpc.library.util.common.DimenUtils;
import com.wpc.library.util.common.StringUtils;
import com.wpc.library.widget.ResizableImageView;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.entity.MainMessage;
import com.tejia.lijin.app.entity.MessageContent;
import com.tejia.lijin.app.entity.UserMessage;
import com.tejia.lijin.app.ui.dialog.AddZFBInfoDialog2;
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
import com.tejia.lijin.app.util.GlideCircleTransform;
import com.tejia.lijin.app.util.GlideRoundTransform;
import com.tejia.lijin.app.util.RecordImageTextVideo;
import com.tejia.lijin.app.util.umengCustomEvent.MessageCustomEvent;
 
import java.util.Iterator;
import java.util.List;
 
/**
 * Created by weikou2015 on 2017/12/5.
 */
 
public class MainMessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private RecyclerView mRecyclerView;
 
    private List<MainMessage> 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_WELCOME = 1003;
    private int TYPE_ACTIVITY = 1004;
    private int TYPE_MESSAGE = 1005;
 
    private DisplayImageOptions options;
 
    private IDeleteMessageListener deleteMessageListener;
 
    public MainMessageAdapter(Context context, List<MainMessage> list, IDeleteMessageListener deleteMessageListener) {
        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();
        this.deleteMessageListener = deleteMessageListener;
    }
 
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            return new MessageHolder(VIEW_FOOTER, TYPE_WELCOME);
        } else if (viewType == TYPE_HEADER) {
            return new MessageHolder(VIEW_HEADER, TYPE_WELCOME);
        } else if (viewType == TYPE_WELCOME) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_message_welcome, parent, false);
            MessageHolder holder = new MessageHolder(view, TYPE_WELCOME);
            return holder;
        } else if (viewType == TYPE_ACTIVITY) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_message_simple_img, parent, false);
            MessageHolder holder = new MessageHolder(view, TYPE_ACTIVITY);
            return holder;
        } else if (viewType == TYPE_MESSAGE) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_main_message, parent, false);
            MessageHolder holder = new MessageHolder(view, TYPE_MESSAGE);
            return holder;
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_main_message, parent, false);
            MessageHolder holder = new MessageHolder(view, TYPE_MESSAGE);
            return holder;
        }
    }
 
    /**
     * 计算内容占用屏幕的宽度
     *
     * @param content
     * @return
     */
    private static Paint pFont = null;
 
    private int computeTextWidth(float textSize, String content) {
        if (pFont == null)
            pFont = new Paint();
        pFont.setTextSize(textSize);
        Rect rect = new Rect();
//返回包围整个字符串的最小的一个Rect区域
        content = content + "-";//多算一个字的宽度
        pFont.getTextBounds(content, 0, content.length(), rect);
        return rect.width();
    }
 
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder cHolder, final int position) {
        MessageHolder holder = (MessageHolder) cHolder;
        if (!isHeaderView(position) && !isFooterView(position)) {
            final MainMessage 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);
            }
 
            if (info.getWelcomeMsg() != null) {
                holder.tv_title.setText(info.getWelcomeMsg().getTitle());
                holder.tv_welcome_content.setText(info.getWelcomeMsg().getContent());
                final SharedPreferences sp = mContext.getSharedPreferences("user", Context.MODE_PRIVATE);
                holder.ll_novice.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RecordImageTextVideo textVideo = new RecordImageTextVideo(mContext);
                        String url = textVideo.getvideoMoreUrl();
                        if (StringUtils.isEmpty(url)) {
                            url = textVideo.gettxtImgUtl();
                        }
                        Intent intent = new Intent(mContext, ShareBrowserActivity.class);
                        intent.putExtra("url", url);
                        mContext.startActivity(intent);
                    }
                });
                holder.ll_help_center.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, ShareBrowserActivity.class);
                        intent.putExtra("title", "帮助中心");
                        intent.putExtra("url", sp.getString("help", ""));
                        mContext.startActivity(intent);
                    }
                });
            } else if (info.getSpecial() != null) {
                Glide.with(mContext).load(info.getSpecial().getSubPicture()).into(holder.iv_type);
                holder.tv_type.setText(info.getSpecial().getName());
                holder.tv_time.setText(info.getSpecial().getCreatetime());
                Glide.with(mContext).load(info.getSpecial().getPicture())
                        .transform(new GlideRoundTransform(mContext, 5)).into(holder.iv_goods_img1);
//                holder.tv_coupon1.setText(info.getActivityInfo().get(0).getCouponAmount() + "元券");
                holder.tv_goods_des.setText(info.getSpecial().getRemark());
                holder.tv_goods_des.setVisibility(StringUtils.isEmpty(info.getSpecial().getRemark()) ? View.GONE : View.VISIBLE);
//                holder.tv_coupon1.setVisibility(Integer.parseInt(info.getGoodsList().get(0).getCouponAmount())
//                        == 0 ? View.GONE : View.VISIBLE);
                final MainMessage info1 = info;
                holder.iv_goods_img1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MessageCustomEvent.msgGuessLike(mContext);
                        Intent intent = null;
                        if ((!StringUtils.isEmpty(info1.getSpecial().getJumpDetail().getActivity()))
                                && info1.getSpecial().getParams() != null) {
                            try {
                                intent = new Intent(mContext, Class.forName(JumpActivityUtil.filterActivityName(info1.getSpecial().getJumpDetail().getActivity())));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            if (info1.getSpecial().getParams() != null) {
                                @SuppressWarnings("unchecked")
                                Iterator<String> its = info1.getSpecial().getParams().keys();
                                while (its.hasNext()) {
                                    String key = its.next();
                                    String value = info1.getSpecial().getParams().optString(key);
                                    intent.putExtra(key, value);
                                }
                            }
                            mContext.startActivity(intent);
                        } else if (StringUtils.isEmpty(info1.getSpecial().getJumpDetail().getActivity())
                                && info1.getSpecial().getParams() != null) {
                            intent = new Intent(mContext, ShareBrowserActivity.class);
 
                            if (info1.getSpecial().getParams() != null) {
                                @SuppressWarnings("unchecked")
                                Iterator<String> its = info1.getSpecial().getParams().keys();
                                while (its.hasNext()) {
                                    String key = its.next();
                                    String value = info1.getSpecial().getParams().optString(key);
                                    intent.putExtra(key, value);
                                }
                            }
 
                            mContext.startActivity(intent);
                        }
                    }
                });
            } else {
                UserMessage infoMsg;
                if (haveHeaderView()) {
                    if (haveFooterView() && position == mList.size() + 1) {
                        return;
                    }
                    infoMsg = mList.get(position - 1).getUserMsg();
                } else if (haveFooterView() && position == mList.size()) {
                    return;
                } else {
                    infoMsg = mList.get(position).getUserMsg();
                }
                Glide.with(mContext).load(infoMsg.getIcon()).into(holder.iv_msg_type);
                holder.tv_msg_type.setText(infoMsg.getTitle());
                holder.tv_msg_time.setText(infoMsg.getTime());
                holder.ll_content.removeAllViews();
                //计算文字宽度
                int titleLength = 0;
                //获取标题字体大小
                View item1 = LayoutInflater.from(mContext).inflate(R.layout.item_message_single_row, null);
                TextView tvTitle = item1.findViewById(R.id.tv_title);
                float textSize = tvTitle.getTextSize();
 
                for (int i = 0; i < infoMsg.getContentItems().size(); i++) {
//                int temp = info.getContentItems().get(i).getTitle().getContent().length();
//                if (temp > titleLength) {
//                    titleLength = temp;
//                }
                    int tempTitleLength = computeTextWidth(textSize, infoMsg.getContentItems().get(i).getTitle().getContent());
                    if (tempTitleLength > titleLength)
                        titleLength = tempTitleLength;
                }
 
                for (int i = 0; i < infoMsg.getContentItems().size(); i++) {
                    View item = LayoutInflater.from(mContext).inflate(R.layout.item_message_single_row, null);
                    TextView tv_title = item.findViewById(R.id.tv_title);
                    ImageView iv_portrait = item.findViewById(R.id.iv_portrait);
                    ImageView iv_input = item.findViewById(R.id.iv_input);
                    TextView tv_content = item.findViewById(R.id.tv_content);
 
//                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DimenUtils.spToPixels(
//                            titleLength * 15, mContext), ViewGroup.LayoutParams.WRAP_CONTENT);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(titleLength, ViewGroup.LayoutParams.WRAP_CONTENT);
                    params.leftMargin = DimenUtils.dip2px(mContext, 15);
                    tv_title.setLayoutParams(params);
 
                    MessageContent mContent = infoMsg.getContentItems().get(i);
                    tv_title.setTextColor(Color.parseColor(mContent.getTitle().getColor()));
                    tv_title.setText(mContent.getTitle().getContent());
 
                    if (mContent.getContent().size() > 0 && !StringUtils.isEmpty(mContent.getContent().get(0).getImg())) {
                        Glide.with(mContext).load(mContent.getContent().get(0).getImg())
                                .transform(new GlideCircleTransform(mContext)).into(iv_portrait);
                        iv_portrait.setVisibility(View.VISIBLE);
                        LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                        params1.leftMargin = 0;
                        tv_content.setLayoutParams(params1);
                    } else {
                        iv_portrait.setVisibility(View.GONE);
                    }
                    String content = "";
                    for (int j = 0; j < mContent.getContent().size(); j++) {
                        content += mContent.getContent().get(j).getContent();
                    }
                    Spannable span = new SpannableString(content);
                    String temp = "";
                    for (int k = 0; k < mContent.getContent().size(); k++) {
                        span.setSpan(new ForegroundColorSpan(Color.parseColor(mContent.getContent().get(k).getColor()))
                                , temp.length(), (temp + mContent.getContent().get(k).getContent()).length(),
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        temp += mContent.getContent().get(k).getContent();
                    }
                    tv_content.setText(span);
                    iv_input.setVisibility(mContent.isClick() ? View.VISIBLE : View.GONE);
                    holder.ll_content.addView(item);
                }
 
                final UserMessage cInfo = infoMsg;
                holder.ll_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (cInfo.getJumpDetail() != null) {
                            Intent intent = null;
                            if ((!StringUtils.isEmpty(cInfo.getJumpDetail().getActivity()))
                                    && cInfo.getParams() != null) {
                                try {
                                    intent = new Intent(mContext, Class.forName(JumpActivityUtil.filterActivityName(cInfo.getJumpDetail().getActivity())));
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                if (cInfo.getParams() != null) {
                                    @SuppressWarnings("unchecked")
                                    Iterator<String> its = cInfo.getParams().keySet().iterator();
                                    while (its.hasNext()) {
                                        String key = its.next();
                                        String value = cInfo.getParams().getString(key);
                                        intent.putExtra(key, value);
                                    }
                                }
                                mContext.startActivity(intent);
                            } else if (StringUtils.isEmpty(cInfo.getJumpDetail().getActivity())
                                    && cInfo.getParams() != null) {
                                intent = new Intent(mContext, ShareBrowserActivity.class);
 
                                if (cInfo.getParams() != null) {
                                    @SuppressWarnings("unchecked")
                                    Iterator<String> its = cInfo.getParams().keySet().iterator();
                                    while (its.hasNext()) {
                                        String key = its.next();
                                        String value = cInfo.getParams().getString(key);
                                        intent.putExtra(key, value);
                                    }
                                }
 
                                mContext.startActivity(intent);
                            }
                        }
                    }
                });
 
                holder.ll_item.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        if (!StringUtil.isEmpty(cInfo.getId())) {
                            AddZFBInfoDialog2.Builder builder = new AddZFBInfoDialog2.Builder(mContext);
                            builder.setTitle("删除提醒").setMessage("确定要删除本条消息?").setMessageTextAligin(AddZFBInfoDialog2.Builder.TEXT_ALIGIN_MIDDLE)
                                    .setPositiveButton("确定删除", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            if (deleteMessageListener != null)
                                                deleteMessageListener.delete(cInfo);
                                            dialog.dismiss();
                                        }
                                    }).create().show();
                        }
                        return false;
                    }
                });
 
            }
        }
    }
 
    @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 {
            if (mList.get(position - 1) == null)
                return TYPE_HEADER;
            if (mList.get(position - 1).getWelcomeMsg() != null) {
                return TYPE_WELCOME;
            } else if (mList.get(position - 1).getSpecial() != null) {
                return TYPE_ACTIVITY;
            } else if (mList.get(position - 1).getUserMsg() != null) {
                return TYPE_MESSAGE;
            } else {
                return TYPE_MESSAGE;
            }
        }
    }
 
    @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;
    }
 
 
    public class MessageHolder extends RecyclerView.ViewHolder {
        /*
        新人欢迎
         */
        TextView tv_title;
        TextView tv_welcome_content;
        LinearLayout ll_novice;
        LinearLayout ll_help_center;
 
        /*
        推荐内容
         */
        ImageView iv_type;
        ResizableImageView iv_goods_img1;
        TextView tv_type;
        TextView tv_time;
        //        TextView tv_coupon1;
        TextView tv_goods_des;
 
        //消息详情
        LinearLayout ll_content;
        ImageView iv_msg_type;
        TextView tv_msg_type;
        TextView tv_msg_time;
        LinearLayout ll_item;
 
        public MessageHolder(View view, int type) {
            super(view);
            if (type == TYPE_WELCOME) {
                tv_title = view.findViewById(R.id.tv_title);
                tv_welcome_content = view.findViewById(R.id.tv_welcome_content);
                ll_novice = view.findViewById(R.id.ll_novice);
                ll_help_center = view.findViewById(R.id.ll_help_center);
            } else if (type == TYPE_ACTIVITY) {
                iv_type = view.findViewById(R.id.iv_type);
                iv_goods_img1 = view.findViewById(R.id.iv_goods_img1);
                tv_type = view.findViewById(R.id.tv_type);
                tv_time = view.findViewById(R.id.tv_time);
//                tv_coupon1 = view.findViewById(R.id.tv_coupon1);
                tv_goods_des = view.findViewById(R.id.tv_goods_des);
            } else {
                ll_content = view.findViewById(R.id.ll_content);
                iv_msg_type = view.findViewById(R.id.iv_msg_type);
                tv_msg_type = view.findViewById(R.id.tv_msg_type);
                tv_msg_time = view.findViewById(R.id.tv_msg_time);
                ll_item = view.findViewById(R.id.ll_item);
            }
        }
    }
}