package cn.flightfeather.thirdapp.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.thirdapp.CommonApplication; import cn.flightfeather.thirdapp.R; import cn.flightfeather.thirdapp.bean.vo.TaskVo; import cn.flightfeather.thirdapp.httpservice.TaskService; import cn.flightfeather.thirdapp.util.CommonUtils; import cn.flightfeather.thirdapp.util.DialogUtil; import cn.flightfeather.thirdapp.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 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> getTaskProgress = taskService.getTaskProgress(application.getCurrentUser().getGuid()); getTaskProgress.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> 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> 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 } } }