package lib.morkim.mfw.ui.lists;

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

import java.util.List;

public abstract class ListAdapter<M extends ListItemModel, VH extends ListItemHolder> extends RecyclerView.Adapter<VH> {

	protected List<M> listModel;

	private View.OnClickListener onClickListener;
	private View.OnLongClickListener onLongClickListener;

	public ListAdapter() {

	}

	public ListAdapter(List<M> listModel) {
		this.listModel = listModel;
	}

	@Override
	public VH onCreateViewHolder(final ViewGroup parent, int viewType) {

		View view = LayoutInflater.from(parent.getContext()).inflate(layoutId(), parent, false);
		view.setOnClickListener(onClickListener);
		view.setOnLongClickListener(onLongClickListener);

		VH holder = holdView(view, 0);

		view.setTag(holder);

		return holder;
	}

	@Override
	public void onBindViewHolder(VH holder, int position) {

		bindView(holder, position);
		holder.position = position;
	}

	protected abstract int layoutId();

	protected abstract VH holdView(View convertView, int position);

	protected abstract void bindView(VH holder, int position);

	public void updateData(List<M> listModel) {

		this.listModel = listModel;

		notifyDataSetChanged();
	}

	@Override
	public int getItemCount() {

		if (listModel != null)
			return listModel.size();

		return 0;
	}

	public M getItem(int position) {

		if (listModel != null)
			return listModel.get(position);

		return null;
	}

	@Override
	public long getItemId(int position) {

		return 0;
	}

	public boolean isEnabled(int position) {

		if (listModel != null)
			return listModel.get(position).isEnabled;

		return false;
	}

	public void setOnClickListener(View.OnClickListener onClickListener) {
		this.onClickListener = onClickListener;
	}

	public void setOnLongClickListener(View.OnLongClickListener onLongClickListener) {
		this.onLongClickListener = onLongClickListener;
	}
}

@@ -1004,38 +1004,8 @@
 n);%0A
-%09%09holder.position = position;%0A
 %09%7D%0A%0A

Remove view holder position