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
package cn.flightfeather.thirdapp.module.inspectioninfo;
 
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.flightfeather.thirdapp.R;
import cn.flightfeather.thirdapp.adapter.AllRecyclerViewAdapter;
import cn.flightfeather.thirdapp.bean.vo.ProblemCategoryVo;
import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo;
import cn.flightfeather.thirdapp.util.Constant;
import cn.flightfeather.thirdapp.util.Domain;
 
/**
 * 2019.1.25
 * @author riku
 * 客户界面
 * 整改情况界面,展示客户未整改和已整改的问题列表
 */
public class ProblemChangeActivity extends AppCompatActivity {
 
 
    //<editor-fold desc="全局变量">
    public final static String ARG_PROBLEM = "problem";
    public final static String ARG_TOTAL_PROBLEM = "totalProblem";
 
    private ArrayList<ProblemlistVo> problemlistVos;
    private ArrayList<ProblemCategoryVo> problemCategories;
    private List<Integer> newSelectedTypes;
    private List<Integer> curSelectedTypes;
 
    private final String TITLE = "整改";
    private final String POPUP_TITLE = "问题类型";
    private Unbinder unbinder;
    //</editor-fold>
 
    //<editor-fold desc="lifecycle">
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置允许使用转场动画
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        setContentView(R.layout.activity_problem_change_pollution_scene);
        unbinder = ButterKnife.bind(this);
        Intent intent = getIntent();
        problemlistVos = (ArrayList<ProblemlistVo>) intent.getSerializableExtra(ARG_PROBLEM);
        problemCategories = (ArrayList<ProblemCategoryVo>) intent.getSerializableExtra(ARG_TOTAL_PROBLEM);
        newSelectedTypes = new ArrayList<>();
        curSelectedTypes = new ArrayList<>();
        initToolBar();
        initTab();
        initViewPager();
        initPopup();
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
    //</editor-fold>
 
    //<editor-fold desc="标题栏">
    @BindView(R.id.action_bar) View action_bar;
    @BindView(R.id.img_right) ImageView img_right;
    @BindView(R.id.img_left) ImageView img_left;
    @BindView(R.id.text_left) TextView text_left;
    @BindView(R.id.text_right) TextView text_right;
    @BindView(R.id.actionbar_title) TextView title;
 
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initToolBar() {
//        action_bar.setElevation(0);
//        action_bar.setBackgroundColor(Color.alpha(0));
        action_bar.findViewById(R.id.spinner_topclass_task).setVisibility(View.GONE);
//        action_bar.findViewById(R.id.img_left).setVisibility(View.GONE);
        img_right.setImageResource(R.drawable.ic_filter_list_white_36dp);
        action_bar.findViewById(R.id.ll_menu_text).setVisibility(View.GONE);
        title.setText(TITLE);
    }
 
    @OnClick(R.id.img_left)
    void onClickBack() {
        this.finish();
    }
 
    @OnClick(R.id.img_right)
    void onClickFilter() {
        popShow();
        newSelectedTypes.clear();
        newSelectedTypes.addAll(curSelectedTypes);
//        img_left.setVisibility(View.INVISIBLE);
//        img_right.setVisibility(View.INVISIBLE);
//        text_left.setVisibility(View.VISIBLE);
//        text_right.setVisibility(View.VISIBLE);
//        title.setTitle(POPUP_TITLE);
    }
    //</editor-fold>
 
    //<editor-fold desc="ViewPager初始化">
    @BindView(R.id.tabLayout)
    TabLayout tabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private List<String> tabIndicators;//tab标题
    private List<Fragment> tabFragments;//内容
    private ContentPagerAdapter contentAdapter;//viewPager内容页面适配器
 
