admin
2021-06-05 ddff7888bf7e754d12fb5fc85a58f3012f456490
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
package com.tejia.lijin.app.ui.goldtask;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import androidx.annotation.NonNull;
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.RelativeLayout;
import android.widget.TextView;
 
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.app.hubert.guide.util.ScreenUtils;
import com.bumptech.glide.Glide;
import com.wpc.library.util.common.StringUtils;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.entity.GoldTaskList;
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
import com.tejia.lijin.app.util.JumpActivityUtil;
import com.tejia.lijin.app.util.user.UserUtil;
 
import java.util.Iterator;
import java.util.List;
 
/**
 * 金币任务 adapter
 */
public class GoldTaskAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private RecyclerView mRecyclerView;
 
    private List<GoldTaskList> mList;
    private GoldTaskActivity 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;
 
    private String type;
 
 
    public GoldTaskAdapter(GoldTaskActivity context, List<GoldTaskList> list, String type, boolean 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_goldtask, parent, false);
            ViewHolder holder = new ViewHolder(view);
            return holder;
        }
    }
 
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (!isHeaderView(position) && !isFooterView(position)) {
            final GoldTaskList 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);
            }
            final ViewHolder viewHolder = (ViewHolder) holder;
            Glide.with(mContext).load(info.picture).into(viewHolder.item_goldtask_img);
            viewHolder.item_goldtask_title.setText(info.name);
            viewHolder.item_goldtask_titlegold.setText("+" + info.totalCoin);
 
            viewHolder.item_goldtask_already.setText(info.progress);
            if (info.finishedCoin != null) {
                viewHolder.item_goldtask_already.setText(viewHolder.item_goldtask_already.getText() + "    " + (info.finishedCoin == null ? "" : info.finishedCoin));
            }
            viewHolder.item_goldtask_content.setText(info.tip);
            viewHolder.item_goldtask_jump.setText(info.btnName);
            //动态改变shape
            GradientDrawable shape = new GradientDrawable();
            shape.setCornerRadius(ScreenUtils.dp2px(mContext, 100));
            if (info.lightUp) {//按钮点亮
                //设置颜色
                shape.setStroke(ScreenUtils.dp2px(mContext, 1), Color.parseColor("#ffe5005c"));
                viewHolder.item_goldtask_jump.setTextColor(Color.parseColor("#ffe5005c"));
            } else {//按钮变灰色
                //设置颜色
                shape.setStroke(ScreenUtils.dp2px(mContext, 1), Color.parseColor("#E0E0E0"));
                viewHolder.item_goldtask_jump.setTextColor(Color.parseColor("#ff888888"));
            }
            //设置大小
            viewHolder.item_goldtask_jump.setBackground(shape);
            viewHolder.item_goldtask_rule.setText(info.ruleName);
            viewHolder.item_goldtask_rule.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (viewHolder.item_goldtask_rulelayout.getVisibility() == View.VISIBLE) {
                        viewHolder.item_goldtask_rulelayout.setVisibility(View.GONE);
                        viewHolder.item_goldtask_rule.setCompoundDrawablesWithIntrinsicBounds(
                                0, 0, R.drawable.ic_arrow_down, 0);
                        viewHolder.item_goldtask_rule.setBackground(mContext.getResources().getDrawable(R.drawable.shape_hot_search));
                    } else {
                        viewHolder.item_goldtask_rulelayout.setVisibility(View.VISIBLE);
                        viewHolder.item_goldtask_rule.setCompoundDrawablesWithIntrinsicBounds(
                                0, 0, R.drawable.ic_arrow_up, 0);
                        viewHolder.item_goldtask_rule.setBackground(mContext.getResources().getDrawable(R.drawable.shape_capital_top));
                    }
                }
            });
