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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
package cn.flightfeather.thirdapp.dataanalysis;
 
 
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.icu.text.DecimalFormat;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
 
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.ping.greendao.gen.CityDao;
import com.ping.greendao.gen.DistrictDao;
import com.ping.greendao.gen.DomainitemDao;
import com.ping.greendao.gen.ProvinceDao;
import com.ping.greendao.gen.TownDao;
 
import org.greenrobot.greendao.query.QueryBuilder;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
 
import cn.flightfeather.thirdapp.CommonApplication;
import cn.flightfeather.thirdapp.R;
import cn.flightfeather.thirdapp.adapter.AllListViewAdapter;
import cn.flightfeather.thirdapp.adapter.AllRecyclerViewAdapter;
import cn.flightfeather.thirdapp.bean.City;
import cn.flightfeather.thirdapp.bean.District;
import cn.flightfeather.thirdapp.bean.Domainitem;
import cn.flightfeather.thirdapp.bean.Province;
import cn.flightfeather.thirdapp.bean.Town;
import cn.flightfeather.thirdapp.bean.vo.AreaVo;
import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo;
import cn.flightfeather.thirdapp.httpservice.ProblemListService;
import cn.flightfeather.thirdapp.util.CommonUtils;
import cn.flightfeather.thirdapp.util.Constant;
import cn.flightfeather.thirdapp.util.DateFormatter;
import cn.flightfeather.thirdapp.util.DialogUtil;
import cn.flightfeather.thirdapp.util.Domain;
import cn.flightfeather.thirdapp.util.ScreenUtils;
import cn.flightfeather.thirdapp.view.ScrollTextView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
 
/**
 * A simple {@link Fragment} subclass.
 */
public class AnysisProblemFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {
 
    private CommonApplication application;
    private Context context;
    private ProblemListService problemListService;
 
    private final static String ALLDISTRICT = "全部区县";
    private final static String ALLTOWN = "全部街道";
    private int TIME_TYPE;//0:快速选择/1:任意时段
    private String BRIEF_TIME = "本月";//初始默认选择本月
    private final static int MAX_CHART_COUNT = 5; //饼图最多显示区块数
    private final static String REMAIN_PROBLEM = "其余";
 
    private List<ProblemlistVo> problemlists;
    private PieChart mChart;
    private ArrayList<PieEntry> entries = new ArrayList<>();//饼图数据
 
    //下拉框数据
    private List<Province> allProvinceData = null;//所有省份
    private List<Province> provinceData = new ArrayList<>();//省份
 
    private List<City> allCityData = new ArrayList<>();//所有城市
    private List<City> cityData = new ArrayList<>();//城市
 
    private List<District> allDistrictData = new ArrayList<>();//所有区县
    private List<District> districtData = new ArrayList<>();//区县
 
    private List<Town> allTownData = new ArrayList<>();//所有街镇
    private List<Town> townData = new ArrayList<>();//街镇
 
