Recyclerview中有多條Editext,並有新增,刪除item
阿新 • • 發佈:2018-12-13
先說一下需求:需要新增多輛車的資訊,車輛數無上限,預設顯示一個空的車輛資訊,可進行增,刪操作,刪除時又可全部刪除!
先上效果圖
CompileCarActivity.class 的xml佈局
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="wrap_content" /> <--!新增按鈕始終固定在底部--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/bg_f4f4f4" android:layout_alignParentBottom="true" android:layout_marginBottom="@dimen/lay_55" > <ImageView android:id="@+id/iv_add_newcar" android:layout_width="match_parent" android:layout_height="@dimen/lay_42" android:src="@mipmap/add_car" android:layout_marginTop="@dimen/lay_8" /> </LinearLayout> </RelativeLayout>
avtivity_car_compile_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@color/bg_f4f4f4" > <RelativeLayout android:id="@+id/rv_title" android:background="@color/white" android:layout_width="match_parent" android:layout_height="@dimen/lay_45"> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingStart="@dimen/lay_15" android:text="車輛1" android:drawablePadding="@dimen/lay_5" android:drawableRight="@mipmap/car_info_delete" android:gravity="center_vertical" android:textSize="@dimen/font_14" android:textColor="@color/text_333333" /> <TextView android:id="@+id/tv_hebing" android:layout_toRightOf="@id/tv_title" android:layout_width="match_parent" android:drawableRight="@mipmap/expand" android:drawablePadding="@dimen/lay_5" android:layout_height="match_parent" android:gravity="center_vertical|right" android:layout_alignParentRight="true" android:paddingEnd="@dimen/lay_15" android:textColor="@color/name_ff9832" android:text="收起" /> </RelativeLayout> <LinearLayout android:id="@+id/ll_hind_show" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="visible" > <RelativeLayout android:id="@+id/rv_car_type" android:background="@color/white" android:layout_width="match_parent" android:layout_height="@dimen/lay_45"> <TextView android:id="@+id/tv_vocher" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingStart="@dimen/lay_15" android:text="車輛品牌" android:gravity="center_vertical" android:textSize="@dimen/font_14" android:textColor="@color/text_999999" /> <TextView android:id="@+id/tv_cartype" android:layout_toRightOf="@id/tv_vocher" android:layout_width="match_parent" android:drawableRight="@mipmap/arrow_right" android:drawablePadding="@dimen/lay_5" android:layout_height="match_parent" android:gravity="center_vertical|right" android:layout_alignParentRight="true" android:paddingEnd="@dimen/lay_15" android:textColor="@color/text_333333" android:textColorHint="@color/text_hint" android:textSize="@dimen/font_14" android:hint="請選擇車輛品牌" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="@dimen/lay_0.5" android:layout_marginLeft="@dimen/lay_15" android:layout_marginRight="@dimen/lay_15" android:background="@color/line" /> <RelativeLayout android:id="@+id/rv_car_model" android:background="@color/white" android:layout_width="match_parent" android:layout_height="@dimen/lay_45"> <TextView android:id="@+id/tv_vocher_number" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingStart="@dimen/lay_15" android:text="品牌型號" android:background="@color/white" android:gravity="center_vertical" android:textSize="@dimen/font_14" android:textColor="@color/text_999999" /> <com.broker.liming.widget.ClearEditText2 android:id="@+id/cet_model" android:layout_toRightOf="@id/tv_vocher_number" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingEnd="@dimen/lay_15" android:paddingLeft="@dimen/lay_15" android:background="@color/white" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:textCursorDrawable="@null" android:singleLine="true" android:gravity="center_vertical|right" android:textColor="@color/text_333333" android:textColorHint="@color/text_hint" android:textSize="@dimen/font_14" android:hint="請輸入品牌型號" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="@dimen/lay_0.5" android:layout_marginRight="@dimen/lay_15" android:layout_marginLeft="@dimen/lay_15" android:background="@color/line" /> <RelativeLayout android:id="@+id/rv_car_number" android:background="@color/white" android:layout_width="match_parent" android:layout_height="@dimen/lay_45"> <TextView android:id="@+id/vocher_values" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingStart="@dimen/lay_15" android:text="車牌號" android:background="@color/white" android:gravity="center_vertical" android:textSize="@dimen/font_14" android:textColor="@color/text_999999" /> <com.broker.liming.widget.ClearEditText2 android:id="@+id/cet_car_number" android:layout_toRightOf="@id/vocher_values" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingEnd="@dimen/lay_15" android:paddingLeft="@dimen/lay_15" android:background="@color/white" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:textCursorDrawable="@null" android:singleLine="true" android:gravity="center_vertical|right" android:textColor="@color/text_333333" android:textColorHint="@color/text_hint" android:textSize="@dimen/font_14" android:hint="請輸入車牌號" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="@dimen/lay_0.5" android:layout_marginRight="@dimen/lay_15" android:layout_marginLeft="@dimen/lay_15" android:background="@color/line" /> <RelativeLayout android:background="@color/white" android:layout_width="match_parent" android:layout_height="@dimen/lay_45"> <TextView android:id="@+id/car_logo" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingStart="@dimen/lay_15" android:text="車險品牌" android:gravity="center_vertical" android:textSize="@dimen/font_14" android:textColor="@color/text_999999" android:background="@color/white" /> <com.broker.liming.widget.ClearEditText2 android:id="@+id/cet_car_logo" android:layout_toRightOf="@id/car_logo" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingEnd="@dimen/lay_15" android:paddingLeft="@dimen/lay_15" android:background="@color/white" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:textCursorDrawable="@null" android:singleLine="true" android:gravity="center_vertical|right" android:textColor="@color/text_333333" android:textColorHint="@color/text_hint" android:textSize="@dimen/font_14" android:hint="請輸入車險品牌" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="@dimen/lay_0.5" android:layout_marginRight="@dimen/lay_15" android:layout_marginLeft="@dimen/lay_15" android:background="@color/line" /> <RelativeLayout android:background="@color/white" android:layout_width="match_parent" android:layout_height="@dimen/lay_45"> <TextView android:id="@+id/car_insuretype" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingStart="@dimen/lay_15" android:text="車險型別" android:gravity="center_vertical" android:textSize="@dimen/font_14" android:textColor="@color/text_999999" android:background="@color/white" /> <com.broker.liming.widget.ClearEditText2 android:id="@+id/cet_car_insuretype" android:layout_toRightOf="@id/car_insuretype" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingEnd="@dimen/lay_15" android:paddingLeft="@dimen/lay_15" android:background="@color/white" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:textCursorDrawable="@null" android:singleLine="true" android:gravity="center_vertical|right" android:textColor="@color/text_333333" android:textColorHint="@color/text_hint" android:textSize="@dimen/font_14" android:hint="請輸入車險型別" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="@dimen/lay_0.5" android:layout_marginRight="@dimen/lay_15" android:layout_marginLeft="@dimen/lay_15" android:background="@color/line" /> <RelativeLayout android:id="@+id/ev_end_date" android:background="@color/white" android:layout_width="match_parent" android:layout_height="@dimen/lay_45"> <TextView android:id="@+id/vocher_end" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingStart="@dimen/lay_15" android:text="車險到期日期" android:gravity="center_vertical" android:textSize="@dimen/font_14" android:textColor="@color/text_999999" /> <TextView android:id="@+id/tv_vocher_end_date" android:layout_toRightOf="@id/vocher_end" android:layout_width="match_parent" android:drawableRight="@mipmap/birthday" android:drawablePadding="@dimen/lay_5" android:layout_height="match_parent" android:gravity="center_vertical|right" android:layout_alignParentRight="true" android:paddingEnd="@dimen/lay_15" android:textSize="@dimen/font_14" android:textColor="@color/text_333333" android:textColorHint="@color/text_hint" android:hint="請選擇車險到期日期" /> </RelativeLayout> </LinearLayout> </LinearLayout>
CompileCarActivity.class
//記得activity要實現此監聽回撥 CompileCarInfoActivity extends BaseActivity implements CarCompileRecyclerAdapter.DeleteOnclickListener //初始化 監聽回撥 在onCreatView中 CarCompileRecyclerAdapter.setDeleteOnclickListener(this); //實現刪除的監聽回撥方法 @Override public void deleteOnClick(VehicleMsgBean vehicleMsgBean, int position) { mPosition=position; recyclerAdapter.remove(position); LogUtil2.log("car====================>>>delterint:" + position+":date:"+map.size()); } //新增車輛按鈕,需要新增一個新佈局 iv_add_newcar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { VehicleMsgBean add=new VehicleMsgBean(); add.uuid=""; add.delFlag=""; add.carInsurenceType=""; add.carInsurenceEndDate=""; add.carInsurenceBrand=""; add.carNo=""; add.serial=""; add.brand=""; // vehicleMsgBeans.add(add); recyclerAdapter.setCarData(add,logoBean); for (int i=0;i<vehicleMsgBeans.size();i++){ LogUtil2.log("car====================>>>增加後toString:" +":date:"+vehicleMsgBeans.get(i).toString()); LogUtil2.log("car====================>>>增加後carNo:" +":date:"+vehicleMsgBeans.get(i).carNo.toString()); LogUtil2.log("car====================>>>增加後position:" + position+":date:"+vehicleMsgBeans.get(i).carNo); } } });
//初始化剛進入,有車輛資料時就顯示(走if),無車輛資料時,就建立一個空佈局(走else)
if (vehicleMsgBeans!=null&&vehicleMsgBeans.size()>0&&logoBean!=null&&logoBean.size()>0){
LinearLayoutManager manager=new LinearLayoutManager(mContext);
recyclerview.setLayoutManager(manager);
recyclerAdapter=new CarCompileRecyclerAdapter(CompileCarInfoActivity.this);
recyclerview.setAdapter(recyclerAdapter);
for (int i=0;i<vehicleMsgBeans.size();i++){
recyclerAdapter.setCarData(vehicleMsgBeans.get(i),logoBean);
}
// recyclerAdapter.notifyDataSetChanged();
for (int i=0;i<vehicleMsgBeans.size();i++){
map.put(i,vehicleMsgBeans.get(i));
}
}else {
VehicleMsgBean add=new VehicleMsgBean();
add.uuid="";
add.delFlag="";
add.carInsurenceType="";
add.carInsurenceEndDate="";
add.carInsurenceBrand="";
add.carNo="";
add.serial="";
add.brand="";
vehicleMsgBeans.add(add);
map.put(0,add);
LinearLayoutManager manager=new LinearLayoutManager(mContext);
recyclerview.setLayoutManager(manager);
recyclerAdapter=new CarCompileRecyclerAdapter(CompileCarInfoActivity.this);
recyclerview.setAdapter(recyclerAdapter);
for (int i=0;i<vehicleMsgBeans.size();i++){
recyclerAdapter.setCarData(vehicleMsgBeans.get(i),logoBean);
}
}
CarCompileRecyclerAdapter
package com.broker.liming.adapter;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
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.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.broker.liming.R;
import com.broker.liming.activity.CompileCarInfoActivity;
import com.broker.liming.bean.CarLogoSearchBean;
import com.broker.liming.bean.VehicleMsgBean;
import com.broker.liming.contact.CompareSootString;
import com.broker.liming.contact.CompareSortCar;
import com.broker.liming.utils.DateUtil;
import com.broker.liming.utils.LogUtil;
import com.broker.liming.utils.LogUtil2;
import com.broker.liming.widget.ClearEditText2;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.zip.Inflater;
import static com.broker.liming.activity.MyPaperActivity.closeSoftKeyboard;
/**
* @author chenhuirong
* @Date 2018/11/19
* @Description
*/
public class CarCompileRecyclerAdapter extends RecyclerView.Adapter<CarCompileRecyclerAdapter.MyViewHolder>{
private CompileCarInfoActivity mContext;
private List<VehicleMsgBean> vehicleMsgBeans=new ArrayList<>();
private TimePickerView pvTime;
private List<CarLogoSearchBean> logoBean;
private final boolean mBoundWatcher = true;
int expandPosition = -1;
public CarCompileRecyclerAdapter(Context mContext) {
this.mContext = (CompileCarInfoActivity) mContext;
}
//新增車輛,新增一個空佈局
public void setCarData( VehicleMsgBean mvehicleMsgBean,List<CarLogoSearchBean> logoSearchBean){
vehicleMsgBeans.add(mvehicleMsgBean);
this.logoBean = logoSearchBean;
notifyDataSetChanged();
}
//刪除一個item,只需要刪除對應position的物件就行
public void remove(@IntRange(from = 0) int position) {
vehicleMsgBeans.remove(position);
notifyDataSetChanged();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.avtivity_car_compile_item,parent,false));
}
@Override
public void onBindViewHolder(final MyViewHolder viewHolder, final int position) {
LogUtil2.log("car====================>>>vehicleMsgBeans:" +vehicleMsgBeans.get(position).toString());
//此物件一定要設定成方法變數,不要設定成全域性的,不然每次輸入資料,就會調監聽,資料都會替換一次,從而會導致資料錯亂(當時由於這個小問題耽誤一下午的時間)
final VehicleMsgBean vehicleMsgBean=vehicleMsgBeans.get(position);
//一定要加的,強制關閉Recyclerview的複用,不然由於複用,資料就會錯亂
viewHolder.setIsRecyclable(false)
//根據點選position和expandPosition決定detail是否可見,點選可見時其餘設定為不可見
if (position==expandPosition){
viewHolder.ll_hind_show.setVisibility(View.VISIBLE);
viewHolder.tv_hebing.setText("收起");
Drawable drawable = mContext.getResources().getDrawable(R.mipmap.pack_up);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 設定邊界
// param 左上右下
viewHolder.tv_hebing.setCompoundDrawables(null,null,drawable,null);
}else {
viewHolder.ll_hind_show.setVisibility(View.GONE);
viewHolder.tv_hebing.setText("展開");
Drawable drawable = mContext.getResources().getDrawable(R.mipmap.expand);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 設定邊界
// param 左上右下
viewHolder.tv_hebing.setCompoundDrawables(null,null,drawable,null);
}
//保證把每次輸入的資料都能賦值給物件,並儲存
viewHolder.cet_car_insuretype.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// vehicleMsgBean.carInsurenceType=viewHolder.cet_car_insuretype.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
vehicleMsgBean.carInsurenceType=viewHolder.cet_car_insuretype.getText().toString();
}
@Override
public void afterTextChanged(Editable s) {
//使用者輸入完畢後,處理輸入資料,回撥給主介面處理
// SaveEditextLisetener listener= (SaveEditextLisetener) mContext;
// if(s!=null){
// vehicleMsgBeans.get(position).carInsurenceType=viewHolder.cet_car_insuretype.getText().toString();
// vehicleMsgBean.carInsurenceType=viewHolder.cet_car_insuretype.getText().toString();
// vehicleMsgBeans.set(position,vehicleMsgBean);
// listener.saveEditext(position,vehicleMsgBean);
// listener.saveEditext((int) viewHolder.cet_car_insuretype.getTag(),vehicleMsgBean);
// }
LogUtil2.log("vehicleMsgBean.carInsurenceEndDate-----------carInsurenceType:"+vehicleMsgBean.carInsurenceType+"--position--"+position);
}
});
//Integer.parseInt(viewHolder.tv_enddate.getTag().toString())
viewHolder. tv_enddate.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// vehicleMsgBean.carInsurenceEndDate=viewHolder. tv_enddate.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
vehicleMsgBean.carInsurenceEndDate=viewHolder. tv_enddate.getText().toString();
}
@Override
public void afterTextChanged(Editable s) {
LogUtil2.log("vehicleMsgBean.carInsurenceEndDate-----------carInsurenceEndDate:"+vehicleMsgBean.carInsurenceEndDate+"--position--"+position);
}
});
viewHolder.cet_model.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// vehicleMsgBean.serial=viewHolder.cet_model.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
vehicleMsgBean.serial=viewHolder.cet_model.getText().toString();
}
@Override
public void afterTextChanged(Editable s) {
}
});
viewHolder.cet_carlogo.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// vehicleMsgBean.carInsurenceBrand=viewHolder.cet_carlogo.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
vehicleMsgBean.carInsurenceBrand=viewHolder.cet_carlogo.getText().toString();
}
@Override
public void afterTextChanged(Editable s) {
//使用者輸入完畢後,處理輸入資料,回撥給主介面處理
// SaveEditextLisetener listener= (SaveEditextLisetener) mContext;
LogUtil2.log("vehicleMsgBean.carInsurenceEndDate-----------carInsurenceBrand:"+vehicleMsgBean.carInsurenceBrand+"--position--"+position);
}
});
viewHolder.tv_cartype.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// vehicleMsgBean.brand=viewHolder.tv_cartype.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
vehicleMsgBean.brand=viewHolder.tv_cartype.getText().toString();
}
@Override
public void afterTextChanged(Editable s) {
LogUtil2.log("vehicleMsgBean.carInsurenceEndDate-----------brand:"+vehicleMsgBean.brand+"--position--"+position);
}
});
viewHolder.cet_carnumber.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// vehicleMsgBean.carNo=viewHolder.cet_carnumber.getText().toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
vehicleMsgBean.carNo=viewHolder.cet_carnumber.getText().toString();
}
@Override
public void afterTextChanged(Editable s) {
}
});
viewHolder. tv_title.setText("車輛"+(position+1));
viewHolder.cet_carnumber.setText(vehicleMsgBean.carNo);
viewHolder.cet_carlogo.setText(vehicleMsgBean.carInsurenceBrand);
viewHolder.tv_cartype.setText(vehicleMsgBean.brand);
viewHolder.cet_model.setText(vehicleMsgBean.serial);
viewHolder.cet_car_insuretype.setText(vehicleMsgBean.carInsurenceType);
LogUtil2.log("vehicleMsgBean.carInsurenceEndDate-----------BindowinputPosition:"+vehicleMsgBeans.get(position).carInsurenceEndDate+"--position--"+position);
if (vehicleMsgBeans.get(position).carInsurenceEndDate!=null&&!vehicleMsgBeans.get(position).carInsurenceEndDate.equals("")){
if (vehicleMsgBeans.get(position).carInsurenceEndDate.contains("年")){
int endyears = Integer.parseInt(vehicleMsgBeans.get(position).carInsurenceEndDate.substring(0,vehicleMsgBeans.get(position).carInsurenceEndDate.indexOf("年")));
int endmonth = Integer.parseInt(vehicleMsgBeans.get(position).carInsurenceEndDate.substring(vehicleMsgBeans.get(position).carInsurenceEndDate.indexOf("年") + 1, vehicleMsgBeans.get(position).carInsurenceEndDate.lastIndexOf("月")));
int endday = Integer.parseInt(vehicleMsgBeans.get(position).carInsurenceEndDate.substring(vehicleMsgBeans.get(position).carInsurenceEndDate.indexOf("月") + 1,vehicleMsgBeans.get(position).carInsurenceEndDate.lastIndexOf("日")));
viewHolder. tv_enddate.setText(endyears+"年"+endmonth+"月"+endday+"日");
}else {
int years = Integer.parseInt(vehicleMsgBeans.get(position).carInsurenceEndDate.substring(0, vehicleMsgBeans.get(position).carInsurenceEndDate.indexOf("-")));
int month = Integer.parseInt(vehicleMsgBeans.get(position).carInsurenceEndDate.substring(vehicleMsgBeans.get(position).carInsurenceEndDate.indexOf("-") + 1, vehicleMsgBeans.get(position).carInsurenceEndDate.lastIndexOf("-")));
int day = Integer.parseInt(vehicleMsgBeans.get(position).carInsurenceEndDate.substring(vehicleMsgBeans.get(position).carInsurenceEndDate.lastIndexOf("-") + 1));
viewHolder. tv_enddate.setText(years+"年"+month+"月"+day+"日");
}
}else {
viewHolder. tv_enddate.setText("");
}
viewHolder. tv_title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(position);
// deleteOnclickListener.deleteOnClick(vehicleMsgBeans.get(position),position);
}
});
viewHolder.tv_hebing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
togglePosition(position);
// viewHolder. ll_hind_show.setFocusable(true);
// if (viewHolder. ll_hind_show.getVisibility()==View.VISIBLE){
// viewHolder.ll_hind_show.setVisibility(View.GONE);
// viewHolder.tv_hebing.setText("展開");
// Drawable drawable = mContext.getResources().getDrawable(R.mipmap.expand);
// drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 設定邊界
// // param 左上右下
// viewHolder.tv_hebing.setCompoundDrawables(null,null,drawable,null);
//
// }else {
// viewHolder.ll_hind_show.setVisibility(View.VISIBLE);
// viewHolder.tv_hebing.setText("收起");
// Drawable drawable = mContext.getResources().getDrawable(R.mipmap.pack_up);
// drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());// 設定邊界
// // param 左上右下
// viewHolder.tv_hebing.setCompoundDrawables(null,null,drawable,null);
//
// }
}
});
viewHolder.rv_car_type.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPickerView( viewHolder.tv_cartype,logoBean);
}
});
viewHolder.ev_enddate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectDate(v,viewHolder.tv_enddate);
}
});
}
public void togglePosition(int position) {
if (expandPosition != position) {//如果閃屏就用notifydatasetchange
notifyItemChanged(expandPosition);
expandPosition = position;
} else {
expandPosition = -1;
}
notifyDataSetChanged();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemCount() {
return vehicleMsgBeans.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv_title;
RelativeLayout rv_car_type;
TextView tv_cartype;
ClearEditText2 cet_model;
ClearEditText2 cet_carnumber;
ClearEditText2 cet_carlogo;
RelativeLayout ev_enddate;
TextView tv_enddate;
TextView tv_hebing;
LinearLayout ll_hind_show;
TextView cet_car_insuretype;
public MyViewHolder(View view) {
super(view);
ll_hind_show=(LinearLayout) view.findViewById(R.id.ll_hind_show);
tv_title=(TextView) view.findViewById(R.id.tv_title);
cet_model=(ClearEditText2)view.findViewById(R.id.cet_model);
cet_carnumber=(ClearEditText2)view.findViewById(R.id.cet_car_number);
cet_carlogo=(ClearEditText2)view.findViewById(R.id.cet_car_logo);
ev_enddate=(RelativeLayout) view.findViewById(R.id.ev_end_date);
tv_enddate=(TextView) view.findViewById(R.id.tv_vocher_end_date);
rv_car_type=(RelativeLayout) view.findViewById(R.id.rv_car_type);
tv_cartype=(TextView) view.findViewById(R.id.tv_cartype);
cet_car_insuretype=(TextView) view.findViewById(R.id.cet_car_insuretype);
tv_hebing=(TextView) view.findViewById(R.id.tv_hebing);
}
}
//此方法暴露出來,activity通過此方法即可取到輸入的每條資料,即可把資料傳給後臺
public List<VehicleMsgBean> getItems() {
return vehicleMsgBeans;
}
//此方法是一個彈框,根據返回的json串來設定成二級聯動的選擇框
private void showPickerView(final TextView tv_cartype, List<CarLogoSearchBean> logoBean) {
if (logoBean!=null){
List<String> options1Items=new ArrayList<>();
final List< List<CarLogoSearchBean>> options2Items=new ArrayList<>();
Map<String, List<CarLogoSearchBean>> map=new TreeMap<>();
for (Iterator it = logoBean.iterator(); it.hasNext();){
CarLogoSearchBean carLogoSearchBean= (CarLogoSearchBean) it.next();
//集合中包含相同的Key,直接存起來
if (map.containsKey(carLogoSearchBean.initials)){
List<CarLogoSearchBean> listData=map.get(carLogoSearchBean.initials);
listData.add(carLogoSearchBean);
//不包含相同的key,建立一個集合以此key存起來
}else {
List<CarLogoSearchBean> syn=new ArrayList<>();
syn.add(carLogoSearchBean);
map.put(carLogoSearchBean.initials,syn);
}
}
//遍歷集合
options2Items.clear();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
options1Items.add((String) entry.getKey());
options2Items.add((List<CarLogoSearchBean>) entry.getValue());
}
OptionsPickerView pvOptions = new OptionsPickerView.Builder(mContext, new OptionsPickerView.OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
//返回的分別是三個級別的選中位置
// String text = options1Items.get(options1).provinceName + "-" + options2Items.get(options1).get(options2).cityName + "-" + options3Items.get(options1).get(options2).get(options3).regionName;
// tv_adress.setText(text);
tv_cartype .setText(options2Items.get(options1).get(options2).brand);
// mProvince = options1Items.get(options1).provinceCode;
// mCity = options2Items.get(options1).get(options2).cityCode;
// mRegion = options3Items.get(options1).get(options2).get(options3).regionCode;
}
}).setTitleText("地址選擇")
.setLabels("", "", "")
.setSelectOptions(0, 0, 0)
.setContentTextSize(16)
.setTitleSize(18)
.setSubCalSize(16)
.setCancelColor(mContext.getResources().getColor(R.color.text_999999))
.setSubmitColor(mContext.getResources().getColor(R.color.text_999999))
.setTitleColor(mContext.getResources().getColor(R.color.text_333333))
.setTitleBgColor(mContext.getResources().getColor(R.color.white))
.setBgColor(mContext.getResources().getColor(R.color.bg_FAFAFA))
.setDividerColor(mContext.getResources().getColor(R.color.line))
.setTextColorCenter(mContext.getResources().getColor(R.color.text_333333))
.setOutSideCancelable(false)
.build();
// pvOptions.setPicker(options1Items);//一級選擇器
pvOptions.setPicker(options1Items, options2Items);//二級選擇器
// pvOptions.setPicker(options1Items, options2Items, options3Items);//三級選擇器
pvOptions.show();
}
}
public void selectDate(View v, final TextView tv_enddate){
closeSoftKeyboard(mContext, v);
Calendar selectedDate = Calendar.getInstance();
if(!tv_enddate.getText().toString().equals("")){
int years = Integer.parseInt(tv_enddate.getText().toString().substring(0, tv_enddate.getText().toString().indexOf("年")));
int month = Integer.parseInt(tv_enddate.getText().toString().substring(tv_enddate.getText().toString().indexOf("年") + 1, tv_enddate.getText().toString().lastIndexOf("月")));
int day = Integer.parseInt(tv_enddate.getText().toString().substring(tv_enddate.getText().toString().indexOf("月") + 1,tv_enddate.getText().toString().lastIndexOf("日")));
selectedDate.set(years, month - 1, day);
}
Calendar startDate = Calendar.getInstance();
startDate.set(1990,0,1);//設定起始年份
Calendar endDate = Calendar.getInstance();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String toDay = df.format(Calendar.getInstance().getTime());
int years = Integer.parseInt(toDay.substring(0, toDay.indexOf("-")));
int month = Integer.parseInt(toDay.substring(toDay.indexOf("-") + 1, toDay.lastIndexOf("-")));
int day = Integer.parseInt(toDay.substring(toDay.lastIndexOf("-") + 1));
endDate.set(years, month - 1, day);//設定結束年份
pvTime = new TimePickerView.Builder(mContext, new TimePickerView.OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {
String data= DateUtil.getDate(date);
int years = Integer.parseInt(data.substring(0, data.indexOf("-")));
int month = Integer.parseInt(data.substring(data.indexOf("-") + 1, data.toString().lastIndexOf("-")));
int day = Integer.parseInt(data.toString().substring(data.toString().lastIndexOf("-") + 1));
tv_enddate.setText(years+"年"+month+"月"+day+"日");
}
}).setType(TimePickerView.Type.YEAR_MONTH_DAY)//預設全部顯示
.setCancelText("取消")
.setSubmitText("確認")
.setTitleSize(18)//標題文字大小
.setSubCalSize(16)//確定取消按鈕大小
.setContentSize(16)//滾輪文字大小
.setTitleText("出險日期")//標題文字
.setOutSideCancelable(true)//點選螢幕,點在控制元件外部範圍時,是否取消顯示
.isCyclic(false)//是否迴圈滾動
.setTitleColor(Color.BLACK)//標題文字顏色
.setSubmitColor(Color.BLACK)//確定按鈕文字顏色
.setCancelColor(Color.BLACK)//取消按鈕文字顏色
.setTitleBgColor(mContext.getResources().getColor(R.color.bg_FAFAFA))//標題背景顏色
.setBgColor(Color.WHITE)//滾輪背景顏色
.setTextColorCenter(mContext.getResources().getColor(R.color.text_333333))//選中文字顏色
.setTextColorOut(mContext.getResources().getColor(R.color.text_999999))//未選中文字顏色
// .setRange(startDate.get(Calendar.YEAR), endDate.get(Calendar.YEAR))//預設是1900-2100年
.setDate(selectedDate)// 如果不設定的話,預設是系統時間*/
// .setRangDate(startDate, endDate)//起始終止年月日設定
.setLabel("年", "月", "日", "時", "分", "秒")
.isCenterLabel(false) //是否只顯示中間選中項的label文字,false則每項item全部都帶有label。
.isDialog(false)//是否顯示為對話方塊樣式
.setDividerColor(mContext.getResources().getColor(R.color.line))//設定分割線的顏色
.build();
pvTime.show();
}
public static DeleteOnclickListener deleteOnclickListener;
public static interface DeleteOnclickListener{
void deleteOnClick(VehicleMsgBean vehicleMsgBean,int position);
}
public static void setDeleteOnclickListener(CarCompileRecyclerAdapter.DeleteOnclickListener mdeleteOnclickListener){
deleteOnclickListener=mdeleteOnclickListener;
}
public interface SaveEditextLisetener{
void saveEditext(int position,VehicleMsgBean date);
}
public static SaveEditextLisetener saveEditextLisetener;
public static void saveEditextLisetener(CarCompileRecyclerAdapter.SaveEditextLisetener msaveEditextLisetener){
saveEditextLisetener=msaveEditextLisetener;
}
}