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
package cn.flightfeather.thirdappmodule.module.dataanalysis;
 
 
import android.app.Dialog;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
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 java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
import cn.flightfeather.thirdappmodule.CommonApplication;
import cn.flightfeather.thirdappmodule.R;
import cn.flightfeather.thirdappmodule.bean.vo.TaskVo;
import cn.flightfeather.thirdappmodule.httpservice.TaskService;
import cn.flightfeather.thirdappmodule.util.CommonUtils;
import cn.flightfeather.thirdappmodule.util.DialogUtil;
import cn.flightfeather.thirdappmodule.view.SectionDecoration;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
 
/**
 * @author riku
 * 任务进度管理界面
 * 按照时间分类,先展示所有的顶层任务
 */
public class AnysisProgressFragment extends Fragment implements View.OnClickListener {
 
    private CommonApplication application;
    private Context context;
 
    private TaskService taskService;//任务信息接口
    private Boolean visible = true;
 
    private TextView TV_task_progress;
    private ImageView image_task_progress;//任务类型图片
    private RecyclerView recyclerView_task_progress;//顶层任务列表
    private LinearLayout LL_task_progress;
 
    private List<TaskVo> taskProgressVoList;
    private TaskProgressAdapter taskProgressAdapter;
 
    Dialog dialog = null;//加载弹出框
 
 
    public AnysisProgressFragment() {
        // Required empty public constructor
    }
 
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_analysis_progress_2, container, false);
    }
 
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
 
        application = (CommonApplication) getActivity().getApplication();
        taskService = application.getRetrofit().create(TaskService.class);
        context = getContext();
 
        dialog = DialogUtil.createLoadingDialog(getActivity(), "Loading...");
        initUI(view);
        initData();
    }
 
    private void initUI(View view){
        TV_task_progress = (TextView) view.findViewById(R.id.TV_task_progress);
        image_task_progress = (ImageView) view.findViewById(R.id.IV_task_progress);
        image_task_progress.setOnClickListener(this);
        recyclerView_task_progress = (RecyclerView) view.findViewById(R.id.RV_task_progress);
        LL_task_progress = (LinearLayout) view.findViewById(R.id.LL_task_progress);
//        LL_task_progress.setVisibility(View.GONE);
    }
 
 
    private void initData(){
        taskProgressVoList = new ArrayList<>();
        Call<List<TaskVo>> getTaskProgress = taskService.getTaskProgress(application.getCurrentUser().getGuid());
        getTaskProgress.enqueue(new Callback<List<TaskVo>>() {
            @Override
            public void onResponse(Call<List<TaskVo>> call, Response<List<TaskVo>> response) {
                if (response.body() != null){
                    taskProgressVoList = response.body();
                    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView_task_progress.setLayoutManager(layoutManager);
                    taskProgressAdapter = new TaskProgressAdapter(taskProgressVoList, context);
                    recyclerView_task_progress.setAdapter(taskProgressAdapter);
                    recyclerView_task_progress.addItemDecoration(new SimplePaddingDecoration(context));//添加分割线
 
                    //region 添加时间分类标题
                    recyclerView_task_progress.addItemDecoration(new SectionDecoration(context, new SectionDecoration.DecorationCallback() {
                        @Override
                        public long getGroupId(int position) {
                            Date sTime = taskProgressVoList.get(position).getStarttime();
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime(sTime);
                            calendar.set(Calendar.DAY_OF_MONTH, 1);
                            calendar.set(Calendar.HOUR_OF_DAY, 0);
                            calendar.set(Calendar.MINUTE, 0);
                            calendar.set(Calendar.SECOND, 0);
                            return calendar.getTimeInMillis();
                        }
 
                        @Override
                        public String getGroupFirstLine(int position) {
                            String name = taskProgressVoList.get(position).getName();
                            String title = CommonUtils.getSubStr(name, 0, "月");
                            return title;
                        }
                    }));
                    //endregion
 
                    DialogUtil.closeDialog(dialog);//关闭加载框
                }
            }
 
            @Override
            public void onFailure(Call<List<TaskVo>> call, Throwable t) {
                DialogUtil.closeDialog(dialog);
            }
        });
 
    }
 
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
//        taskProgressAdapter.notifyDataSetChanged();
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.image_task_progress:
//                LL_task_progress.setVisibility(visible? View.VISIBLE : View.GONE);
//                visible = !visible;
////                Toast.makeText(context, "dianji", Toast.LENGTH_SHORT).show();
//                break;
 
        }
    }
 
    public class SimplePaddingDecoration extends RecyclerView.ItemDecoration {
 
        private int dividerHeight;
 
 
        public SimplePaddingDecoration(Context context) {
            dividerHeight = context.getResources().getDimensionPixelSize(R.dimen.divider_height);
        }
 
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.bottom = dividerHeight;//类似加了一个bottom padding
        }
    }
}