RecyclerView 通用适配器

£可爱£侵袭症+ 提交于 2019-12-02 06:07:58

 原文地址:https://www.jianshu.com/p/82a74c9ccba5

public abstract class RVBaseAdapter<T> extends RecyclerView.Adapter<RVBaseAdapter.CommonViewHolder> {

    protected List<T> mBeans;
    protected Context mContext;
    protected boolean mAnimateItems = true;
    protected int mLastAnimatedPosition = -1;

    public RVBaseAdapter(Context context, List<T> beans) {
        mContext = context;
        mBeans = beans;
    }

    @Override
    public RVBaseAdapter.CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(getItemLayoutID(viewType), parent, false);
        final CommonViewHolder holder = new CommonViewHolder(view);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick(holder.getAdapterPosition());
            }
        });
        onCreateClickListener(holder);
        return holder;
    }

    @Override
    public void onBindViewHolder(RVBaseAdapter.CommonViewHolder holder, int position) {
//        runEnterAnimation(holder.itemView, position);
        onBindDataToView(holder, mBeans.get(position));
    }

    /**
     * 为一些控件设置点击监听器
     * @param holder
     */

    protected abstract void onCreateClickListener(CommonViewHolder holder);

    /**
     * 绑定数据到Item的控件中去     *
     * @param holder
     * @param bean
     */
    protected abstract void onBindDataToView(CommonViewHolder holder, T bean);

    /**
     * 取得ItemView的布局文件     *
     * @return
     */
    public abstract int getItemLayoutID(int viewType);


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

    public void add(T bean) {
        mBeans.add(bean);
        notifyDataSetChanged();
    }

    public void addAll(List<T> beans) {
        mBeans.addAll(beans);
        notifyDataSetChanged();
    }

    public void clear() {
        mBeans.clear();
        notifyDataSetChanged();
    }

    /***
     * item的加载动画
     * @param view
     * @param position
     */
    private void runEnterAnimation(View view, int position) {
        if (!mAnimateItems) {
            return;
        }
        if (position > mLastAnimatedPosition) {
            mLastAnimatedPosition = position;
            view.setTranslationY(SystemUtils.getScreenHeight(mContext));
            view.animate()
                    .translationY(50)
                    .setStartDelay(100)
                    .setInterpolator(new DecelerateInterpolator(3.f))
                    .setDuration(300)
                    .start();
        }
    }


    public class CommonViewHolder extends RecyclerView.ViewHolder {
        private final SparseArray<View> mViews;
        private View itemView;

        public CommonViewHolder(View itemView) {
            super(itemView);
            this.mViews = new SparseArray<>();
            this.itemView = itemView;
            //添加Item的点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClick(getAdapterPosition());
                }
            });
        }

        public <T extends View> T getView(int viewId) {

            View view = mViews.get(viewId);
            if (view == null) {
                view = itemView.findViewById(viewId);
                mViews.put(viewId, view);
            }
            return (T) view;
        }

        public void setViewVisibility(int viewId, int visibilityType){
            View view = getView(viewId);
            view.setVisibility(visibilityType);
        }

        public void setText(int viewId, String text) {
            TextView tv = getView(viewId);
            tv.setText(text);
        }

        /**
         * 加载drawable中的图片
         *
         * @param viewId
         * @param resId
         */
        public void setImage(int viewId, int resId) {
            ImageView iv = getView(viewId);
            iv.setImageResource(resId);
        }

        /**
         * 加载网络上的图片
         *
         * @param viewId
         * @param url
         */
        public void setImageByGlide(int viewId, String url) {
            ImageView iv = getView(viewId);
            Glide.with(mContext)
                    .load(url)
                    .dontAnimate()
                    .fitCenter()
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
//                        .crossFade()//或者使用 dontAnimate() 关闭动画
                    .placeholder(R.drawable.picture_demo)
                    .error(R.drawable.picture_demo)
                    .into(iv);
        }
        public void setImageByImageLoader(int viewId, String url) {
            ImageView iv = getView(viewId);
            ImageLoader.getInstance().displayImage(url, iv);
        }
    }

    /**
     * ItemView的单击事件(如果需要,重写此方法就行)
     *
     * @param position
     */
    protected abstract void onItemClick(int position);

}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!