package cn.flightfeather.thirdapp.adapter; import android.app.Activity; import android.app.DatePickerDialog; import android.content.Intent; import android.os.Environment; import android.support.v7.widget.RecyclerView; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.ping.greendao.gen.MediafileDao; import java.io.File; import java.io.Serializable; import java.text.ParseException; 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.activity.PhotoViewerActivity; import cn.flightfeather.thirdapp.activity.PromiseActivity; import cn.flightfeather.thirdapp.activity.SignActivity; import cn.flightfeather.thirdapp.bean.AllBoolean; import cn.flightfeather.thirdapp.bean.Inspection; import cn.flightfeather.thirdapp.bean.Mediafile; import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo; import cn.flightfeather.thirdapp.httpservice.InspectionService; import cn.flightfeather.thirdapp.task.DownloadAndSetImageTask; import cn.flightfeather.thirdapp.task.SetImageTask; import cn.flightfeather.thirdapp.util.DateFormatter; /** * Created by note_ff_1602 on 2018/3/2. */ public class ProblemPromiseListAdapter extends RecyclerView.Adapter{ private List problemlistVoList; private Activity activity; private LayoutInflater layoutInflater; private ProblemlistVo problemlistVoCurrent; private MediafileDao mediafileDao; private InspectionService inspectionService; private Inspection inspectionCurrent; private File signFile; private List signMediafileList; private CommonApplication application; private PromiseActivity promiseActivity; private final int SIGN = 9; private final int VIEW_SIGN_PHOTO = 117; private AllBoolean allBoolean; public ProblemPromiseListAdapter(List problemlistVoList, Activity activity, CommonApplication application, PromiseActivity promiseActivity, List signMediaFileList, AllBoolean allBoolean) { this.problemlistVoList = problemlistVoList; this.activity = activity; layoutInflater = LayoutInflater.from(activity); mediafileDao = application.getDaoSession().getMediafileDao(); this.application =application; this.promiseActivity = promiseActivity; this.signMediafileList = signMediaFileList; this.allBoolean = allBoolean; } @Override public ProblemPromiseListHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = layoutInflater.inflate(R.layout.item_problem_promise,parent,false); return new ProblemPromiseListHolder(view, new CustomTextWatcher(), new SignClickListener(), new TimeClickListener(), new PhotoClickListener1(), new PhotoClickListener2(), new PhotoClickListener3()); } @Override public void onBindViewHolder(ProblemPromiseListHolder holder, int position) { if (position mediafileList = problemlistVoCurrent.getMediafileList(); List mediafileList1 = mediafileDao.queryBuilder().where(MediafileDao.Properties.Businessguid.eq(problemlistVoCurrent.getGuid())).where(MediafileDao.Properties.Remark.eq("未上传")).list(); if (!mediafileList.containsAll(mediafileList1)){ mediafileList.addAll(mediafileList1); } List changedMediaFilelist = new ArrayList<>(); for (Mediafile mediafile:mediafileList){ if (mediafile.getIschanged()){ changedMediaFilelist.add(mediafile); } } mediafileList.removeAll(changedMediaFilelist); if (mediafileList!=null&&mediafileList.size()>0){ if (mediafileList.size() ==1){ holder.iv_photo1.setVisibility(View.VISIBLE); setImage(mediafileList.get(0),holder.iv_photo1); holder.iv_photo2.setVisibility(View.GONE); holder.iv_photo3.setVisibility(View.GONE); }else if (mediafileList.size() ==2){ holder.iv_photo1.setVisibility(View.VISIBLE); holder.iv_photo2.setVisibility(View.VISIBLE); setImage(mediafileList.get(0),holder.iv_photo1); setImage(mediafileList.get(1),holder.iv_photo2); holder.iv_photo3.setVisibility(View.GONE); }else if (mediafileList.size() ==3){ holder.iv_photo1.setVisibility(View.VISIBLE); holder.iv_photo2.setVisibility(View.VISIBLE); holder.iv_photo3.setVisibility(View.VISIBLE); setImage(mediafileList.get(0),holder.iv_photo1); setImage(mediafileList.get(1),holder.iv_photo2); setImage(mediafileList.get(2),holder.iv_photo3); }else if (mediafileList.size() ==0){ holder.iv_photo1.setVisibility(View.GONE); holder.iv_photo2.setVisibility(View.GONE); holder.iv_photo3.setVisibility(View.GONE); } }else { holder.iv_photo1.setVisibility(View.GONE); holder.iv_photo2.setVisibility(View.GONE); holder.iv_photo3.setVisibility(View.GONE); } if (allBoolean.isAll()){ holder.ll_promise2.setVisibility(View.GONE); }else { holder.ll_promise2.setVisibility(View.VISIBLE); } }else { holder.ll_promise.setVisibility(View.GONE); holder.tv_sign.setVisibility(View.VISIBLE); holder.iv_sign.setVisibility(View.VISIBLE); if (signMediafileList!=null&&signMediafileList.size()>0){ setImage(signMediafileList.get(0),holder.iv_sign); }else { holder.iv_sign.setImageResource(R.drawable.icon_add_sign); } } } //设置图片 private void setImage(Mediafile mediafile,ImageView imageView){ File file = new File(Environment.getExternalStorageDirectory(), (mediafile.getPath() + mediafile.getDescription())); if (file.exists()){ SetImageTask task1 = new SetImageTask(file, imageView, activity); task1.execute(); }else { DownloadAndSetImageTask task2 = new DownloadAndSetImageTask(mediafile,imageView, activity); task2.execute(); } } @Override public int getItemCount() { return (problemlistVoList.size()+1); } public class ProblemPromiseListHolder extends RecyclerView.ViewHolder{ LinearLayout ll_promise; ImageView iv_sign; TextView tv_name; TextView tv_location; TextView tv_time; EditText et_promise; TextView tv_count; ImageView iv_photo1; ImageView iv_photo2; ImageView iv_photo3; CustomTextWatcher customTextWatcher; TimeClickListener timeClickListener; PhotoClickListener1 photoClickListener1; PhotoClickListener2 photoClickListener2; PhotoClickListener3 photoClickListener3; LinearLayout ll_promise2; TextView tv_sign; public ProblemPromiseListHolder(View itemView, CustomTextWatcher watcher, SignClickListener signClickListener, TimeClickListener timeClickListener, PhotoClickListener1 photoClickListener1, PhotoClickListener2 photoClickListener2, PhotoClickListener3 photoClickListener3) { super(itemView); ll_promise = (LinearLayout) itemView.findViewById(R.id.ll_promise); iv_sign = (ImageView) itemView.findViewById(R.id.iv_sign); tv_name = (TextView) itemView.findViewById(R.id.tv_problem_name); tv_location = (TextView) itemView.findViewById(R.id.tv_problem_location); tv_time = (TextView) itemView.findViewById(R.id.tv_time); et_promise = (EditText) itemView.findViewById(R.id.et_promise); tv_count = (TextView) itemView.findViewById(R.id.tv_count); iv_photo1 = (ImageView) itemView.findViewById(R.id.iv_photo1); iv_photo2 = (ImageView) itemView.findViewById(R.id.iv_photo2); iv_photo3 = (ImageView) itemView.findViewById(R.id.iv_photo3); ll_promise2 = (LinearLayout) itemView.findViewById(R.id.ll_promise2); tv_sign = (TextView) itemView.findViewById(R.id.tv_sign); iv_sign.setOnClickListener(signClickListener); this.customTextWatcher = watcher; this.timeClickListener = timeClickListener; et_promise.addTextChangedListener(watcher); tv_time.setOnClickListener(timeClickListener); this.photoClickListener1 = photoClickListener1; this.photoClickListener2 = photoClickListener2; this.photoClickListener3 = photoClickListener3; iv_photo1.setOnClickListener(photoClickListener1); iv_photo2.setOnClickListener(photoClickListener2); iv_photo3.setOnClickListener(photoClickListener3); } } public class CustomTextWatcher implements TextWatcher{ private int position; public void updatePosition(int position) { this.position = position; } @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { problemlistVoList.get(position).setExtension2(editable.toString()); } } //签名的点击事件 public class SignClickListener implements View.OnClickListener{ @Override public void onClick(View view) { if (signMediafileList!=null&&signMediafileList.size()>0){ Mediafile mediaFile = signMediafileList.get(0); File file = new File(Environment.getExternalStorageDirectory(), (mediaFile.getPath() + mediaFile.getDescription())); List files = new ArrayList<>(); files.add(file); Intent intent = new Intent(application, PhotoViewerActivity.class); // intent.putExtra("file", file); intent.putExtra(PhotoViewerActivity.PARA_FILES, (Serializable) files); intent.putExtra("type", PhotoViewerActivity.CAMERA_PHOTO); intent.putExtra("deletable", true); // intent.putExtra("mediaFile", mediaFile); intent.putExtra(PhotoViewerActivity.PARA_MEDIAS, (Serializable) signMediafileList); promiseActivity.startActivityForResult(intent, VIEW_SIGN_PHOTO); }else { promiseActivity.startActivityForResult(new Intent(promiseActivity, SignActivity.class), SIGN); } } } //时间的点击事件 public class TimeClickListener implements View.OnClickListener{ private int position; public void updatePosition(int position) { this.position = position; } @Override public void onClick(View view) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); if ( problemlistVoList.get(position).getExtension1()!=null){ try { Date date = DateFormatter.dateFormat.parse(problemlistVoList.get(position).getExtension1()); Calendar calendar1 =Calendar.getInstance(); calendar1.setTime(date); year = calendar1.get(Calendar.YEAR); month = calendar1.get(Calendar.MONTH); day = calendar1.get(Calendar.DAY_OF_MONTH); } catch (ParseException e) { e.printStackTrace(); } } DatePickerDialog dp = new DatePickerDialog(promiseActivity, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) { Calendar calendar2 = Calendar.getInstance(); calendar2.set(year,monthOfYear,dayOfMonth); String date = DateFormatter.dateFormat.format(calendar2.getTime()); problemlistVoList.get(position).setExtension1(date); ProblemPromiseListAdapter.this.notifyDataSetChanged(); } }, year, month, day); dp.show(); } } //图片1的点击事件 public class PhotoClickListener1 implements View.OnClickListener{ private int position; public void updatePosition(int position) { this.position = position; } @Override public void onClick(View view) { Intent intent = new Intent(promiseActivity,PhotoViewerActivity.class); List mediaFileList = problemlistVoList.get(position).getMediafileList(); List files = new ArrayList<>(); for (Mediafile m : mediaFileList) { File file = new File(Environment.getExternalStorageDirectory(), (m.getPath() + m.getDescription())); files.add(file); } intent.putExtra("deletable",false); intent.putExtra(PhotoViewerActivity.PARA_MEDIAS, (Serializable) mediaFileList); // intent.putExtra("mediaFile",mediaFile); intent.putExtra(PhotoViewerActivity.PARA_FILES, (Serializable) files); // intent.putExtra("file",file); intent.putExtra("type",PhotoViewerActivity.PROBLEM_PHOTO); promiseActivity.startActivity(intent); } } //图片2的点击事件 public class PhotoClickListener2 implements View.OnClickListener{ private int position; public void updatePosition(int position) { this.position = position; } @Override public void onClick(View view) { Intent intent = new Intent(promiseActivity,PhotoViewerActivity.class); List mediaFileList = problemlistVoList.get(position).getMediafileList(); List files = new ArrayList<>(); for (Mediafile m : mediaFileList) { File file = new File(Environment.getExternalStorageDirectory(), (m.getPath() + m.getDescription())); files.add(file); } intent.putExtra("deletable",false); intent.putExtra(PhotoViewerActivity.PARA_MEDIAS, (Serializable) mediaFileList); // intent.putExtra("mediaFile",mediaFile); intent.putExtra(PhotoViewerActivity.PARA_FILES, (Serializable) files); // intent.putExtra("file",file); intent.putExtra("type",PhotoViewerActivity.PROBLEM_PHOTO); promiseActivity.startActivity(intent); } } //图片3的点击事件 public class PhotoClickListener3 implements View.OnClickListener{ private int position; public void updatePosition(int position) { this.position = position; } @Override public void onClick(View view) { Intent intent = new Intent(promiseActivity,PhotoViewerActivity.class); List mediaFileList = problemlistVoList.get(position).getMediafileList(); List files = new ArrayList<>(); for (Mediafile m : mediaFileList) { File file = new File(Environment.getExternalStorageDirectory(), (m.getPath() + m.getDescription())); files.add(file); } intent.putExtra("deletable",false); intent.putExtra(PhotoViewerActivity.PARA_MEDIAS, (Serializable) mediaFileList); // intent.putExtra("mediaFile",mediaFile); intent.putExtra(PhotoViewerActivity.PARA_FILES, (Serializable) files); // intent.putExtra("file",file); intent.putExtra("type",PhotoViewerActivity.PROBLEM_PHOTO); promiseActivity.startActivity(intent); } } }