• [借鉴] Android简便通用的SimpleBaseAdapter


    public abstract class SimpleBaseAdapter<T> extends BaseAdapter {
    
        protected Context context;
        protected List<T> data;
    
        public SimpleBaseAdapter(Context context, List<T> data) {
            this.context = context;
            this.data = data == null ? new ArrayList<T>() : data;
        }
    
        @Override
        public int getCount() {
            return data.size();
        }
    
        @Override
        public Object getItem(int position) {
            if (position >= data.size())
                return null;
            return data.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        /**
         * 该方法需要子类实现,需要返回item布局的resource id
         * 
         * @return
         */
        public abstract int getItemResource();
    
        /**
         * 使用该getItemView方法替换原来的getView方法,需要子类实现
         * 
         * @param position
         * @param convertView
         * @param parent
         * @param holder
         * @return
         */
        public abstract View getItemView(int position, View convertView, ViewHolder holder);
    
        @SuppressWarnings("unchecked")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (null == convertView) {
                convertView = View.inflate(context, getItemResource(), null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            return getItemView(position, convertView, holder);
        }
    
        public class ViewHolder {
            private SparseArray<View> views = new SparseArray<View>();
            private View convertView;
            
            public ViewHolder(View convertView) {
                this.convertView = convertView;
            }
    
            @SuppressWarnings("unchecked")
            public <T extends View> T getView(int resId) {
                View v = views.get(resId);
                if (null == v) {
                    v = convertView.findViewById(resId);
                    views.put(resId, v);
                }
                return (T) v;
            }
        }
    
        public void addAll(List<T> elem) {
            data.addAll(elem);
            notifyDataSetChanged();
        }
        
        public void remove(T elem) {
            data.remove(elem);
            notifyDataSetChanged();
        }
    
        public void remove(int index) {
            data.remove(index);
            notifyDataSetChanged();
        }
    
        public void replaceAll(List<T> elem) {
            data.clear();
            data.addAll(elem);
            notifyDataSetChanged();
        }
    }
    public class TestFoodListAdapter extends SimpleBaseAdapter<String> {
    
        public TestFoodListAdapter(Context context, List<String> data) {
            super(context, data);
        }
    
        @Override
        public int getItemResource() {
            return R.layout.listitem_test;
        }
    
        @Override
        public View getItemView(int position, View convertView, ViewHolder holder) {
            TextView text = holder.getView(R.id.text);
            text.setText(getItem(position));
            return convertView;
        }
    }
  • 相关阅读:
    浅析七种经典排序算法
    一个可编辑与新增博客园文章的 Python 脚本
    快速排序的几种实现方式
    如何查找某个网站的(如:有道云笔记)的接口
    一键导出「有道云笔记」所有笔记
    2020年启蒙及小学识字练字APP或小程序测评榜
    2020年部编版小学二年级语文上册知识点(完整版)
    2020年部编人教版小学语文一年级下册知识点汇总
    换个角度,程序员爸爸应该关注一下
    计算机基础知识-I/O篇
  • 原文地址:https://www.cnblogs.com/qianyukun/p/5098006.html
Copyright © 2020-2023  润新知