美文网首页菜鸟商城学习笔记
10-Adapter的封装以及使用

10-Adapter的封装以及使用

作者: 小楠总 | 来源:发表于2016-08-04 18:07 被阅读172次

    一、Adapter的封装

    由于我们的RecycleView在项目中很常用,因此有必要对Adapter进行封装。

    封装的原则是:

    BaseAdapter的代码如下:

    import android.content.Context;
    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;
    
    /**
     * Adapter的简单封装
     */
    public abstract class BaseAdapter<T, H extends BaseViewHolder> extends RecyclerView.Adapter<BaseViewHolder> {
    
        protected static final String TAG = BaseAdapter.class.getSimpleName();
    
        protected final Context context;
    
        protected final int layoutResId;
    
        protected List<T> datas;
    
        private OnItemClickListener mOnItemClickListener = null;
    
    
        public interface OnItemClickListener {
            void onItemClick(View view, int position);
        }
    
        public BaseAdapter(Context context, int layoutResId) {
            this(context, layoutResId, null);
        }
    
    
        public BaseAdapter(Context context, int layoutResId, List<T> datas) {
            this.datas = datas == null ? new ArrayList<T>() : datas;
            this.context = context;
            this.layoutResId = layoutResId;
        }
    
        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(layoutResId, viewGroup, false);
            BaseViewHolder vh = new BaseViewHolder(view, mOnItemClickListener);
            return vh;
        }
    
        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
            T item = getItem(position);
            convert((H) holder, item);
        }
    
        @Override
        public int getItemCount() {
            if (datas == null || datas.size() <= 0)
                return 0;
    
            return datas.size();
        }
    
        public T getItem(int position) {
            if (position >= datas.size()) return null;
            return datas.get(position);
        }
    
        /**
         * 清空数据
         */
        public void clearData() {
            int size = datas.size();
            datas.clear();
            notifyItemRangeRemoved(0, size);
        }
    
        /**
         * 下拉刷新,清除原有数据,添加新数据
         *
         * @param newData
         */
        public void refreshData(List<T> newData) {
            datas.clear();
            datas.addAll(newData);
            notifyItemRangeChanged(0, datas.size());
        }
    
        /**
         * 在原来数据的末尾追加新数据
         *
         * @param moreData
         */
        public void loadMoreData(List<T> moreData) {
            int lastPosition = datas.size();
            datas.addAll(lastPosition, moreData);
            notifyItemRangeInserted(lastPosition, moreData.size());
        }
    
        /**
         * Implement this method and use the helper to adapt the view to the given item.
         *
         * @param viewHoder A fully initialized helper.
         * @param item      The item that needs to be displayed.
         */
        protected abstract void convert(H viewHoder, T item);
    
    
        public void setOnItemClickListener(OnItemClickListener listener) {
            this.mOnItemClickListener = listener;
    
        }
    }
    

    BaseViewHolder的代码如下:

    import android.support.v7.widget.RecyclerView;
    import android.util.SparseArray;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    /**
     * 封装的ViewHolder
     */
    public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    
        //使用数组把条目中的View保存起来
        private SparseArray<View> views;
    
        private BaseAdapter.OnItemClickListener mOnItemClickListener;
    
        public BaseViewHolder(View itemView, BaseAdapter.OnItemClickListener onItemClickListener) {
            super(itemView);
            itemView.setOnClickListener(this);
    
            this.mOnItemClickListener = onItemClickListener;
            this.views = new SparseArray<View>();
        }
    
        public TextView getTextView(int viewId) {
            return retrieveView(viewId);
        }
    
        public Button getButton(int viewId) {
            return retrieveView(viewId);
        }
    
        public ImageView getImageView(int viewId) {
            return retrieveView(viewId);
        }
    
        public View getView(int viewId) {
            return retrieveView(viewId);
        }
    
    
        protected <T extends View> T retrieveView(int viewId) {
            View view = views.get(viewId);
            if (view == null) {
                view = itemView.findViewById(viewId);
                views.put(viewId, view);
            }
            return (T) view;
        }
    
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(v, getLayoutPosition());
            }
        }
    }
    

    为了使用方便,再提供一个SimpleAdapter:

    import android.content.Context;

    import java.util.List;

    /**
     * 一个简易的adapter
     */
    public abstract class SimpleAdapter<T> extends BaseAdapter<T, BaseViewHolder> {
    
        public SimpleAdapter(Context context, int layoutResId) {
            super(context, layoutResId);
        }
    
        public SimpleAdapter(Context context, int layoutResId, List<T> datas) {
            super(context, layoutResId, datas);
        }
    }
    

    二、封装Adapter的基本使用

    1、使用

    mAdapter = new SimpleAdapter<Page.Ware>(getActivity(), R.layout.item_hot_wares) {
        @Override
        protected void convert(BaseViewHolder holder, final Page.Ware item) {
    
            SimpleDraweeView s = (SimpleDraweeView) holder.getView(R.id.drawee_view);
            s.setImageURI(Uri.parse(item.getImgUrl()));
            holder.getTextView(R.id.text_title).setText(item.getName());
            holder.getTextView(R.id.text_price).setText(item.getPrice() + "");
            holder.getButton(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), item.getName(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    };
    

    2、还可以进行点击监听

    mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            
        }
    });
    

    如果觉得我的文字对你有所帮助的话,欢迎关注我的公众号:

    公众号:Android开发进阶

    我的群欢迎大家进来探讨各种技术与非技术的话题,有兴趣的朋友们加我私人微信huannan88,我拉你进群交(♂)流(♀)

    相关文章

      网友评论

        本文标题:10-Adapter的封装以及使用

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