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
356
357
358
359
360
361
362
363
364
package com.tejia.lijin.app.ui.message;
 
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
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.tejia.lijin.app.R;
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 java.util.Iterator;
import java.util.List;
 
public class UserMessageAdapter extends RecyclerView.Adapter {
    private RecyclerView mRecyclerView;
 
    private List<UserMessage> 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;
 
    private DisplayImageOptions options;
    private IDeleteMessageListener deleteMessageListener;
 
    public UserMessageAdapter(Context context, List<UserMessage> 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 MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            return new MyHolder(VIEW_FOOTER);
        } else if (viewType == TYPE_HEADER) {
            return new MyHolder(VIEW_HEADER);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_user_message, parent, false);
            MyHolder holder = new MyHolder(view);
            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 holder, int position) {
        final MyHolder viewHolder = (MyHolder) holder;
        if (!isHeaderView(position) && !isFooterView(position)) {
            UserMessage info;
            if (haveHeaderView()) {
                if (haveFooterView() && position == mList.size() + 1) {
                    return;
                }
                info = mList.get(position - 1);
            } else if (haveFooterView() && position == mList.size()) {
                return;
            } else {
                info = mList.get(position);
            }
            Glide.with(mContext).load(info.getIcon()).into(viewHolder.iv_msg_type);
            viewHolder.tv_msg_type.setText(info.getTitle());
            viewHolder.tv_msg_time.setText(info.getTime());
            viewHolder.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 < info.getContentItems().size(); i++) {
//                int temp = info.getContentItems().get(i).getTitle().getContent().length();
//                if (temp > titleLength) {
//                    titleLength = temp;
//                }
                int tempTitleLength = computeTextWidth(textSize, info.getContentItems().get(i).getTitle().getContent());
                if (tempTitleLength > titleLength)
                    titleLength = tempTitleLength;
            }
            for (int i = 0; i < info.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);
                TextView tv_content = item.findViewById(R.id.tv_content);
                ImageView iv_input = item.findViewById(R.id.iv_input);
 
//                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DimenUtils.spToPixels(
//                        titleLength * 3, mContext), ViewGroup.LayoutParams.WRAP_CONTENT);
 
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        titleLength, ViewGroup.LayoutParams.WRAP_CONTENT);
 
                params.leftMargin = DimenUtils.dip2px(mContext, 15);
                params.rightMargin = 0;
                tv_title.setLayoutParams(params);
 
                MessageContent mContent = info.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);
                viewHolder.ll_content.addView(item);
            }
            final UserMessage cInfo = info;
            viewHolder.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);
                        }
                    }
                }
            });
 
            viewHolder.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 {
            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;
    }
 
 
    public class MyHolder extends RecyclerView.ViewHolder {
        LinearLayout ll_item;
        LinearLayout ll_content;
        ImageView iv_msg_type;
        TextView tv_msg_type;
        TextView tv_msg_time;
 
        public MyHolder(View convertView) {
            super(convertView);
            ll_item = convertView.findViewById(R.id.ll_item);
            ll_content = convertView.findViewById(R.id.ll_content);
            iv_msg_type = convertView.findViewById(R.id.iv_msg_type);
            tv_msg_type = convertView.findViewById(R.id.tv_msg_type);
            tv_msg_time = convertView.findViewById(R.id.tv_msg_time);
        }
    }
}