package cn.flightfeather.thirdapp.module.home; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; 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.RelativeLayout; import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationListener; import com.amap.api.services.weather.LocalWeatherForecastResult; import com.amap.api.services.weather.LocalWeatherLive; import com.amap.api.services.weather.LocalWeatherLiveResult; import com.amap.api.services.weather.WeatherSearch; import com.amap.api.services.weather.WeatherSearchQuery; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import cn.flightfeather.thirdapp.CommonApplication; import cn.flightfeather.thirdapp.R; import cn.flightfeather.thirdapp.bean.entity.Evaluation; import cn.flightfeather.thirdapp.bean.vo.LastSubtaskPack; import cn.flightfeather.thirdapp.bean.entity.Subtask; import cn.flightfeather.thirdapp.bean.vo.TaskPack; import cn.flightfeather.thirdapp.bean.vo.InspectionVo; import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo; import cn.flightfeather.thirdapp.bean.vo.TaskVo; import cn.flightfeather.thirdapp.httpservice.EvaluationService; import cn.flightfeather.thirdapp.httpservice.InspectionService; import cn.flightfeather.thirdapp.httpservice.TaskService; import cn.flightfeather.thirdapp.util.Constant; import cn.flightfeather.thirdapp.util.DateFormatter; import cn.flightfeather.thirdapp.util.Domain; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; /** * A simple {@link Fragment} subclass. */ public class HomeFragment extends Fragment implements WeatherSearch.OnWeatherSearchListener, View.OnClickListener { private TextView tv_location; private ImageView iv_weather_icon; private TextView tv_temperature; private TextView tv_weather; private LinearLayout ll_weatherBG; private TextView tv_timeCurrent; private ScrollView sv_home_page; private boolean hidden = false; private CommonApplication application; //定位相关 private AMapLocationClient mLocationClient = null; //查询天气相关 private boolean searchWeather = true; private WeatherSearchQuery query; private WeatherSearch mweathersearch; private String temperature, weather; // private ViewHolder1 viewHolder1; private ViewHolder2 viewHolder2; private ViewHolder3 viewHolder3; private ViewHolder4 viewHolder4; private ViewHolder5 viewHolder5; //ViewHolder1(今日任务) private Map> taskAllMapList = new HashMap<>();//Map<时间, 所有任务(顶层任务、日任务、子任务)> private ArrayList mSubtaskList = new ArrayList<>(); private ArrayList subtaskListOfUser = new ArrayList<>(); private ArrayList mDayTaskList = new ArrayList<>(); private java.util.Calendar calendarCurrent; private String mCurYearMonth; private String mToday; private boolean requestAgain; private Retrofit mRetrofit; private boolean firstLoad = true; private ViewHolder1Adapter viewHolder1Adapter; private final int VISIBLENUM = 2;//今日任务显示最少的任务数 private Boolean visibility = false; private Subtask subtask; private InspectionVo inspectionVo; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_home, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); searchWeather = true; initData(); initUI(view); initCurLocation(); initCurrentTime(); initScrollView(savedInstanceState); } @Override public void onDestroy() { super.onDestroy(); mLocationClient.onDestroy(); } @Override public void onPause() { super.onPause(); mLocationClient.stopLocation(); searchWeather = true; } @Override public void onResume() { super.onResume(); if (!hidden){ mLocationClient.startLocation(); initCurrentTime(); } // refreshView(); } public HomeFragment() { // Required empty public constructor } private void initData(){ application = (CommonApplication) getActivity().getApplication(); mRetrofit = application.getRetrofit(); Date now = Calendar.getInstance().getTime(); mCurYearMonth = DateFormatter.YearMonthFormat.format(now); mToday = DateFormatter.dateFormat2.format(now); } private void initUI(View view) { tv_location = (TextView) view.findViewById(R.id.tv_location); tv_location.setOnClickListener(this); iv_weather_icon = (ImageView) view.findViewById(R.id.iv_weather_icon); // iv_weather_icon.setOnClickListener(this); // tv_temperature = (TextView) view.findViewById(R.id.tv_temperature); tv_weather = (TextView) view.findViewById(R.id.tv_weather); ll_weatherBG = (LinearLayout) view.findViewById(R.id.ll_weather_bg); tv_timeCurrent = (TextView) view.findViewById(R.id.tv_now_date_time); sv_home_page = (ScrollView) view.findViewById(R.id.sv_home_page); } //初始化时间textView private void initCurrentTime() { Calendar calendar = Calendar.getInstance(); tv_timeCurrent.setText(DateFormatter.dateFormatWeek.format(calendar.getTime())); } private void initScrollView(Bundle savedInstanceState){ // sv_home_page.setLayoutManager(new LinearLayoutManager(getContext())); // parentAdapter = new RecyclerViewAdapter(getContext(),savedInstanceState); // sv_home_page.setAdapter(parentAdapter); viewHolder1 = new ViewHolder1(savedInstanceState, sv_home_page); viewHolder2 = new ViewHolder2(savedInstanceState, sv_home_page); viewHolder3 = new ViewHolder3(savedInstanceState, sv_home_page); viewHolder4 = new ViewHolder4(savedInstanceState, sv_home_page); viewHolder5 = new ViewHolder5(savedInstanceState, sv_home_page); viewHolder1.initViewHolder1(); } private void initCurLocation() { final String curLocation = null; //声明定位回调监听器 AMapLocationListener mLocationListener = new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation aMapLocation) { if (aMapLocation != null) { if (aMapLocation.getErrorCode() == 0) { //可在其中解析amapLocation获取相应内容 //城市地址 String address = aMapLocation.getAddress(); tv_location.setText(address); searchWeather(aMapLocation.getCity()); } else { // Toast.makeText(getActivity(), "定位失败,请检查网络", Toast.LENGTH_SHORT).show(); } } } }; //初始化定位 mLocationClient = new AMapLocationClient(getActivity().getApplicationContext()); //设置定位回调监听 mLocationClient.setLocationListener(mLocationListener); //声明AMapLocationClientOption对象 AMapLocationClientOption mLocationOption = null; //初始化AMapLocationClientOption对象 mLocationOption = new AMapLocationClientOption(); //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位模式为AMapLocationMode.Battery_Saving,低功耗模式。 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving); //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。 mLocationOption.setInterval(10000); //设置是否返回地址信息(默认返回地址信息) mLocationOption.setNeedAddress(true); //设置是否强制刷新WIFI,默认为true,强制刷新。 mLocationOption.setWifiActiveScan(true); //设置是否允许模拟位置,默认为true,允许模拟位置 mLocationOption.setMockEnable(true); //关闭缓存机制 mLocationOption.setLocationCacheEnable(false); //给定位客户端对象设置定位参数 mLocationClient.setLocationOption(mLocationOption); //启动定位 mLocationClient.startLocation(); } //根据城市搜索天气 private void searchWeather(String city) { query = new WeatherSearchQuery(city, WeatherSearchQuery.WEATHER_TYPE_LIVE); mweathersearch = new WeatherSearch(getContext()); mweathersearch.setOnWeatherSearchListener(this); mweathersearch.setQuery(query); mweathersearch.searchWeatherAsyn(); //异步搜索 } @Override public void onHiddenChanged(boolean hidden) { super.onHiddenChanged(hidden); this.hidden = hidden; if (hidden) { mLocationClient.stopLocation(); searchWeather = true; } else { mLocationClient.startLocation(); initCurrentTime(); // viewHolder2.clearView(); viewHolder2.setSubtask(null); refreshView(); } } @Override public void onWeatherLiveSearched(LocalWeatherLiveResult localWeatherLiveResult, int i) { if (searchWeather) { if (i == 1000) { if (localWeatherLiveResult != null || localWeatherLiveResult.getLiveResult() != null) { LocalWeatherLive weatherlive = localWeatherLiveResult.getLiveResult(); temperature = weatherlive.getTemperature() + "°"; weather = weatherlive.getWeather().trim(); tv_weather.setText(weather); tv_temperature.setText(temperature); if (weather != null && !weather.trim().equals("")) { if (weather.equals("多云")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_1); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_cloudy); } else if (weather.equals("晴")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_0); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_sunny); } else if (weather.equals("阴")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_2); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_cloudy); } else if (weather.equals("小雨")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_3); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); } else if (weather.equals("中雨")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_4); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); } else if (weather.equals("大雨")) { iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_5); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); } else if(weather.equals("阵雨")){ iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_8); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); } else if (weather.equals("雨夹雪")){ iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_15); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); }else if (weather.equals("小雪")){ iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_9); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); }else if (weather.equals("中雪")){ iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_10); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); }else if (weather.equals("大雪")){ iv_weather_icon.setBackgroundResource(R.drawable.weather_icon_11); ll_weatherBG.setBackgroundResource(R.drawable.home_feeds_weather_bkg_rain); } } searchWeather = false; } } } } @Override public void onWeatherForecastSearched(LocalWeatherForecastResult localWeatherForecastResult, int i) { } @Override public void onClick(View view) { switch (view.getId()){ case R.id.iv_weather_icon: Toast.makeText(getContext(),"打开天气详情", Toast.LENGTH_SHORT).show(); break; } } //region recyclerview的每一项item的枚举类 public enum ITEM_TYPE { ITEM1, ITEM2, ITEM3, ITEM4, ITEM5 } //endregion public void refreshView(){ viewHolder1.refreshView(); // viewHolder2.refreshView();//需要viewHolder1获取的subtaskList作为前置数据 // viewHolder3.refreshView(); // viewHolder4.refreshView(); // viewHolder5.refreshView(); } /** * 返回当前用户执行的子任务列表 * @param msubtaskList * @return */ public ArrayList getmSubtaskListOfUser(List msubtaskList){ subtaskListOfUser.clear(); // Pattern p = Pattern.compile(application.getCurrentUser().getAdGuid()); for (Subtask subtask : msubtaskList){ // Matcher m = p.matcher(subtask.getExecutorguids()); // if (m.matches()){ // subtaskListOfUser.add(subtask); // } if(subtask.getExecutorguids().contains(application.getCurrentUser().getGuid())) subtaskListOfUser.add(subtask); } return subtaskListOfUser; } // private class RecyclerViewAdapter extends RecyclerView.Adapter { // private Context context; // private LayoutInflater mLayoutInflater; // private Bundle savedInstanceState; // private List holderList = new ArrayList<>(); // // // public RecyclerViewAdapter(Context context, Bundle savedInstanceState) { // this.context = context; // mLayoutInflater = LayoutInflater.from(context); // this.savedInstanceState = savedInstanceState; // } // // public void addData(int position) { // notifyItemInserted(position); // } // // public void removeData(int position) { // notifyItemRemoved(position); // } // // /** // * 创建 ViewHolder 对象 // * // * @param parent // * @param viewType // * @return // */ // @Override // public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // if (viewType == ITEM_TYPE.ITEM1.ordinal()) { // return new ViewHolder1(mLayoutInflater.inflate(R.layout.function_card1, parent, false)); // } else if (viewType == ITEM_TYPE.ITEM2.ordinal()) { // return new ViewHolder2(mLayoutInflater.inflate(R.layout.function_card2, parent, false)); // } else if (viewType == ITEM_TYPE.ITEM3.ordinal()) { // return new ViewHolder3(mLayoutInflater.inflate(R.layout.function_card5, parent, false)); // } else if (viewType == ITEM_TYPE.ITEM4.ordinal()) { // return new ViewHolder4(mLayoutInflater.inflate(R.layout.function_card3, parent, false)); // } else { // return new ViewHolder4(mLayoutInflater.inflate(R.layout.function_card4, parent, false)); // } // // } // // //设置ITEM类型,可以自由发挥,这里设置item position单数显示item1 偶数显示item2 // @Override // public int getItemViewType(int position) { // if (position == 0) { // return ITEM_TYPE.ITEM1.ordinal(); // } else if (position == 1) { // return ITEM_TYPE.ITEM2.ordinal(); // } else if (position == 2) { // return ITEM_TYPE.ITEM3.ordinal(); // } else if (position == 3) { // return ITEM_TYPE.ITEM4.ordinal(); // } else if (position == 4) { // return ITEM_TYPE.ITEM5.ordinal(); // } else { // return 0; // } // } // // @Override // public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { // if (holder instanceof ViewHolder1) { // ((ViewHolder1) holder).initView(); // holderList.add(holder); // } else if (holder instanceof ViewHolder2) { // ((ViewHolder2) holder).initView(); // holderList.add(holder); // } else if (holder instanceof ViewHolder3) { // ((ViewHolder3) holder).initView(); // holderList.add(holder); // } else if (holder instanceof ViewHolder4) { // ((ViewHolder4) holder).initView(); // holderList.add(holder); // } else { // ((ViewHolder5) holder).initView(); // holderList.add(holder); // } // // } // // @Override // public int getItemCount() { // return 5; // } // // public RecyclerView.ViewHolder getViewHolder(String holder, int position){ // if (Objects.equals(holder, VIEWHOLDER1)) { // ViewHolder1 tmpholder = (ViewHolder1)holderList.get(position); // return tmpholder; // } else if (Objects.equals(holder, VIEWHOLDER2)) { // ViewHolder2 tmpholder = (ViewHolder2)holderList.get(position); // return tmpholder; // } else if (Objects.equals(holder, VIEWHOLDER3)) { // ViewHolder3 tmpholder = (ViewHolder3)holderList.get(position); // return tmpholder; // } else if (Objects.equals(holder, VIEWHOLDER4)) { // ViewHolder4 tmpholder = (ViewHolder4)holderList.get(position); // return tmpholder; // } else if (Objects.equals(holder, VIEWHOLDER5)){ // ViewHolder5 tmpholder = (ViewHolder5)holderList.get(position); // return tmpholder; // } // // return null; // } // // } public class ViewHolder1 { RecyclerView rv_viewholder1; TextView tv_look_more; TextView tv_no_task; // List subtaskListOfUser = new ArrayList<>(); ViewHolder1(Bundle savedInstanceState, View itemView) { rv_viewholder1 = (RecyclerView) itemView.findViewById(R.id.rv_today_task); tv_look_more = (TextView) itemView.findViewById(R.id.look_more); tv_no_task = (TextView) itemView.findViewById(R.id.tv_no_task); tv_look_more.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { viewHolder1Adapter.setVisiblity(!visibility); visibility = !visibility; if (subtaskListOfUser.size() > VISIBLENUM){ if (visibility) tv_look_more.setText("点击收回"); else tv_look_more.setText("还有" + (subtaskListOfUser.size() - VISIBLENUM) + "个任务,点击展开"); } } }); } void initViewHolder1(){ calendarCurrent = Calendar.getInstance(); mCurYearMonth = DateFormatter.YearMonthFormat.format(calendarCurrent.getTime()); getThreeMonthTask(mCurYearMonth, Constant.TASK_GETINFOTYPE_MIDDLE); // subtaskListOfUser = getmSubtaskListOfUser(mSubtaskList); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext()); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); rv_viewholder1.setLayoutManager(linearLayoutManager); viewHolder1Adapter = new ViewHolder1Adapter(getContext(),subtaskListOfUser); if (subtaskListOfUser.isEmpty())tv_no_task.setVisibility(View.VISIBLE); else tv_no_task.setVisibility(View.GONE); // if (mSubtaskList.size() > VISIBLENUM) tv_look_more.setTitle("还有" + (mSubtaskList.size() - VISIBLENUM) + "个任务,点击展开"); rv_viewholder1.setAdapter(viewHolder1Adapter); } void refreshView() { getThreeMonthTask(mCurYearMonth, Constant.TASK_GETINFOTYPE_MIDDLE); } /** * 获取当前月及前后月的任务信息 * * @param yearMonth 年月:例:2018-01 * @param type Middle、Left、Right */ public void getThreeMonthTask(final String yearMonth, final String type) { Call> loadThreeMonthTasks = mRetrofit.create(TaskService.class).loadThreeMonthTasks(yearMonth, "0", type); loadThreeMonthTasks.enqueue(new Callback>() { @Override public void onResponse(@NonNull Call> call, @NonNull Response> response) { if (response.body() != null) { requestAgain = false; List taskPackList = response.body(); if (taskPackList != null && taskPackList.size() > 0) { for (TaskPack taskPack : taskPackList) { taskAllMapList.put(taskPack.getDate(), taskPack.getUpperTaskList()); } if (firstLoad) { firstLoad = false; // showMonthAllTask(calendarCurrent); } initSubtask(); if (mSubtaskList.size() > 0) { // Toast.makeText(getContext(),"加载完成", Toast.LENGTH_SHORT).show(); // parentAdapter.notifyDataSetChanged(); subtaskListOfUser = getmSubtaskListOfUser(mSubtaskList); if (!subtaskListOfUser.isEmpty()) { viewHolder1Adapter.notifyDataSetChanged(); if (subtaskListOfUser.size() > VISIBLENUM) tv_look_more.setText("还有" + (subtaskListOfUser.size() - VISIBLENUM) + "个任务,点击展开"); tv_no_task.setVisibility(View.GONE); } } viewHolder2.refreshView(); } } else if (response.errorBody() != null) { if (!requestAgain) { requestAgain = true; getThreeMonthTask(yearMonth, type); } else { Toast.makeText(application, "获取任务信息出错", Toast.LENGTH_SHORT).show(); // showMonthAllTask(calendarCurrent); } System.out.println("ThreeMonthTask:" + response.errorBody()); } } @Override public void onFailure(@NonNull Call> call, @NonNull Throwable t) { if (!requestAgain) { requestAgain = true; getThreeMonthTask(yearMonth, type); } else { Toast.makeText(application, "网络链接失败", Toast.LENGTH_SHORT).show(); // showMonthAllTask(calendarCurrent); } System.out.println("ThreeMonthTask:" + t.toString()); } }); } private void initSubtask(){ mDayTaskList.clear(); mSubtaskList.clear(); // int daytaskTotalNumThisWeek = 0; // int daytaskFinishedNumThisWeek = 0; // int daytaskTotalNumThisMonth = 0; // int daytaskFinishedNumThisMonth = 0; // int subtaskTotalNumToday = 0; // int subtaskFinishedNumToday = 0; for (TaskVo taskVo : taskAllMapList.get(mCurYearMonth)) { for (TaskVo dayTaskVo : taskVo.getDaytaskList()) { // daytaskTotalNumThisMonth++; mDayTaskList.add(dayTaskVo); // long taskTime = dayTaskVo.getStarttime().getTime(); // long monday = CommonUtils.getTimesWeekmorning().getTime(); // long sunday = CommonUtils.getTimesWeeknight().getTime(); // if (taskTime >= monday && taskTime < sunday) { // daytaskTotalNumThisWeek++; // if (dayTaskVo.getRuningstatus().equals(Domain.TASK_STATUS_FINISHED)) { // daytaskFinishedNumThisWeek++; // } // } // if (dayTaskVo.getRuningstatus().equals(Domain.TASK_STATUS_FINISHED)) { // daytaskFinishedNumThisMonth++; // } List subTaskList = dayTaskVo.getSubtaskList(); for (Subtask subtask : subTaskList) { String subtaskStartTime = DateFormatter.dateFormat2.format(subtask.getPlanstarttime()); if (subtaskStartTime.equals(mToday)) { // subtaskTotalNumToday++; // if (subtask.getStatus().equals(Domain.TASK_STATUS_FINISHED)) { // subtaskFinishedNumToday++; // } mSubtaskList.add(subtask); } } } } } } public class ViewHolder2 { private int thisproblemCount = 0; private List lastproblemlist; private LinearLayout ll_inspection; TextView tv_info, tv_address; TextView tv_recheck, tv_problem_num, tv_changed_num, tv_promissed_num; RelativeLayout rl_executing_task; ViewHolder2(Bundle savedInstanceState, View itemView) { tv_info = (TextView) itemView.findViewById(R.id.tv_info); tv_address = (TextView) itemView.findViewById(R.id.tv_address); tv_recheck = (TextView) itemView.findViewById(R.id.tv_recheck); tv_problem_num = (TextView) itemView.findViewById(R.id.tv_problem_num); tv_changed_num = (TextView) itemView.findViewById(R.id.tv_changed_num); tv_promissed_num = (TextView) itemView.findViewById(R.id.tv_promissed_num); rl_executing_task = (RelativeLayout) itemView.findViewById(R.id.rl_executing_task); rl_executing_task.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (subtask != null) { Intent intent = new Intent("cn.flightfeather.thirdapp.broadcasereceiver.MainReceiver"); try { intent.putExtra("subtask", subtask); // Log.e("数据!!", intent.getStringExtra("subtask")); intent.setAction(Constant.GOTOSUBTASK); // getActivity().switchSelectedFragment(ll_inspection); getActivity().sendBroadcast(intent); } catch (NullPointerException e) { e.printStackTrace(); } } } }); itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { return true; } }); } void refreshView(){ initViewholder2(); } void initView() { if (subtask == null) return; tv_info.setText(subtask.getName()); tv_address.setText(subtask.getScenseaddress()); try { if (inspectionVo.getIsrechecked()) { tv_recheck.setText(Constant.ISRECHECKED); } else { tv_recheck.setText(Constant.UNRECHECKED); } }catch (NullPointerException e){ e.printStackTrace(); } tv_problem_num.setText(inspectionVo.getProblemcount() == null ? Constant.DEFAULTTEXTZERO : String.valueOf(inspectionVo.getProblemcount())); tv_changed_num.setText(inspectionVo.getChangednum() == null ? Constant.DEFAULTTEXTZERO : String.valueOf(inspectionVo.getChangednum())); tv_promissed_num.setText(inspectionVo.getPromissednum() == null ? Constant.DEFAULTTEXTZERO : String.valueOf(inspectionVo.getPromissednum())); } void clearView(){ tv_info.setText(Constant.DEFAULTTEXTNULL); tv_address.setText(Constant.DEFAULTTEXTNULL); tv_recheck.setText(Constant.NORECHECKED); tv_problem_num.setText(Constant.DEFAULTTEXTDASHES); tv_changed_num.setText(Constant.DEFAULTTEXTDASHES); tv_promissed_num.setText(Constant.DEFAULTTEXTDASHES); } public void setSubtask(Subtask subtask1) { subtask = subtask1; } private void initViewholder2(){ subtask = null; for (Subtask s : subtaskListOfUser) { if (Objects.equals(s.getStatus(), Domain.TASK_STATUS_RUNNING)) { subtask = s; break; } } if (subtask != null) { Call loadInspectionData = mRetrofit.create(InspectionService.class).loadInspectionData(subtask.getStguid()); loadInspectionData.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.body() != null) { inspectionVo = response.body(); initView(); viewHolder3.refreshView(); } else if (response.body() == null){ clearView(); } else if (response.errorBody() != null) { Toast.makeText(application, "获取执行任务信息出错", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(Call call, Throwable t) { Toast.makeText(application, "网络链接失败", Toast.LENGTH_SHORT).show(); } }); }else { clearView(); } } } public class ViewHolder3 { private Subtask lastsubtask; private InspectionVo lastinspectionVo; private Evaluation lastevaluation; TextView tv_history_name;//场景 TextView tv_history_recheck, tv_history_problem_num, tv_history_changed_num, tv_history_promissed_num, tv_git_num;//复核、问题、整改、承诺、技防 TextView tv_result_type, tv_result_score, tv_score_unit, tv_result_standard;//考核类别、评分/不规范问题数(评估结果(定量))、评分单位(分/条)、评估结果(定性) RelativeLayout rl_history_info; ViewHolder3(Bundle savedInstanceState, View itemView) { tv_history_name = (TextView) itemView.findViewById(R.id.tv_history_name); tv_history_recheck = (TextView) itemView.findViewById(R.id.tv_history_recheck); tv_history_problem_num = (TextView) itemView.findViewById(R.id.tv_history_problem_num); tv_history_changed_num = (TextView) itemView.findViewById(R.id.tv_history_changed_num); tv_history_promissed_num = (TextView) itemView.findViewById(R.id.tv_history_promissed_num); tv_git_num = (TextView) itemView.findViewById(R.id.tv_git_num); tv_result_type = (TextView) itemView.findViewById(R.id.tv_result_type); tv_result_score = (TextView) itemView.findViewById(R.id.tv_result_score); tv_score_unit = (TextView) itemView.findViewById(R.id.tv_score_unit); tv_result_standard = (TextView) itemView.findViewById(R.id.tv_result_standard); rl_history_info = (RelativeLayout) itemView.findViewById(R.id.rl_history_info); // rl_history_info.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (lastsubtask != null) { Intent intent = new Intent("cn.flightfeather.thirdapp.broadcasereceiver.MainReceiver"); try { intent.putExtra("subtask", lastsubtask); // Log.e("数据!!", intent.getStringExtra("subtask")); intent.setAction(Constant.GOTOSUBTASK); // getActivity().switchSelectedFragment(ll_inspection); getActivity().sendBroadcast(intent); } catch (NullPointerException e) { e.printStackTrace(); } } } }); itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { return true; } }); } void refreshView() { initViewHolder3(); } void initView(){ tv_history_name.setText(lastsubtask.getName()); try { if (inspectionVo.getIsrechecked()) { tv_history_recheck.setText(Constant.ISRECHECKED); } else { tv_history_recheck.setText(Constant.UNRECHECKED); } }catch (NullPointerException e){ e.printStackTrace(); } tv_history_problem_num.setText(lastinspectionVo.getProblemcount() == null ? Constant.DEFAULTTEXTZERO : String.valueOf(lastinspectionVo.getProblemcount())); tv_history_changed_num.setText(lastinspectionVo.getChangednum() == null ? Constant.DEFAULTTEXTZERO : String.valueOf(lastinspectionVo.getChangednum())); tv_history_promissed_num.setText(lastinspectionVo.getPromissednum() == null ? Constant.DEFAULTTEXTZERO : String.valueOf(lastinspectionVo.getPromissednum())); tv_git_num.setText(lastinspectionVo.getGitList() == null ? Constant.DEFAULTTEXTZERO : String.valueOf(lastinspectionVo.getGitList().size())); //考核类型 try { if (Objects.equals(lastevaluation.getErtype(), Domain.STANDARD)) { tv_result_type.setText(Constant.STANDARD); tv_score_unit.setText(Constant.UNITOFSTANDARD); } else { tv_result_type.setText(Constant.SCORE); tv_score_unit.setText(Constant.UNITOFSCORE); } }catch (NullPointerException e){ e.printStackTrace(); } //考核结果(整改前或整改后) if (lastevaluation != null) { tv_result_score.setText(lastevaluation.getResultscoreaft() == null ? lastevaluation.getResultscorebef() : lastevaluation.getResultscoreaft()); tv_result_standard.setText(lastevaluation.getResultstandardaft() == null ? lastevaluation.getResultstandardbef() : lastevaluation.getResultstandardaft()); } } void clearView() { tv_history_name.setText(Constant.DEFAULTTEXTNULL); tv_history_recheck.setText(Constant.NORECHECKED); tv_history_problem_num.setText(Constant.DEFAULTTEXTDASHES); tv_history_changed_num.setText(Constant.DEFAULTTEXTDASHES); tv_history_promissed_num.setText(Constant.DEFAULTTEXTDASHES); tv_git_num.setText(Constant.DEFAULTTEXTDASHES); tv_result_type.setText(Constant.DEFAULTTEXTDASHES); tv_result_score.setText(Constant.DEFAULTTEXTDASHES); // tv_score_unit. tv_result_standard.setText(Constant.DEFAULTTEXTDASHES); } private void initViewHolder3(){ try { Call loadLastProblemList = mRetrofit.create(InspectionService.class).loadLastProblemList(inspectionVo.getSguid(), DateFormatter.dateTimeFormat2.format(inspectionVo.getExecutionstarttime())); loadLastProblemList.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.body() != null) { // lastproblemlist = response.body().getProblemlistVo(); lastsubtask = response.body().getSubtaskVo(); if (lastsubtask == null) { Toast.makeText(getActivity(), "未查到上次巡查信息。", Toast.LENGTH_SHORT).show(); return; } Call loadInspectionData = mRetrofit.create(InspectionService.class).loadInspectionData(lastsubtask.getStguid()); loadInspectionData.enqueue(new Callback() { @Override public void onResponse(Call call, Response response) { if (response.body() != null) { lastinspectionVo = response.body(); Call> getEvaluations = mRetrofit.create(EvaluationService.class).takeAllEvaluations(); getEvaluations.enqueue(new Callback>() { @Override public void onResponse(Call> call, Response> response) { if (response.body() != null){ for (Evaluation tmp : response.body()){ if (Objects.equals(tmp.getIguid(), lastinspectionVo.getGuid())) { lastevaluation = tmp; break; } } initView(); } } @Override public void onFailure(Call> call, Throwable t) { } }); } else if (response.body() == null){ clearView(); } } @Override public void onFailure(Call call, Throwable t) { } }); } else if (response.errorBody() != null) { Toast.makeText(application, "获取上次任务信息出错", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(Call call, Throwable t) { Toast.makeText(application, "网络链接失败", Toast.LENGTH_SHORT).show(); } }); }catch (NullPointerException e){ e.printStackTrace(); clearView(); } } } public class ViewHolder4 { // ImageView task1,task2; // TextView task1date, task1name,task2date, task2name; ViewHolder4(Bundle savedInstanceState, View itemView) { // task2 = (ImageView) itemView.findViewById(R.id.iv_tody_task2); // task1date = (TextView) itemView.findViewById(R.id.tv_tody_task1date); // task1name = (TextView) itemView.findViewById(R.id.tv_tody_task1name); // task2date = (TextView) itemView.findViewById(R.id.tv_tody_task2date); // task2name = (TextView) itemView.findViewById(R.id.tv_tody_task2name); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // mRecyclerViewAdapter.addData(0); } }); itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { // mRecyclerViewAdapter.addData(0); return true; } }); } void refreshView() { // task1.setBackgroundResource(R.drawable.icon_tody_task_1); // task2.setBackgroundResource(R.drawable.icon_tody_task_2); // task1date.setTitle(new Date().toString()); // task2date.setTitle(new Date().toString()); // task1name.setTitle("任务名称任务名称任务名称"); // task2name.setTitle("任务名称任务名称任务名称"); } } public class ViewHolder5 { // ImageView task1,task2; // TextView task1date, task1name,task2date, task2name; ViewHolder5(Bundle savedInstanceState, View itemView) { // task2 = (ImageView) itemView.findViewById(R.id.iv_tody_task2); // task1date = (TextView) itemView.findViewById(R.id.tv_tody_task1date); // task1name = (TextView) itemView.findViewById(R.id.tv_tody_task1name); // task2date = (TextView) itemView.findViewById(R.id.tv_tody_task2date); // task2name = (TextView) itemView.findViewById(R.id.tv_tody_task2name); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // mRecyclerViewAdapter.addData(0); } }); itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View view) { // mRecyclerViewAdapter.addData(0); return true; } }); } void refreshView() { // task1.setBackgroundResource(R.drawable.icon_tody_task_1); // task2.setBackgroundResource(R.drawable.icon_tody_task_2); // task1date.setTitle(new Date().toString()); // task2date.setTitle(new Date().toString()); // task1name.setTitle("任务名称任务名称任务名称"); // task2name.setTitle("任务名称任务名称任务名称"); } } private class ViewHolder1Adapter extends RecyclerView.Adapter{ private Context context; private LayoutInflater mLayoutInflater; private Bundle savedInstanceState; private List mData; private Boolean visibility = false; public ViewHolder1Adapter(Context context, List mData) { this.context = context; mLayoutInflater = LayoutInflater.from(context); this.savedInstanceState = savedInstanceState; this.mData = mData; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemview = mLayoutInflater.inflate(R.layout.item_today_task,parent,false); // DisplayMetrics dm = new DisplayMetrics(); // RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((dm.widthPixels - GradeActivity.dip2px(getContext(), 20)) / 3, (dm.widthPixels - GradeActivity.dip2px(getContext(), 20)) / 3); // itemview.setLayoutParams(lp); return new TaskViewHolder(itemview); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if (holder instanceof TaskViewHolder) { ((TaskViewHolder) holder).task.setText(String.valueOf(position + 1)); ((TaskViewHolder) holder).taskname.setText(mData.get(position).getName()); String date = DateFormatter.dateFormat.format(mData.get(position).getPlanstarttime()); ((TaskViewHolder) holder).taskdate.setText(date); ((TaskViewHolder) holder).district.setText(mData.get(position).getDistrictname()); if (position + 1 > VISIBLENUM) ((TaskViewHolder) holder).setVisibility(visibility); else ((TaskViewHolder)holder).setVisibility(true); } } @Override public int getItemCount() { return mData.size(); } //设置itemView隐藏 public void setVisiblity(Boolean visiblity){ this.visibility = visiblity; notifyDataSetChanged(); } class TaskViewHolder extends RecyclerView.ViewHolder{ TextView task, taskdate, taskname, district; public TaskViewHolder(View itemView) { super(itemView); task = (TextView) itemView.findViewById(R.id.tv_tody_task); taskdate = (TextView) itemView.findViewById(R.id.tv_tody_task_date); taskname = (TextView) itemView.findViewById(R.id.tv_tody_task_name); district = (TextView) itemView.findViewById(R.id.tv_district); } public void setVisibility(boolean isVisible){ RecyclerView.LayoutParams param = (RecyclerView.LayoutParams)itemView.getLayoutParams(); if (isVisible){ param.height = LinearLayout.LayoutParams.WRAP_CONTENT; param.width = LinearLayout.LayoutParams.MATCH_PARENT; itemView.setVisibility(View.VISIBLE); }else{ itemView.setVisibility(View.GONE); param.height = 0; param.width = 0; } itemView.setLayoutParams(param); } } } }