美文网首页程序员android收集
构建BaseRecyclerAdapter,RecyclerVi

构建BaseRecyclerAdapter,RecyclerVi

作者: 饮水思源为名 | 来源:发表于2018-07-31 16:06 被阅读13次

      本文主要跟大家分享笔者简单搭建的BaseRecyclerAdapter。首先我们先看一下需求:

    1. 继承后可以舍去大量重复的复制粘贴操作
    2. 自动契合不同的Bean
    3. 有用点击监听和长按监听
    4. 指定位置对列表进行增删改
    5. 自带上拉加载监听

    实现思路;

      首先为了契合不同的bean,泛型肯定是必不可少的,然后我们需要用户传入Context即可,提供List的get、set方法。基本的Adapter就搭建完成了。对外提供两个方法,onMyCreateViewHolder和onMyBindViewHolder分别对应onCreateViewHolder和onBindViewHolder让用户自定义。利用接口回调的方式实现Item点击事件以及长按监听。最后指定项的增删改,可以自定义方法,通过notifyItem...()方法实现。

    代码:

    package com.wusy.wusylibrary.base;
    
    import android.content.Context;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;
    import android.view.ViewGroup;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by XIAO RONG on 2018/4/20.
     */
    
    public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private List<T> list;
        private Context context;
        private onRecyclerItemClickLitener onRecyclerItemClickLitener;
        public BaseRecyclerAdapter(Context context){
            this.context=context;
            list=new ArrayList<>();
        }
    
        public List<T> getList() {
            return list;
        }
    
        public void setList(List<T> list) {
            this.list = list;
        }
    
        public Context getContext() {
            return context;
        }
    
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return onMyCreateViewHolder(parent,viewType);
        }
    
        public abstract RecyclerView.ViewHolder onMyCreateViewHolder(ViewGroup parent, int viewType);
    
        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
            if(onRecyclerItemClickLitener!=null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onRecyclerItemClickLitener.onRecyclerItemClick(holder,position);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View view) {
                        onRecyclerItemClickLitener.onRecyclerItemLongClick(holder,position);
                        return false;
                    }
                });
            }
            onMyBindViewHolder(holder,position);
        }
    
        public abstract void onMyBindViewHolder(RecyclerView.ViewHolder holder, int position);
    
        @Override
        public int getItemCount() {
            return list.size();
        }
    
        /**
         *   指定位置添加item
         */
        public void addData(int position,T data) {
            list.add(position, data);
            notifyItemInserted(position);
        }
    
    
        /**
         * 指定位置移除item
         */
        public void removeData(int position) {
            list.remove(position);
            notifyItemRemoved(position);
        }
    
        public interface onRecyclerItemClickLitener {
            void onRecyclerItemClick(RecyclerView.ViewHolder view, int position);
            void onRecyclerItemLongClick(RecyclerView.ViewHolder view , int position);
        }
        public void setOnRecyclerItemClickLitener(onRecyclerItemClickLitener onRecyclerItemClickLitener)
        {
            this.onRecyclerItemClickLitener = onRecyclerItemClickLitener;
        }
    
        /**
         * 为RecyclerView添加上拉加载更多的实现接口
         * firstVisibleItem=页面显示的第一个Item的Position
         * visibleItemCount=页面显示的Item的数量
         * totalItemCount=总共的Item的数量
         * previousTotal=与totalItemCount做比较,用于判断是否可以执行加载
         * loading=是否处于加载中
         * currentPage=页数
         * firstVisibleItem+visibleItemCount=totalItemCount 即拉倒了最底部。
         * 当页面刷新时,必须将previousTotal变为0.否则无法执行上拉加载
         */
        public abstract static class LoadMoreRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
            private int previousTotal = 0;
            private boolean loading = true;
            int firstVisibleItem, visibleItemCount, totalItemCount;
            private int currentPage = 1;
    
            private LinearLayoutManager mLinearLayoutManager;
            public LoadMoreRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
                this.mLinearLayoutManager = linearLayoutManager;
            }
    
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                visibleItemCount = recyclerView.getChildCount();
                totalItemCount = mLinearLayoutManager.getItemCount();
                firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
                if (loading) {
                    if (totalItemCount > previousTotal) {
                        loading = false;
                        previousTotal = totalItemCount;
                    }
                }
                if (!loading && (totalItemCount - visibleItemCount) <= firstVisibleItem) {
                    currentPage++;
                    onLoadMore(currentPage);
                    loading = true;
                }
            }
            public abstract void onLoadMore(int currentPage);
            public void clearPreviousTotal(){
                previousTotal=0;
            }
        }
    }
    
    

    相关文章

      网友评论

        本文标题:构建BaseRecyclerAdapter,RecyclerVi

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