//            viewHolder.item_goldtask_ruletxt.setText(Html.fromHtml(info.rule) + "");
//            String string = info.rule;
            String la = info.rule.replaceAll("\\\\r\\\\n", "\n");// \r\n 替换为正常的回车
            viewHolder.item_goldtask_ruletxt.setText(la);
            viewHolder.item_goldtask_questionmark.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, ShareBrowserActivity.class);
                    intent.putExtra("url", info.ruleLink);
                    mContext.startActivity(intent);
                }
            });
            //按钮点击
            viewHolder.item_goldtask_jump.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setClickSpe(info);
                }
            });
        }
 
    }
 
    /**
     * 当列表项出现到可视界面的时候调用
     *
     * @param holder
     */
    @Override
    public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        if (holder != null && holder.getAdapterPosition() == 0 && mList.size() > 0) {
//            Log.e("eee", "onViewAttachedToWindow: ");
            mContext.onWaterView_create();//开启WaterView
        }
    }
 
    /**
     * 列表项滑出可见窗口之外
     *
     * @param holder
     */
    @Override
    public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        if (holder != null && holder.getAdapterPosition() == 0 && mList.size() > 0) {
            mContext.mNotReceived = false;
            mContext.onWaterView_reset();//关闭子view
        }
    }
 
    @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();
        }
    }
 
    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 ImageView item_goldtask_img;// 頭像
        private TextView item_goldtask_title;//每日签到
        private TextView item_goldtask_titlegold;//显示金币
        private TextView item_goldtask_already;//已签到
        private TextView item_goldtask_content;//签到说明
        private TextView item_goldtask_jump;//已签到按钮
        private TextView item_goldtask_rule;//维护规则
        private TextView item_goldtask_ruletxt;//规则说明
        private ImageView item_goldtask_questionmark;//规则网址
        private RelativeLayout item_goldtask_rulelayout;
 
 
        public ViewHolder(View convertView) {
            super(convertView);
            /*
            竖屏
             */
            item_goldtask_img = convertView.findViewById(R.id.item_goldtask_img);//
            item_goldtask_title = convertView.findViewById(R.id.item_goldtask_title);//每日签到
            item_goldtask_titlegold = convertView.findViewById(R.id.item_goldtask_titlegold);//显示金币
            item_goldtask_already = convertView.findViewById(R.id.item_goldtask_already);//已签到
            item_goldtask_content = convertView.findViewById(R.id.item_goldtask_content);//签到说明
            item_goldtask_jump = convertView.findViewById(R.id.item_goldtask_jump);//已签到按钮
            item_goldtask_rule = convertView.findViewById(R.id.item_goldtask_rule);//维护规则
            item_goldtask_ruletxt = convertView.findViewById(R.id.item_goldtask_ruletxt);//规则说明
            item_goldtask_questionmark = convertView.findViewById(R.id.item_goldtask_questionmark);//规则网址
            item_goldtask_rulelayout = convertView.findViewById(R.id.item_goldtask_rulelayout);
            /*
            横屏
             */
        }
    }
 
    /**
     * 专题列表点击
     */
    public void setClickSpe(GoldTaskList info) {
        if (info == null) {
            return;
        }
        Intent intent = null;
        if (info.jumpDetail != null && info.jumpDetail.getType().equalsIgnoreCase("baichuan") && info.params != null) {
            //判断登陆
            if (info.jumpDetail.isNeedLogin() && !isLogin()) {//
                UserUtil.jumpLogin(mContext);
                return;
            }
            AlibcShowParams alibcShowParams = new AlibcShowParams();
            alibcShowParams.setOpenType(OpenType.Auto);
            String url = info.params.getString("url");
            JumpActivityUtil.jumpBaiChuan((Activity) mContext, null, url, "", alibcShowParams);
        } else if (info.jumpDetail != null && (!StringUtils.isEmpty(info.jumpDetail.getActivity()))
                && info.params != null) {//有 跳转页面 有参数
            //判断登陆
            if (info.jumpDetail.isNeedLogin() && !isLogin()) {//
                UserUtil.jumpLogin(mContext);
                return;
            }
            try {
                intent = new Intent(mContext, Class.forName(JumpActivityUtil.filterActivityName(info.jumpDetail.getActivity())));
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (info.params != null) {
                @SuppressWarnings("unchecked")
                Iterator<String> its = info.params.keySet().iterator();
                while (its.hasNext()) {
                    String key = its.next();
                    String value = info.params.getString(key);
                    intent.putExtra(key, value);
                }
            }
            mContext.startActivity(intent);
        }
        //无 跳转页面 有参数
        else if ((info.jumpDetail == null || (StringUtils.isEmpty(info.jumpDetail.getActivity()))) && info.params != null) {
            intent = new Intent(mContext, ShareBrowserActivity.class);
            if (info.params != null) {
                @SuppressWarnings("unchecked")
                Iterator<String> its = info.params.keySet().iterator();
                while (its.hasNext()) {
                    String key = its.next();
                    String value = info.params.getString(key);
                    intent.putExtra(key, value);
                }
            }
            mContext.startActivity(intent);
        }
        ////有 跳转页面 有无参数
        else if (info.jumpDetail != null && (!StringUtils.isEmpty(info.jumpDetail.getActivity())) && info.params == null) {
            try {
                intent = new Intent(mContext, Class.forName(JumpActivityUtil.filterActivityName(info.jumpDetail.getActivity())));
            } catch (Exception e) {
                e.printStackTrace();
            }
            mContext.startActivity(intent);
        }
    }
 
    /**
     * 判断登陆
     *
     * @return 登陆状态
     */
    public boolean isLogin() {
        return  UserUtil.isLogin(mContext);
    }
}