美文网首页
Recycler所有adapter的父类-BaseRecycle

Recycler所有adapter的父类-BaseRecycle

作者: EraJieZhang | 来源:发表于2018-12-11 14:15 被阅读0次

    BaseRecyclerAdapter 需要配合RecyclerViewHolder 一起使用

    BaseRecyclerAdapter:

    /**
     * Created by EraJieZhang
     */
    
    public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerViewHolder> {
        protected final List<T> mData;
        protected final Context mContext;
        protected LayoutInflater mInflater;
        private OnItemClickListener mClickListener;
        private OnItemLongClickListener mLongClickListener;
    
        public BaseRecyclerAdapter(Context ctx, List<T> list) {
            mData = (list != null) ? list : new ArrayList<T>();
            mContext = ctx;
            mInflater = LayoutInflater.from(ctx);
        }
    
        @Override
        public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            final RecyclerViewHolder holder = new RecyclerViewHolder(mContext,
                    mInflater.inflate(getItemLayoutId(viewType), parent, false));
            if (mClickListener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                    }
                });
            }
            if (mLongClickListener != null) {
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        mLongClickListener.onItemLongClick(holder.itemView, holder.getLayoutPosition());
                        return true;
                    }
                });
            }
            return holder;
        }
    
        @Override
        public void onBindViewHolder(RecyclerViewHolder holder, int position) {
            bindData(holder, position, mData.get(position));
        }
    
        @Override
        public int getItemCount() {
            return mData.size();
        }
    
        public void add(int pos, T item) {
            mData.add(pos, item);
            notifyItemInserted(pos);
        }
    
        public void delete(int pos,String  defaultprompt) {
            if (mData.size()>pos){
                mData.remove(pos);
                notifyItemRemoved(pos);
            }else {
                Toast.makeText(mContext,defaultprompt, Toast.LENGTH_SHORT).show();
            }
        }
    
        public void setOnItemClickListener(OnItemClickListener listener) {
            mClickListener = listener;
        }
    
        public void setOnItemLongClickListener(OnItemLongClickListener listener) {
            mLongClickListener = listener;
        }
    
        /**
         * 设置item的布局
         * @param viewType
         * @return
         */
        abstract public int getItemLayoutId(int viewType);
    
        /**
         * 设置数据
         * @param holder
         * @param position
         * @param item
         */
        abstract public void bindData(RecyclerViewHolder holder, int position, T item);
    
        /**
         * 添加每个条目的点击事件
         */
        public interface OnItemClickListener {
            public void onItemClick(View itemView, int pos);
        }
    
        /**
         * 添加每个条目的长按事件
         */
        public interface OnItemLongClickListener {
            public void onItemLongClick(View itemView, int pos);
        }
    }
    

    RecyclerViewHolder :

    public class RecyclerViewHolder extends RecyclerView.ViewHolder {
        private SparseArray<View> mViews;//集合类,layout里包含的View,以view的id作为key,value是view对象
        private Context mContext;//上下文对象
    
        public RecyclerViewHolder(Context ctx, View itemView) {
            super(itemView);
            mContext = ctx;
            mViews = new SparseArray<View>();
        }
    
        private <T extends View> T findViewById(int viewId) {
            View view = mViews.get(viewId);
            if (view == null) {
                view = itemView.findViewById(viewId);
                mViews.put(viewId, view);
            }
            return (T) view;
        }
    
        public View getView(int viewId) {
            return findViewById(viewId);
        }
    
        public TextView getTextView(int viewId) {
            return (TextView) getView(viewId);
        }
    
        public Button getButton(int viewId) {
            return (Button) getView(viewId);
        }
    
        public ImageView getImageView(int viewId) {
            return (ImageView) getView(viewId);
        }
    
        public ImageButton getImageButton(int viewId) {
            return (ImageButton) getView(viewId);
        }
    
        public EditText getEditText(int viewId) {
            return (EditText) getView(viewId);
        }
    
        public RecyclerViewHolder setText(int viewId, String value) {
            TextView view = findViewById(viewId);
            view.setText(value);
            return this;
        }
    
        public RecyclerViewHolder setBackground(int viewId, int resId) {
            View view = findViewById(viewId);
            view.setBackgroundResource(resId);
            return this;
        }
    
        public RecyclerViewHolder setClickListener(int viewId, View.OnClickListener listener) {
            View view = findViewById(viewId);
            view.setOnClickListener(listener);
            return this;
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Recycler所有adapter的父类-BaseRecycle

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