public abstract class BaseRecyclerViewAdapter<T> extends RecyclerView.Adapter<BaseRecyclerViewAdapter.BaseViewHolder> {

    List<T> dataSource;
    Activity activity;

    public static final int HEADER = 1;
    public static final int NORMAL = 2;
    public static final int FOOTER = 3;

    View headerView;

    View footerView;

    private OnItemClickListener onItemClickListener;

    public BaseRecyclerViewAdapter() {
        dataSource = getData();
        activity = getActivity();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case HEADER:
                return new BaseViewHolder(headerView);
            case NORMAL:
                return new BaseViewHolder(LayoutInflater.from(activity).inflate(getLayoutRes(), parent,
                        false));
            case FOOTER:
                return new BaseViewHolder(footerView);
            default:
                return new BaseViewHolder(LayoutInflater.from(activity).inflate(getLayoutRes(), parent,
                        false));
        }
    }

    @Override
    public void onBindViewHolder(BaseRecyclerViewAdapter.BaseViewHolder holder, int position) {

        if (NORMAL != holder.getItemViewType()) {
            return;
        }


        if (null != onItemClickListener) {
            holder.itemView.setOnClickListener(v ->
                    onItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition()));
            holder.itemView.setOnLongClickListener(v -> {
                onItemClickListener.onItemLongClick(holder.itemView, holder.getLayoutPosition());
                return false;
            });
        }
    }

    @Override
    public int getItemCount() {
        int dataCount = null == dataSource ? 0 : dataSource.size();
        if (null != headerView) {
            dataCount++;
        }
        if (null != footerView) {
            dataCount++;
        }
        return dataCount;
    }

    @Override
    public int getItemViewType(int position) {
        if (null != headerView && 0 == position) {
            return HEADER;
        }
        if (null != footerView && position == getItemCount() - 1) {
            return FOOTER;
        }
        return NORMAL;
    }

    public T getItemData(int position) {
        if (null != headerView) {
            position = position - 1;
        }
        if (null != dataSource && position >= 0 && position < dataSource.size()) {
            return dataSource.get(position);
        }
        return null;
    }

    public void setData(List<T> data) {
        dataSource = data;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    public void setHeader(View view) {
        headerView = view;
    }

    public void setFooter(View view) {
        footerView = view;
    }

    public void setHeader(@LayoutRes int layoutId) {
        headerView = LayoutInflater.from(activity).inflate(layoutId, null,
                false);
    }

    public void setFooter(@LayoutRes int layoutId) {
        footerView = LayoutInflater.from(activity).inflate(layoutId, null,
                false);
    }


    public abstract @LayoutRes int getLayoutRes();

    public abstract List<T> getData();

    public abstract Activity getActivity();

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        Map<Integer, View> viewMap;

        public BaseViewHolder(View itemView) {
            super(itemView);
            viewMap = new HashMap<>();
        }

        public View getView(@IdRes int id) {
            if (viewMap.containsKey(id)) {
                return viewMap.get(id);
            }
            View view = itemView.findViewById(id);
            viewMap.put(id, view);
            return view;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
        void onItemLongClick(View view , int position);
    }

}

使用示例

 BaseRecyclerViewAdapter recyclerViewAdapter = new BaseRecyclerViewAdapter<String>() {
            @Override
            public void onBindViewHolder(BaseRecyclerViewAdapter.BaseViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                TextView textView = (TextView) holder.getView(R.id.text_view);
                textView.setText(getItemData(position));
            }

            @Override
            public int getLayoutRes() {
                return R.layout.main_recycler_view_item;
            }

            @Override
            public List<String> getData() {
                List<String> data = new ArrayList<String>(10);
                for (int i = 0; i < 10; i++) {
                    data.add(String.valueOf(i));
                }
                return data;
            }

            @Override
            public Activity getActivity() {
                return activity;
            }

        };

        recyclerViewAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                SHJump.openUrl(activity, "http://baidu.com");
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        recyclerView.setAdapter(recyclerViewAdapter);

标签: none

评论已关闭