    private void initTab(){
//        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setTabGravity(Gravity.CENTER_HORIZONTAL);
        tabLayout.setTabTextColors(ContextCompat.getColor(this, R.color.unselected), ContextCompat.getColor(this, R.color.main_text_color));
        tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.main_text_color));
        ViewCompat.setElevation(tabLayout, 2);
        tabLayout.setupWithViewPager(viewPager);//将tab和viewPager绑定
    }
 
    private ArrayList<ProblemlistVo> unChangedProblems = new ArrayList<>();
    private ArrayList<ProblemlistVo> changedProblems = new ArrayList<>();
 
    private void initViewPager(){
        this.changedProblems.clear();
        this.unChangedProblems.clear();
 
        for (int i = 0; i < problemlistVos.size(); i++) {
            ProblemlistVo problemlistVoTemp = problemlistVos.get(i);
            if (problemlistVoTemp.getIschanged()) {
                this.changedProblems.add(problemlistVoTemp);
            } else {
                this.unChangedProblems.add(problemlistVoTemp);
            }
        }
        tabIndicators = new ArrayList<>();
        tabIndicators.add("待整改问题");
        tabIndicators.add("已整改问题");
 
        tabFragments = new ArrayList<>();
        tabFragments.add(ProblemChangeFragment.newInstance(this.unChangedProblems, Constant.UNCHANGED));
        tabFragments.add(ProblemChangeFragment.newInstance(this.changedProblems, Constant.CHANGED));
 
        contentAdapter = new ContentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(contentAdapter);
    }
 
    //刷新ViewPager
    public void refreshViewPager() {
        this.changedProblems.clear();
        this.unChangedProblems.clear();
 
        for (int i = 0; i < problemlistVos.size(); i++) {
            ProblemlistVo problemlistVoTemp = problemlistVos.get(i);
            if (!problemlistVoTemp.getIschanged()) {
                this.unChangedProblems.add(problemlistVoTemp);
            } else if (!Objects.equals(problemlistVoTemp.getExtension3(), Domain.CHANGE_CHECK_PASS)) {
                this.unChangedProblems.add(problemlistVoTemp);
            } else {
                this.changedProblems.add(problemlistVoTemp);
            }
        }
        ProblemChangeFragment pf1 = (ProblemChangeFragment) tabFragments.get(0);
        ProblemChangeFragment pf2 = (ProblemChangeFragment) tabFragments.get(1);
        pf1.notifyDataSetChanged();
        pf2.notifyDataSetChanged();
    }
 
    //整改完成后,刷新问题列表
    public void refreshProblemList(ProblemlistVo problemlistVo) {
        if (problemlistVo==null) return;
        for (ProblemlistVo p : this.problemlistVos) {
            if (p.getGuid().equals(problemlistVo.getGuid())) {
                this.problemlistVos.remove(p);
                break;
            }
        }
        this.problemlistVos.add(problemlistVo);
    }
 
    /**
     * viewPager适配器
     */
    class ContentPagerAdapter extends FragmentPagerAdapter {
 
        public ContentPagerAdapter(FragmentManager fm) {
            super(fm);
        }
 
        @Override
        public Fragment getItem(int position) {
            return tabFragments.get(position);
        }
 
        @Override
        public int getCount() {
            return tabIndicators.size();
        }
 
        @Override
        public CharSequence getPageTitle(int position) {
            return tabIndicators.get(position);
        }
    }
    //</editor-fold>
 
    //<editor-fold desc="根据用户选择显示对应类型的问题">
    private void showSelectedProblemType(List<Integer> type) {
        problemlistVos.clear();
        for (int t : type) {
            problemlistVos.addAll(problemCategories.get(t).getProblemlistVos());
        }
        if (type.isEmpty()) {
            for (ProblemCategoryVo p : problemCategories) {
                problemlistVos.addAll(p.getProblemlistVos());
            }
        }
        refreshViewPager();
    }
    //</editor-fold>
 
    //<editor-fold desc="popupWindow">
    private FilterPopupWindow popupWindow;
    private boolean popupIsShowing;
 
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initPopup() {
        popupWindow = new FilterPopupWindow(this, problemCategories);
    }
 
    private void popShow() {
        popupWindow.showAtLocation(action_bar, Gravity.BOTTOM, 0, 0);
        popupIsShowing = true;
 
        setWindowAloha(0.6f);
    }
 
    private void popDismiss() {
        popupWindow.dismiss();
        popupIsShowing = false;
    }
 
    private void setWindowAloha(float f) {
        Window window=getWindow();
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.alpha = f;
        window.setAttributes(wl);
    }
 
    private void popupRefresh() {
        popupWindow.notifyDataSetChanged();
    }
 
    class FilterPopupWindow extends PopupWindow {
 
        private List<ProblemCategoryVo> datas;
        RecyclerView rv_problem_type;
        AllRecyclerViewAdapter<ProblemCategoryVo> adapter;
 
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public FilterPopupWindow(Context context, List<ProblemCategoryVo> datas) {
            super(context);
            this.datas = datas;
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            setElevation(2f);
            setBackgroundDrawable(null);
            setOutsideTouchable(true);
            setFocusable(true);
            setAnimationStyle(R.style.popwin_anim_style);
            setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss() {
                    setWindowAloha(1f);
                }
            });
            View contentView = LayoutInflater.from(ProblemChangeActivity.this).inflate(R.layout.popup_problem_selector, null, false);
            initView(contentView);
            setClickListener(contentView);
            setContentView(contentView);
        }
 
        private void initView(View view) {
            rv_problem_type = view.findViewById(R.id.rv_problem_type_2);
            adapter = new AllRecyclerViewAdapter<ProblemCategoryVo>(datas,
                    R.layout.item_problem_type,
                    ProblemChangeActivity.this) {
                @Override
                public void bindView(final AllRecyclerViewAdapter.MyViewHolder holder, final ProblemCategoryVo obj, final boolean isSelected, final int position) {
                    final Integer pos = position;
                    //填充问题类型名称
                    holder.setText(R.id.text_problem_type, obj.getProblemTypeName());
 
                    //根据问题数量设置点击事件
                    if (obj.getTotalCount() == 0) {
                        holder.setAlpha(R.id.text_problem_type, 0.2f);
                        holder.setOnClickListener(R.id.text_problem_type, null);
                    } else {
                        holder.setOnClickListener(R.id.text_problem_type, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                v.setSelected(!v.isSelected());
                                if (v.isSelected()) {
                                    newSelectedTypes.add(pos);
                                } else {
                                    newSelectedTypes.remove(pos);
                                }
                            }
                        });
                        holder.setAlpha(R.id.text_problem_type, 1f);
                    }
 
                    //根据用户的选择设置是否选择状态
                    if (curSelectedTypes.contains(pos)) {
                        holder.setSelected(R.id.text_problem_type, true);
                    } else {
                        holder.setSelected(R.id.text_problem_type, false);
                    }
                }
            };
 
            GridLayoutManager layoutManager = new GridLayoutManager(ProblemChangeActivity.this, 3);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rv_problem_type.setLayoutManager(layoutManager);
            rv_problem_type.setAdapter(adapter);
        }
 
        private void setClickListener(View v) {
            TextView text_complete = v.findViewById(R.id.text_complete);
            TextView text_cancel = v.findViewById(R.id.text_cancel);
            text_complete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popDismiss();
                    curSelectedTypes.clear();
                    curSelectedTypes.addAll(newSelectedTypes);
                    showSelectedProblemType(curSelectedTypes);
                    popupRefresh();
                }
            });
            text_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popDismiss();
                }
            });
        }
 
        public void  notifyDataSetChanged() {
            adapter.notifyDataSetChanged();
        }
    }
    //</editor-fold>
}