美文网首页
封装ListView的Adapter--减少重复代码

封装ListView的Adapter--减少重复代码

作者: wenou | 来源:发表于2018-05-30 18:06 被阅读30次

    由于最近使用RecyclerView的一个的开源项目BaseRecyclerViewAdapterHelper,觉得挺好用的,功能很多,也不用写重复的代码了,用起来也很方便,简洁.

    简书介绍:
    简书地址 https://www.jianshu.com/p/b343fcff51b0

    但是有的时候还是会用到 ListView ,由于没有使用开源框架,以前也没记录一些敏捷的工具,所以还是要自己继承google的BaseAdapter, 然后写重复的getView, getItem,之类的,感觉非常不开心..

    所以就自己写一下,以后用到就复制粘贴,很爽的!!

    封装之后的使用效果:
    public class ListAdapter extends BaseListAdapter<TestBean> {
    
        public ListAdapter(Context context, List<TestBean> datas) {
            super(R.layout.item_adapter_test, context, datas);
        }
    
        @Override
        protected void convert(BaseListViewHolder viewHolder, TestBean bean, int position) {
            //设置内容和 点击监听
            viewHolder.setTest(R.id.item_test_tv, bean.getTitle())
                    .addOnClickListener(R.id.item_click_btn,position)
                    .addOnClickListener(R.id.item_test_tv, position);
        }
    
    }
    
    对比封装之前的:
    public class OldAdapter extends BaseAdapter {
        private Context mContext;
        private List<Object> mDatas;
        public OldAdapter (Context context, List<Object> datas){
            mContext = context;
            mDatas = datas;
        }
        @Override
        public int getCount() {
            return 0;
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if(convertView == null){
                convertView = LayoutInflater.from(加载布局);
                //省略代码
            }
            //省略代码
            return convertView;
        }
        static class ViewHolder{
            //省略代码
        }
    }
    

    重复的代码都封装在Base类里面隐藏起来了
    瞬间感觉爽了很多,以后再也不用搬砖了...(其实....还是要的)

    封装思路:
    提供给新人看的.大佬绕路哈!~~(好像也没人看...想多了.)

    第一步:
    创建一个BaseListAdapter作为基类,数据类型使用泛型,布局redId要实现类传入,然后把公共的代码写在基类

    public class BaseListAdapter<B> extends BaseAdapter {
    
        protected Context mContext;
        private List<B> mDatas;
        private LayoutInflater mInflater;
        private int mLayoutResId;
    
        public BaseListAdapter(@LayoutRes int layoutResId, Context context, List<B> datas){
            mContext = context;
            mDatas = datas;
            mLayoutResId = layoutResId;
            mInflater = LayoutInflater.from(context);
        }
    
        @Override
        public int getCount() {
            if(mDatas != null){
                return mDatas.size();
            }
            return 0;
        }
    
        @Override
        public B getItem(int position) {
            if(mDatas != null){
                return mDatas.get(position);
            }
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return null;
        }
    }
    

    第二步:
    处理好 getView() 方法

    按照以前的写法,都会为Adapter创建一个 ViewHolder 来记录
    itemView 里面的子View,所以这里就封装一个BaseListViewHolder ,通过构造,把整个ItemView保存到ViewHolder 里面

    public class BaseListViewHolder {
    
        private View convertView;
        private BaseListAdapter mAdapter;
    
        //缓存view, key是id
        private final SparseArray<View> mViews;
    
        public BaseListViewHolder(View itemView){
            mViews = new SparseArray<>();
            convertView = itemView;
        } 
    
        public BaseListViewHolder setAdapter(BaseListAdapter adapter) {
            mAdapter = adapter;
            return this;
        }
    }
    
    

    这个时候 getView() 就可以这样写了

     @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            BaseListViewHolder viewHolder;
            if(convertView == null){
                convertView = mInflater.inflate(mLayoutResId,parent,false);
                viewHolder = bindViewHolder(convertView);
                viewHolder.setAdapter(this);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (BaseListViewHolder) convertView.getTag();
            }
            convert(viewHolder,getItem(position),position);
            return convertView;
        }
        //抽象方法,子类实现
        protected abstract void convert(BaseListViewHolder viewHolder, B bean ,int position);
    
    

    这里差不多就完成整个Adapter的封装了,还有一些其他事情需要在 BaseListViewHolder 里面处理,比如我们需要 数据绑定,设置子View的背景,字体颜色,点击事件等等...

    完整的BaseListAdapter
    public abstract class BaseListAdapter<B> extends BaseAdapter {
    
        protected Context mContext;
        private List<B> mDatas;
        private LayoutInflater mInflater;
        private int mLayoutResId;
    
        public BaseListAdapter(@LayoutRes int layoutResId, Context context,List<B> datas){
            mContext = context;
            mDatas = datas;
            mLayoutResId = layoutResId;
            mInflater = LayoutInflater.from(context);
        }
    
        @Override
        public int getCount() {
            if(mDatas != null){
                return mDatas.size();
            }
            return 0;
        }
    
        @Override
        public B getItem(int position) {
            if(mDatas != null){
                return mDatas.get(position);
            }
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            BaseListViewHolder viewHolder;
            if(convertView == null){
                convertView = mInflater.inflate(mLayoutResId,parent,false);
                viewHolder = bindViewHolder(convertView);
                convertView.setTag(viewHolder);
                viewHolder.setAdapter(this);
            }else {
                viewHolder = (BaseListViewHolder) convertView.getTag();
            }
            convert(viewHolder,getItem(position),position);
            return convertView;
        }
    
        protected abstract void convert(BaseListViewHolder viewHolder, B bean ,int position);
    
        private BaseListViewHolder bindViewHolder(View convertView){
            return new BaseListViewHolder(convertView);
        }
    
        /**
         * 监听子View的点击事件
         */
        public void setOnItemChildClickListener(OnItemChildClickListener listener) {
            mOnItemChildClickListener = listener;
        }
    
        public OnItemChildClickListener getOnItemChildClickListener() {
            return mOnItemChildClickListener;
        }
    
        private OnItemChildClickListener mOnItemChildClickListener;
        public interface OnItemChildClickListener{
            void onItemChildClick(BaseListAdapter adapter, View view, int position);
        }
    }
    
    
    完整的BaseListViewHolder
    public class BaseListViewHolder {
    
        private View convertView;
        private BaseListAdapter mAdapter;
    
        //缓存view, key是id
        private final SparseArray<View> mViews;
    
        public BaseListViewHolder(View itemView){
            mViews = new SparseArray<>();
            convertView = itemView;
        }
    
        public View getConvertView() {
            return convertView;
        }
    
        public <T extends View> T getView(@IdRes int viewId) {
            View view = mViews.get(viewId);
            if (view == null) {
                view = convertView.findViewById(viewId);
                mViews.put(viewId, view);
            }
            return (T) view;
        }
    
        public BaseListViewHolder setTest(@IdRes int viewId ,CharSequence value) {
            TextView view = getView(viewId);
            view.setText(value);
            return this;
        }
    
        public BaseListViewHolder setImageResource(@IdRes int viewId, @DrawableRes int imageResId) {
            ImageView view = getView(viewId);
            view.setImageResource(imageResId);
            return this;
        }
    
        public BaseListViewHolder setBackgroundColor(@IdRes int viewId, @ColorInt int color) {
            View view = getView(viewId);
            view.setBackgroundColor(color);
            return this;
        }
    
        public BaseListViewHolder setTextColor(@IdRes int viewId, @ColorInt int textColor) {
            TextView view = getView(viewId);
            view.setTextColor(textColor);
            return this;
        }
    
        public BaseListViewHolder setVisible(@IdRes int viewId, boolean visible) {
            View view = getView(viewId);
            view.setVisibility(visible ? View.VISIBLE : View.GONE);
            return this;
        }
    
        public BaseListViewHolder addOnClickListener(@IdRes int viewId, final int position) {
            final View view = getView(viewId);
            if (view != null) {
                if (!view.isClickable()) {
                    view.setClickable(true);
                }
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mAdapter.getOnItemChildClickListener() != null) {
                            mAdapter.getOnItemChildClickListener().onItemChildClick(mAdapter, v, position);
                        }
                    }
                });
            }
            return this;
        }
    
    
        public BaseListViewHolder setAdapter(BaseListAdapter adapter) {
            mAdapter = adapter;
            return this;
        }
    }
    

    这里只实现一些基础的功能,如需添加,可以自己实现

    相关文章

      网友评论

          本文标题:封装ListView的Adapter--减少重复代码

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