美文网首页
Android——RecyclerView实现item的增删,i

Android——RecyclerView实现item的增删,i

作者: 未聞椛洺 | 来源:发表于2017-07-15 11:03 被阅读0次

    直接贴代码

    packagecom.etcxc.android.ui.adapter;

    importandroid.content.Context;

    importandroid.support.v7.widget.RecyclerView;

    importandroid.view.LayoutInflater;

    importandroid.view.View;

    importandroid.view.ViewGroup;

    importandroid.widget.ImageView;

    importandroid.widget.TextView;

    importandroid.widget.Toast;

    importcom.etcxc.android.R;

    importcom.etcxc.android.base.App;

    importcom.etcxc.android.bean.OrderRechargeInfo;

    importjava.util.ArrayList;

    import staticcom.etcxc.android.utils.UIUtils.saveInfoList;

    /**

    * Created by刘涛on 2017/7/6 0006.

    */

    public classMyRechaergeRecylerViewAdapterextendsRecyclerView.AdapterimplementsView.OnClickListener {

    privateArrayListlist=null ;

    privateContextcon;

    privateOnItemRechargeClickListenermOnItemRechargeClickListener=null;

    @Override

    public voidonClick(View v) {

    if(mOnItemRechargeClickListener!=null) {

    mOnItemRechargeClickListener.onItemRechargeClick((ImageView) v,(Integer) v.getTag());

    }

    }

    public voidsetmOnItemRechargeClickListener(OnItemRechargeClickListener listener) {

    this.mOnItemRechargeClickListener= listener;

    }

    public interfaceOnItemRechargeClickListener {

    voidonItemRechargeClick(ImageView view, intposition);

    }

    publicMyRechaergeRecylerViewAdapter(Context con,ArrayList list) {

    this.con= con;

    this.list= list;

    }

    @Override

    publicViewHolderonCreateViewHolder(ViewGroup parent, intviewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recharge_form_recylerview,parent, false);

    ViewHolder holer =newViewHolder(view);

    holer.deletebtn.setOnClickListener(this);

    returnholer;

    }

    @Override

    public voidonBindViewHolder(ViewHolder holder, intposition) {

    OrderRechargeInfo info =list.get(position);

    if(info !=null){

    holder.username.setText(info.getRechargename());

    holder.carnumber.setText(info.getCarnumber());

    holder.etccard.setText(info.getEtccarnumber());

    holder.moneynumber.setText(info.getRechargemoney());

    holder.deletebtn.setTag(position);

    }

    }

    @Override

    public intgetItemCount() {

    if(list!=null&&list.size() >0){

    returnlist.size();

    }

    return0;

    }

    public static classViewHolderextendsRecyclerView.ViewHolder {

    publicTextViewusername;

    publicTextViewcarnumber;

    publicTextViewetccard;

    publicTextViewmoneynumber;

    publicImageViewdeletebtn;

    publicViewHolder(View view) {

    super(view);

    username= (TextView) view.findViewById(R.id.item_card_name);

    carnumber= (TextView) view.findViewById(R.id.item_car_number);

    etccard= (TextView) view.findViewById(R.id.item_recharge_card_number);

    moneynumber= (TextView) view.findViewById(R.id.item_money_number);

    deletebtn= (ImageView) view.findViewById(R.id.item_etc_delete_img);

    }

    }

    public voidaddData(OrderRechargeInfo ord, intpoasation,TextView edt) {

    if(poasation ==0) {

    list=newArrayList<>();

    list.add(0,ord);

    saveInfoList(App.get(),list);

    edt.setText(list.size()+"");

    }else{

    list.add(0,ord);

    saveInfoList(App.get(),list);

    edt.setText(list.size()+"");

    }

    notifyItemInserted(0);

    notifyItemRangeChanged(0,list.size());

    }

    public voidremoveData(intposition) {

    if(list.size() <1&&list.size() !=0) {

    list.remove(0);

    notifyDataSetChanged();

    }else if(list.size() ==0) {

    Toast.makeText(con,R.string.nothing_isempty,Toast.LENGTH_SHORT).show();

    }else{//更新列表

    list.remove(position);

    notifyDataSetChanged();

    notifyItemRemoved(position);

    notifyItemRangeChanged(position,list.size());

    }

    }

    }

    相关文章

      网友评论

          本文标题:Android——RecyclerView实现item的增删,i

          本文链接:https://www.haomeiwen.com/subject/fdmahxtx.html