package com.king.applib.ui.recyclerview;

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

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

/**
 * 基础RecyclerView.Adapter
 *
 * @author VanceKing
 * @since 2017/3/30.
 */

public abstract class BaseRecyclerViewAdapter<E> extends RecyclerView.Adapter<RecyclerHolder> {
    private final List<E> mAdapterList = new ArrayList<>();
    private OnItemClickListener<E> mOnItemClickListener;

    public BaseRecyclerViewAdapter() {
    }

    public BaseRecyclerViewAdapter(List<E> adapterData) {
        addDataList(adapterData, false);
    }

    public List<E> getAdapterData() {
        return mAdapterList;
    }

    public void setAdapterData(List<E> adapterData) {
        setAdapterData(adapterData, false);
    }

    public void setAdapterData(List<E> adapterData, boolean append) {
        addDataList(adapterData, append);
    }

    protected void addDataList(List<E> adapterData, boolean append) {
        if (isListEmpty(adapterData)) {
            return;
        }
        if (append) {
            mAdapterList.addAll(adapterData);
        } else {
            if (!isListEmpty(mAdapterList)) {
                mAdapterList.clear();
            }
            mAdapterList.addAll(adapterData);
        }
        notifyDataSetChanged();
    }

    public void clearAdapterData() {
        mAdapterList.clear();
        notifyDataSetChanged();
    }

    private boolean isListEmpty(List<E> list) {
        return list == null || list.isEmpty();
    }

    public void addData(int position, E entity) {
        if (position >= 0 && position <= mAdapterList.size()) {
            mAdapterList.add(position, entity);
            notifyItemInserted(position);
        }
    }

    public void appendList(List<E> appendedList) {
        if (isListEmpty(appendedList)) {
            return;
        }
        int positionStart = mAdapterList.size();
        mAdapterList.addAll(positionStart, appendedList);
        //RecyclerView 嵌套 NestScrollView 无效。notifyDataSetChanged()有效。
        notifyItemRangeInserted(positionStart, appendedList.size());
    }

    public void deleteData(int position) {
        if (checkPosition(position)) {
            mAdapterList.remove(position);
            notifyItemRemoved(position);
        }
    }

    public void modifyData(int position, E e) {
        if (checkPosition(position)) {
            mAdapterList.remove(position);
            mAdapterList.add(position, e);
            notifyItemChanged(position);
        }
    }

    private boolean checkPosition(int position) {
        return position >= 0 && position < mAdapterList.size();
    }

    public E getItem(int position) {
        return mAdapterList.get(position);
    }

    @Override
    public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getItemLayoutRes(viewType), parent, false);
        return new RecyclerHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerHolder holder, int position) {
        if (!checkPosition(position)) {
            return;
        }
        final E e = mAdapterList.get(position);
        if (e != null) {
            convert(holder, e, position);
        }

        /*holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(e);
                }
            }
        });*/
    }

    public abstract void convert(RecyclerHolder holder, E item, int position);

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

    @LayoutRes public abstract int getItemLayoutRes(int viewType);

    public void setOnItemClickListener(OnItemClickListener<E> listener) {
        mOnItemClickListener = listener;
    }


    public interface OnItemClickListener<E> {
        void onItemClick(E e);
    }
}

@@ -35,16 +35,48 @@
 rview;%0A%0A
+import android.content.Context;%0A
 import a
@@ -101,25 +101,23 @@
 otation.
-LayoutRes
+NonNull
 ;%0Aimport
@@ -542,65 +542,8 @@
 %3E();
-%0A    private OnItemClickListener%3CE%3E mOnItemClickListener;
 %0A%0A  
@@ -2830,32 +2830,41 @@
 on);%0A    %7D%0A%0A    
+@NonNull 
 @Override%0A    pu
@@ -2902,16 +2902,25 @@
 wHolder(
+@NonNull 
 ViewGrou
@@ -2964,16 +2964,49 @@
 w view =
+ getItemLayoutRes(viewType) %3E 0 ?
  LayoutI
@@ -3085,24 +3085,86 @@
 rent, false)
+ :%0A                generateView(parent.getContext(), viewType)
 ;%0A        re
@@ -3248,16 +3248,25 @@
 wHolder(
+@NonNull 
 Recycler
@@ -3494,290 +3494,8 @@
    %7D
-%0A%0A        /*holder.itemView.setOnClickListener(new View.OnClickListener() %7B%0A            @Override public void onClick(View v) %7B%0A                if (mOnItemClickListener != null) %7B%0A                    mOnItemClickListener.onItemClick(e);%0A                %7D%0A            %7D%0A        %7D);*/
 %0A   
@@ -3675,19 +3675,8 @@
     
-@LayoutRes 
 publ
@@ -3733,201 +3733,87 @@
    p
-ublic void setOnItemClickListener(OnItemClickListener%3CE%3E listener) %7B%0A        mOnItemClickListener = listener;%0A    %7D%0A%0A%0A    public interface OnItemClickListener%3CE%3E %7B%0A        void onItemClick(E e)
+rotected View generateView(Context context, int viewType) %7B%0A        return null
 ;%0A  

重构 BaseRecyclerViewAdapter