package com.philisense.rdgo.adapter;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.philisense.rdgo.R;
import com.philisense.rdgo.databinding.ItemLineInBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shaop on 2018/1/4.
 */

public class ReplaceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<String> dataArray;

    public ReplaceAdapter() {
        dataArray = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemLineInBinding mBindHeader = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_line_in, parent, false);
        return new ReplaceAdapter.LineViewHolder(mBindHeader.getRoot());
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        LineViewHolder lineViewHolder = (LineViewHolder) holder;
        lineViewHolder.bindView(dataArray.get(position));
    }

    public void addOne(String s) {
        dataArray.add(s);
        notifyItemInserted(dataArray.size()-1);
    }

    public List<String> getDataArray() {
        return dataArray;
    }

    @Override
    public int getItemCount() {
        return dataArray.size();
    }

    private class LineViewHolder extends RecyclerView.ViewHolder {

        ItemLineInBinding itemLineInBinding;

        LineViewHolder(View view) {
            super(view);
            itemLineInBinding = DataBindingUtil.getBinding(view);
        }

        public void bindView(String s) {
            itemLineInBinding.setX(s);
        }
    }
}