    private List<Domainitem> scenseTypeData = new ArrayList<>();//场景类型
    private List<String> dateData = new ArrayList<>();//时间
    //下拉框数据适配器
    private AllListViewAdapter<Province> provinceAdapter= null;
    private AllListViewAdapter<City> cityAdapter= null;
    private AllListViewAdapter<District> districtAdapter= null;
    private AllListViewAdapter<Town> townAdapter= null;
    private AllListViewAdapter<Domainitem> scenseAdapter= null;
    //    private AllListViewAdapter<String> dateAdapter= null;
    //RecyclerView时间快速选择列表
    private AllRecyclerViewAdapter<String> dateAdapter = null;
    private AllRecyclerViewAdapter<PieEntry> entryAdapter = null;
    //
    private RecyclerView RV_quick_choose;//时间快速选择
    private RecyclerView rv_problem_type;//问题类型统计
    //
    private LinearLayout LL_any_time;
    //
    private ImageView IV_problem;
    private ImageView IV_scene_type;
    //
    private TextView TV_problem;
    private TextView TV_area;
    private TextView tv_Cancel;
    private TextView tv_Ok;
    private TextView TV_quick_choose;
    private TextView TV_any_time;
    private TextView TV_start_time;
    private TextView TV_end_time;
    private TextView TV_date_display1;
    private ScrollTextView stv_date_display2;
    //
    private PieChart PC_problem;
    //
    private Spinner SP_scense_type;
    //    private Spinner SP_date;
    private Spinner SP_province;
    private Spinner SP_city;
    private Spinner SP_district;
    private Spinner SP_town;
    //
    private FloatingActionButton FAB_menu;
    //
    private Dialog dialog;
 
 
    public AnysisProblemFragment() {
        // Required empty public constructor
    }
 
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_anysis_problem, container, false);
    }
 
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
 
        application = (CommonApplication) getActivity().getApplication();
        context = getContext();
        problemListService = application.getRetrofit().create(ProblemListService.class);
        problemlists = new ArrayList<>();
 
        initUI(view);
        initDialog();
        initPieChart(view);
        initSpinnerData();
        initSpinner(view);
        initRecyclerView(view);
    }
 
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        dialog.dismiss();
    }
 
    private void initUI(View view) {
        IV_scene_type = (ImageView) view.findViewById(R.id.IV_scense_type);
        FAB_menu = (FloatingActionButton) view.findViewById(R.id.FAB_menu);
        TV_area = (TextView) view.findViewById(R.id.TV_area);
        stv_date_display2 = (ScrollTextView) view.findViewById(R.id.stv_date_display2);
 
        FAB_menu.setOnClickListener(this);
    }
 
    /**
     * 初始化下拉框数据(包括popupWindow)
     */
    private void initSpinnerData() {
        ProvinceDao provinceDao = application.getDaoSession().getProvinceDao();
        CityDao cityDao = application.getDaoSession().getCityDao();
        DistrictDao districtDao = application.getDaoSession().getDistrictDao();
        TownDao townDao = application.getDaoSession().getTownDao();
 
        DomainitemDao domainitemDao = application.getDaoSession().getDomainitemDao();
 
        allProvinceData = provinceDao.loadAll();
        provinceData = provinceDao.loadAll();
 
        allCityData = cityDao.loadAll();
        cityData = cityDao.loadAll();
 
        allDistrictData = districtDao.loadAll();
        District emptydistrict = new District();
        emptydistrict.setDistrictname(ALLDISTRICT);
        districtData.add(emptydistrict);
 
        allTownData = townDao.loadAll();
        Town emptytown = new Town();
        emptytown.setTownname(ALLTOWN);
        townData.add(emptytown);
 
        //场景类型
        QueryBuilder<Domainitem> queryBuilder = domainitemDao.queryBuilder()
                .where(DomainitemDao.Properties.Dcguid.eq(Domain.DOMAINGUID_SCENSETYPE))
                .orderAsc(DomainitemDao.Properties.Value);
        scenseTypeData.addAll(queryBuilder.list());
 
        dateData.add(Constant.THIS_WEEK);
        dateData.add(Constant.LAST_WEEK);
        dateData.add(Constant.THIS_MONTH);
        dateData.add(Constant.LAST_MONTH);
        dateData.add(Constant.THIS_SEASON);
        dateData.add(Constant.LAST_SEASON);
        dateData.add(Constant.THIS_YEAR);
        dateData.add(Constant.LAST_YEAR);
    }
 
    private void initSpinner(View view) {
        SP_scense_type = (Spinner) view.findViewById(R.id.SP_scense_type);
        SP_scense_type.setOnItemSelectedListener(this);
//        SP_date.setOnItemSelectedListener(this);
        initAdapter();
 
//        dialog.dismiss();
        SP_province.setSelection(0);
        SP_city.setSelection(0);
        SP_district.setSelection(0);
        SP_town.setSelection(0);
        SP_scense_type.setSelection(0);//会同时通过onClick时间触发setChartTitle(),所以放在最后
    }
 
    /**
     * 问题类型分布的列表
     * @param view 问题类型分布fragment
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initRecyclerView(View view) {
        final DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        rv_problem_type = (RecyclerView) view.findViewById(R.id.rv_problem_type);
        entryAdapter = new AllRecyclerViewAdapter<PieEntry>(entries, R.layout.item_day_task_progress, context) {
            @Override
            public void bindView(AllRecyclerViewAdapter.MyViewHolder holder, PieEntry obj, boolean bool, int position) {
                float total = 0;
                for (PieEntry entry : entries) {
                    total = total + entry.getValue();
                }
                holder.setText(R.id.tv_item1, entries.get(position).getLabel())
                        .setText(R.id.tv_item2, entries.get(position).getValue() + "")
                        .setText(R.id.tv_item3, decimalFormat.format(entries.get(position).getValue()/ total * 100) + "%");
            }
        };
 
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_problem_type.setLayoutManager(layoutManager);
        rv_problem_type.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        rv_problem_type.setAdapter(entryAdapter);
    }
 
    private void initPieChart(View view) {
 
        //饼图样式设置***************************************************************************************************
        mChart = (PieChart) view.findViewById(R.id.PC_problem);
        mChart.setUsePercentValues(true);
        mChart.getDescription().setEnabled(false);
        mChart.setExtraOffsets(5, 10, 5, 5);
//        mChart.setExtraOffsets(20.f, 0.f, 20.f, 0.f);
 
        mChart.setDragDecelerationFrictionCoef(0.95f);
 
//        mChart.setCenterTextTypeface(mTfLight);
//        mChart.setCenterText(generateCenterSpannableText());
 
        mChart.setDrawHoleEnabled(true);
        mChart.setHoleColor(Color.WHITE);
 
        mChart.setTransparentCircleColor(Color.WHITE);
        mChart.setTransparentCircleAlpha(110);
 
        mChart.setHoleRadius(39f);
        mChart.setTransparentCircleRadius(41f);
 
        mChart.setDrawCenterText(true);
 
        mChart.setRotationAngle(0);
        // enable rotation of the chart by touch
        mChart.setRotationEnabled(true);
        mChart.setHighlightPerTapEnabled(true);
 
        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);
 
        // entry label styling
        mChart.setEntryLabelColor(Color.WHITE);
//        mChart.setEntryLabelTypeface(mTfRegular);
        mChart.setEntryLabelTextSize(12f);
        mChart.setDrawEntryLabels(false);
 
        //test
//        setData(new ArrayList<ProblemlistVo>());
        //
 
    }
 
    private void setData(List<ProblemlistVo> problemlists) {
        //数据初始化***************************************************************************************************
        entries.clear();
 
        PieDataSet dataSet = new PieDataSet(entries, "问题类型");
 
        //test*****
//        int count = 5;
//        int mult = 100;
//        String[] mParties = {"路面积尘", "地面局部未硬化", "站厂出入口无外围护", "无喷淋装置", REMAIN_PROBLEM};
//        for (int i = 0; i < count; i++) {
//            entries.add(new PieEntry((int) (Math.random() * mult) + mult / 5, mParties[i]));
//        }
        //*********
 
        //初始化数据
        HashMap<String, Integer> problemTypeMap = new HashMap<>();
        while (!problemlists.isEmpty()) {
            String problemType = problemlists.get(0).getTypename();
            if (problemType == null) {
                problemlists.remove(0);
                continue;
            }
 
            int problemCount = 0;
            for (int i = 0; i < problemlists.size(); i++) {
                if (Objects.equals(problemType, problemlists.get(i).getTypename())) {
                    problemCount++;
                    problemlists.remove(i);
                    i--;
                }
            }
            problemTypeMap.put(problemType, problemCount);
        }
        //按HashMap的value降序排序
        List<HashMap.Entry<String, Integer>> list = new ArrayList<>(problemTypeMap.entrySet());
        Collections.sort(list, new Comparator<HashMap.Entry<String, Integer>>() {
            @Override
            public int compare(HashMap.Entry<String, Integer> o1, HashMap.Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
//        problemTypeMap.clear();
//        problemTypeMap.entrySet().addAll(list);
        //初始化Entry,限定最多显示5个扇区,超出的合并为第5个扇区
        String remainProblemType = "";
        int remainProblemCount = 0;
        int chartcount = 0;
        for (HashMap.Entry<String, Integer> entry : list) {
            if (chartcount < MAX_CHART_COUNT - 1) {
                entries.add(new PieEntry((float)entry.getValue(), entry.getKey()));
            } else {
                remainProblemCount = remainProblemCount + entry.getValue();
                remainProblemType = entry.getKey();
            }
            chartcount++;
        }
        if (chartcount == MAX_CHART_COUNT) {
            entries.add(new PieEntry((float) remainProblemCount, remainProblemType));
        } else if (chartcount > MAX_CHART_COUNT) {
            entries.add(new PieEntry((float) remainProblemCount, REMAIN_PROBLEM));
        }
 
        // add a lot of colors
        ArrayList<Integer> colors = new ArrayList<>();
 
        for (int c : ColorTemplate.JOYFUL_COLORS)
            colors.add(c);
 
        for (int c : ColorTemplate.COLORFUL_COLORS)
            colors.add(c);
 
        for (int c : ColorTemplate.VORDIPLOM_COLORS)
            colors.add(c);
 
        for (int c : ColorTemplate.LIBERTY_COLORS)
            colors.add(c);
 
        for (int c : ColorTemplate.PASTEL_COLORS)
            colors.add(c);
 
        colors.add(ColorTemplate.getHoloBlue());
 
        dataSet.setColors(colors);
 
        //value line
        dataSet.setValueLinePart1OffsetPercentage(80.f);
        dataSet.setValueLinePart1Length(0.2f);
        dataSet.setValueLinePart2Length(0.4f);
        //dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
 
        //
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.BLACK);
//        data.setValueTypeface(mTfLight);
        if (!entries.isEmpty()) {//数据全为空时,不显示
            mChart.setData(data);
        } else {
            mChart.setData(null);
        }
        mChart.setNoDataText("暂无数据");
 
        // undo all highlights
        mChart.highlightValues(null);
 
        mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);
    }
 
    /**
     * 初始化下拉框适配器
     */
    private void initAdapter() {
        //****下面为设置适配器**********************************************
        //省份
        provinceAdapter = new AllListViewAdapter<Province>((ArrayList<Province>)provinceData, R.layout.item_scense_detail_list) {
            @Override
            public void bindView(ViewHolder holder, Province obj) {
                holder.setText(R.id.tv_item, obj.getProvincename());
            }
        };
        SP_province.setAdapter(provinceAdapter);
        provinceAdapter.notifyDataSetChanged();
        //城市
        cityAdapter = new AllListViewAdapter<City>((ArrayList<City>)cityData, R.layout.item_scense_detail_list) {
            @Override
            public void bindView(ViewHolder holder, City obj) {
                holder.setText(R.id.tv_item, obj.getCityname());
            }
        };
        SP_city.setAdapter(cityAdapter);
        cityAdapter.notifyDataSetChanged();
        //区县
        districtAdapter = new AllListViewAdapter<District>((ArrayList<District>)districtData, R.layout.item_scense_detail_list) {
            @Override
            public void bindView(ViewHolder holder, District obj) {
                holder.setText(R.id.tv_item, obj.getDistrictname());
            }
        };
        SP_district.setAdapter(districtAdapter);
        districtAdapter.notifyDataSetChanged();
        //街镇
        townAdapter = new AllListViewAdapter<Town>((ArrayList<Town>)townData, R.layout.item_scense_detail_list) {
            @Override
            public void bindView(ViewHolder holder, Town obj) {
                holder.setText(R.id.tv_item, obj.getTownname());
            }
        };
 
        SP_town.setAdapter(townAdapter);
        //场景类型
        scenseAdapter = new AllListViewAdapter<Domainitem>((ArrayList<Domainitem>) scenseTypeData, R.layout.item_spinner_simple_text) {
            @Override
            public void bindView(ViewHolder holder, Domainitem obj) {
                holder.setText(R.id.tv_item, obj.getText());
            }
        };
        SP_scense_type.setAdapter(scenseAdapter);
        scenseAdapter.notifyDataSetChanged();
        //时间
        dateAdapter = new AllRecyclerViewAdapter<String>(dateData, R.layout.item_scense_detail_list, context) {
            @Override
            public void bindView(final AllRecyclerViewAdapter.MyViewHolder holder, final String obj, boolean bool, final int position) {
                holder.setText(R.id.tv_item, obj);
                holder.setSelected(R.id.tv_item, bool);
                holder.setOnClickListener(R.id.tv_item, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dateAdapter.setAllFalse();
                        dateAdapter.setSelected(true, position);
                        dateAdapter.notifyDataSetChanged();
                        BRIEF_TIME = obj;
                        Log.e("对象名称", obj);
                        showDialog();
                        search();
                    }
                });
                if (Objects.equals(BRIEF_TIME, obj)) {
                    holder.setSelected(R.id.tv_item, true);
                }
            }
        };
        RV_quick_choose.setAdapter(dateAdapter);
        dateAdapter.notifyDataSetChanged();
        //****上面为设置适配器**********************************************
    }
 
    /**
     * 区域选择弹出框
     */
    private void initDialog() {
//        int mScreenWidth = getResources().getDisplayMetrics().widthPixels;
        // 讲布局文件转换成 view 对象
        dialog = new Dialog(context);
        dialog.setContentView(R.layout.popwindow);
        //设置dialog宽度
        Window dialogWindow = dialog.getWindow();
        assert dialogWindow != null;
        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
        final WindowManager.LayoutParams p = dialogWindow.getAttributes();
        p.width = ScreenUtils.getScreenWidth(context);
//        p.height = (int) (ScreenUtils.getScreenHeight(context) * 0.9);
        /* 获取 PopupWindow 加载布局文件中的view */
        SP_province = (Spinner) dialog.findViewById(R.id.SP_province);
        SP_city = (Spinner) dialog.findViewById(R.id.SP_city);
        SP_district = (Spinner) dialog.findViewById(R.id.SP_district);
        SP_town = (Spinner) dialog.findViewById(R.id.SP_town);
        SP_province.setOnItemSelectedListener(this);
        SP_city.setOnItemSelectedListener(this);
        SP_district.setOnItemSelectedListener(this);
        SP_town.setOnItemSelectedListener(this);
//        SP_province.setSelection(0);
//        SP_city.setSelection(0);
//        SP_district.setSelection(0);
//        SP_town.setSelection(0);
        /*设置时间选择按钮*/
        TV_quick_choose = (TextView) dialog.findViewById(R.id.TV_quick_choose);
        TV_any_time = (TextView) dialog.findViewById(R.id.TV_any_time);
        TV_quick_choose.setSelected(true);
        TV_quick_choose .setOnClickListener(this);
        TV_any_time .setOnClickListener(this);
        /*设置快速时间选择列表*/
        TV_start_time = (TextView) dialog.findViewById(R.id.TV_start_time);
        TV_end_time = (TextView) dialog.findViewById(R.id.TV_end_time);
        TV_start_time.setText(DateFormatter.dateFormat.format(CommonUtils.getTimesMonthmorning()));
        TV_end_time.setText(DateFormatter.dateFormat.format(CommonUtils.getTimesMonthnight()));
        TV_start_time.setOnClickListener(this);
        TV_end_time.setOnClickListener(this);
        //设置显示对其方式
        GridLayoutManager layoutManager = new GridLayoutManager(context, 2);
        layoutManager.setOrientation(GridLayoutManager.VERTICAL);
        layoutManager.setAutoMeasureEnabled(true);
        RV_quick_choose = (RecyclerView) dialog.findViewById(R.id.RV_quick_choose);
        RV_quick_choose.setLayoutManager(layoutManager);
        RV_quick_choose.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        /* 设置任意时间选择列表*/
        LL_any_time = (LinearLayout) dialog.findViewById(R.id.LL_any_time);
        LL_any_time.setVisibility(View.GONE);
        /*设置确定、取消按钮*/
        tv_Cancel = (TextView) dialog.findViewById(R.id.btn_cancel);
        tv_Ok = (TextView) dialog.findViewById(R.id.btn_ok);
        tv_Cancel.setOnClickListener(this);
        tv_Ok.setOnClickListener(this);
        dialog.show();
//        dialog.hide();
        // 点击会自动 PopupWindow 关闭
//        dialog.setOutsideTouchable(false);
        /* 设置 PopupWindow 出现时的动画 */
//        dialog.setAnimationStyle(R.style.anim_menu_dialog);
        /* 设置背景颜色 */
        // dialog.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
//        dialog.update();
        /* 允许 PopupWindow 获取焦点 */
//        this.dialog.setFocusable(false);
    }
 
    /**
     * 显示和隐藏Popupwindow
     */
    private void showDialog(){
        // 设置显示的位置
        if (!dialog.isShowing())
            dialog.show();
        else
            dialog.dismiss();
    }
 
    /**
     * 按照搜索条件查询问题分布结果
     */
    private void search() {
        final Dialog dialog = DialogUtil.createLoadingDialog(getActivity(), "加载中...");
        AreaVo areaVo = new AreaVo();
        areaVo.setProvincecode(((Province)SP_province.getSelectedItem()).getProvincecode());
        areaVo.setProvincename(((Province)SP_province.getSelectedItem()).getProvincename());
        areaVo.setCitycode(((City)SP_city.getSelectedItem()).getCitycode());
        areaVo.setCityname(((City)SP_city.getSelectedItem()).getCityname());
        areaVo.setDistrictcode(((District)SP_district.getSelectedItem()).getDistrictcode());
        areaVo.setDistrictname(((District)SP_district.getSelectedItem()).getDistrictname());
        areaVo.setTowncode(((Town) SP_town.getSelectedItem()).getTowncode());
        areaVo.setTownname(((Town) SP_town.getSelectedItem()).getTownname());
        areaVo = gettime(areaVo);
        int position = SP_scense_type.getSelectedItemPosition();
        areaVo.setScensetypeid(scenseTypeData.get(position).getValue());
        Call<List<ProblemlistVo>> getProblemByArea = problemListService.getByArea(areaVo);
        getProblemByArea.enqueue(new Callback<List<ProblemlistVo>>() {
            @Override
            public void onResponse(Call<List<ProblemlistVo>> call, Response<List<ProblemlistVo>> response) {
                if (response.body() != null) {
                    problemlists = response.body();
                }
                setData(problemlists);
                entryAdapter.notifyDataSetChanged();//刷新列表
                DialogUtil.closeDialog(dialog);
            }
 
            @Override
            public void onFailure(Call<List<ProblemlistVo>> call, Throwable t) {
                setData(problemlists);
                DialogUtil.closeDialog(dialog);
            }
        });
 
        setChartTitle();//刷新标题
        setDateDisplay();//刷新时间
    }
 
    /**
     * 设定饼图标题及图标
     */
    private void setChartTitle() {
        String title = ((Province)SP_province.getSelectedItem()).getProvincename();
        if (!Objects.equals(title, ((City) SP_city.getSelectedItem()).getCityname()))
            title = title + ((City) SP_city.getSelectedItem()).getCityname();
        if (!Objects.equals(((District)SP_district.getSelectedItem()).getDistrictname(), ALLDISTRICT))
            title = title + ((District)SP_district.getSelectedItem()).getDistrictname();
        if (!Objects.equals(((Town)SP_town.getSelectedItem()).getTownname(), ALLTOWN))
            title = title + ((Town)SP_town.getSelectedItem()).getTownname();
        title = title + "问题类型分布情况";
        TV_area.setText(title);
        IV_scene_type.setImageResource(CommonUtils.getIconBySceneType(((Domainitem)SP_scense_type.getSelectedItem()).getValue()));
    }
 
    /**
     * 获取所选的时间
     * @param areaVo RequestBody,查询问题列表
     * @return
     */
    private AreaVo gettime(AreaVo areaVo){
        if (TIME_TYPE == 0) {//快速选择
            Date date1;
            Date date2;
            switch (BRIEF_TIME) {
                case Constant.THIS_WEEK:
                    date1 = CommonUtils.getTimesWeekmorning();
                    date2 = CommonUtils.getTimesWeeknight();
                    break;
                case Constant.LAST_WEEK:
                    date1 = CommonUtils.getTimesLastWeekmorning();
                    date2 = CommonUtils.getTimesLastWeeknight();
                    break;
                case Constant.THIS_MONTH:
                    date1 = CommonUtils.getTimesMonthmorning();
                    date2 = CommonUtils.getTimesMonthnight();
                    break;
                case Constant.LAST_MONTH:
                    date1 = CommonUtils.getTimesLastMonthmorning();
                    date2 = CommonUtils.getTimesLastMonthnight();
                    break;
                case Constant.THIS_SEASON:
                    date1 = CommonUtils.getTimesSeasonmorning();
                    date2 = CommonUtils.getTimesSeasonnight();
                    break;
                case Constant.LAST_SEASON:
                    date1 = CommonUtils.getTimesLastSeasonmorning();
                    date2 = CommonUtils.getTimesLastSeasonnight();
                    break;
                case Constant.THIS_YEAR:
                    date1 = CommonUtils.getCurrentYearStartTime();
                    date2 = CommonUtils.getCurrentYearEndTime();
                    break;
                case Constant.LAST_YEAR:
                    date1 = CommonUtils.getLastYearStartTime();
                    date2 = CommonUtils.getLastYearEndTime();
                    break;
                default:
                    date1 = CommonUtils.getTimesMonthmorning();
                    date2 = CommonUtils.getTimesMonthnight();
                    break;
            }
            areaVo.setStarttime(DateFormatter.dateTimeFormat2.format(date1));
            areaVo.setEndtime(DateFormatter.dateTimeFormat2.format(date2));
        }
        else if (TIME_TYPE == 1){//任意时段
            areaVo.setStarttime(TV_start_time.getText().toString());
            areaVo.setEndtime(TV_end_time.getText().toString());
            return areaVo;
        }
        return areaVo;
    }
 
    /**
     * 设置所选时间的显示
     */
    private void setDateDisplay() {
        if (TIME_TYPE == 0) {//快速选择
//            TV_date_display1.setTitle(BRIEF_TIME);
            stv_date_display2.setText(BRIEF_TIME);
        } else if (TIME_TYPE == 1) {//任意时段
//            TV_date_display1.setTitle(TV_start_time.getTitle().toString());
            stv_date_display2.setText(TV_start_time.getText().toString() + " ~ " + TV_end_time.getText().toString());
        }
    }
 
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.FAB_menu:
                showDialog();
