Recyclerview中有多条Editext,并有添加,删除item

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35821446/article/details/84986409

先说一下需求:需要添加多辆车的信息,车辆数无上限,默认显示一个空的车辆信息,可进行增,删操作,删除时又可全部删除!
先上效果图

 

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;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35821446/article/details/84986409
今日推荐