//                FAB_menu.setImageResource(dialog.isShowing()? R.drawable.icon_close_white : R.drawable.icon_open_white);
                break;
            case R.id.btn_cancel:
                showDialog();
                break;
            case R.id.btn_ok:
                search();
                showDialog();
                break;
            case R.id.TV_quick_choose:
                TV_quick_choose.setSelected(true);
                TV_any_time.setSelected(false);
                TV_quick_choose.setTextColor(Color.WHITE);
                TV_any_time.setTextColor(Color.BLACK);
                RV_quick_choose.setVisibility(View.VISIBLE);
                LL_any_time.setVisibility(View.GONE);
                TIME_TYPE = 0;
                break;
            case R.id.TV_any_time:
                TV_any_time.setSelected(true);
                TV_quick_choose.setSelected(false);
                TV_any_time.setTextColor(Color.WHITE);
                TV_quick_choose.setTextColor(Color.BLACK);
                LL_any_time.setVisibility(View.VISIBLE);
                RV_quick_choose.setVisibility(View.GONE);
                TIME_TYPE = 1;
                break;
            //开始时间
            case R.id.TV_start_time:
                dialog.hide();
                Calendar calendar1 = Calendar.getInstance();
                new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener(){
 
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                        String result = "";
                        result += year + "." + (month+1) + "." + day + ".";
                        Toast.makeText(getContext(), result, Toast.LENGTH_SHORT).show();
                        dialog.show();
                        TV_start_time.setText(result);
                    }
                }
                        ,calendar1.get(Calendar.YEAR)
                        ,calendar1.get(Calendar.MONTH)
                        ,calendar1.get(Calendar.DAY_OF_MONTH)).show();
                break;
            //结束时间
            case R.id.TV_end_time:
                dialog.hide();
                Calendar calendar2 = Calendar.getInstance();
                new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener(){
 
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                        String result = "";
                        result += year + "." + (month+1) + "." + day + ".";
                        Toast.makeText(getActivity(), result, Toast.LENGTH_SHORT).show();
                        dialog.show();
                        TV_end_time.setText(result);
                    }
                }
                        ,calendar2.get(Calendar.YEAR)
                        ,calendar2.get(Calendar.MONTH)
                        ,calendar2.get(Calendar.DAY_OF_MONTH)).show();
                break;
        }
    }
 
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.SP_province:
                cityData.clear();
                for (int y = 0; y < allCityData.size(); y++){
                    if(Objects.equals(allCityData.get(y).getPronvinceid(), ((Province) SP_province.getSelectedItem()).getProvinceid()))
                        cityData.add(allCityData.get(y));
                }
                cityAdapter.notifyDataSetChanged();
                break;
            case R.id.SP_city:
                districtData.clear();
                District emptydistrict = new District();
                emptydistrict.setDistrictname(ALLDISTRICT);
                districtData.add(emptydistrict);
                for (int y = 0; y < allDistrictData.size(); y++){
                    if(Objects.equals(allDistrictData.get(y).getCityid(), ((City) SP_city.getSelectedItem()).getCityId()))
                        districtData.add(allDistrictData.get(y));
                }
                districtAdapter.notifyDataSetChanged();
                break;
            case R.id.SP_district:
                townData.clear();
                Town emptytown = new Town();
                emptytown.setTownname(ALLTOWN);
                townData.add(emptytown);
                for (int y = 0; y < allTownData.size(); y++){
                    if(Objects.equals(allTownData.get(y).getDistrictid(), ((District) SP_district.getSelectedItem()).getDistrictid()))
                        townData.add(allTownData.get(y));
                }
                townAdapter.notifyDataSetChanged();
                break;
            case R.id.SP_town:
//                setChartTitle();
                break;
            case R.id.SP_scense_type:
                search();
//                setDateDisplay();
                break;
        }
    }
 
    @Override
    public void onNothingSelected(AdapterView<?> parent) {
 
    }
}