• Android开发之自定义万能BaseAdapter


    【本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究。若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!】

    话不多说哦,直接上模板:

    package com.zyzpp.adapter;
    
    import android.content.Context;
    import android.util.SparseArray;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    public abstract class MyAdapter<T> extends BaseAdapter {
    
        private ArrayList<T> mData;
        private int mLayoutRes;           //布局id
    
    
        public MyAdapter() {
        }
    
        public MyAdapter(ArrayList<T> mData, int mLayoutRes) {
            this.mData = mData;
            this.mLayoutRes = mLayoutRes;
        }
    
        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }
    
        @Override
        public T getItem(int position) {
            return mData.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = ViewHolder.bind(parent.getContext(), convertView, parent, mLayoutRes
                    , position);
            bindView(holder, getItem(position));
            return holder.getItemView();
        }
    
        public abstract void bindView(ViewHolder holder, T obj);
    
        //添加一个元素
        public void add(T data) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            mData.add(data);
            notifyDataSetChanged();
        }
    
        //往特定位置,添加一个元素
        public void add(int position, T data) {
            if (mData == null) {
                mData = new ArrayList<>();
            }
            mData.add(position, data);
            notifyDataSetChanged();
        }
    
        public void remove(T data) {
            if (mData != null) {
                mData.remove(data);
            }
            notifyDataSetChanged();
        }
    
        public void remove(int position) {
            if (mData != null) {
                mData.remove(position);
            }
            notifyDataSetChanged();
        }
    
        public void clear() {
            if (mData != null) {
                mData.clear();
            }
            notifyDataSetChanged();
        }
    
    
        public static class ViewHolder {
    
            private SparseArray<View> mViews;   //存储ListView 的 item中的View
            private View item;                  //存放convertView
            private int position;               //游标
            private Context context;            //Context上下文
    
            //构造方法,完成相关初始化
            private ViewHolder(Context context, ViewGroup parent, int layoutRes) {
                mViews = new SparseArray<>();
                this.context = context;
                View convertView = LayoutInflater.from(context).inflate(layoutRes, parent, false);
                convertView.setTag(this);
                item = convertView;
            }
    
            //绑定ViewHolder与item
            public static ViewHolder bind(Context context, View convertView, ViewGroup parent,
                                          int layoutRes, int position) {
                ViewHolder holder;
                if (convertView == null) {
                    holder = new ViewHolder(context, parent, layoutRes);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                    holder.item = convertView;
                }
                holder.position = position;
                return holder;
            }
    
            @SuppressWarnings("unchecked")
            public <T extends View> T getView(int id) {
                T t = (T) mViews.get(id);
                if (t == null) {
                    t = (T) item.findViewById(id);
                    mViews.put(id, t);
                }
                return t;
            }
    
    
            /**
             * 获取当前条目
             */
            public View getItemView() {
                return item;
            }
    
            /**
             * 获取条目位置
             */
            public int getItemPosition() {
                return position;
            }
    
            /**
             * 设置文字
             */
            public ViewHolder setText(int id, CharSequence text) {
                View view = getView(id);
                if (view instanceof TextView) {
                    ((TextView) view).setText(text);
                }
                return this;
            }
    
            /**
             * 设置图片
             */
            public ViewHolder setImageResource(int id, int drawableRes) {
                View view = getView(id);
                if (view instanceof ImageView) {
                    ((ImageView) view).setImageResource(drawableRes);
                } else {
                    view.setBackgroundResource(drawableRes);
                }
                return this;
            }
    
    
            /**
             * 设置点击监听
             */
            public ViewHolder setOnClickListener(int id, View.OnClickListener listener) {
                getView(id).setOnClickListener(listener);
                return this;
            }
    
            /**
             * 设置可见
             */
            public ViewHolder setVisibility(int id, int visible) {
                getView(id).setVisibility(visible);
                return this;
            }
    
            /**
             * 设置标签
             */
            public ViewHolder setTag(int id, Object obj) {
                getView(id).setTag(obj);
                return this;
            }
            /**
             * 设置文字and颜色
             */
            public ViewHolder setTextAndColor(int id, CharSequence text,int color) {
                View view = getView(id);
                if (view instanceof TextView) {
                    ((TextView) view).setText(text);
                    ((TextView) view).setTextColor(color);
                }
                return this;
            }
    
            //其他方法可自行扩展
    
        }
    
    }
    

    下面说一下ListView(GridView)的使用流程:

    private void useListView() {
            //绑定View
            listview = (ListView) view.findViewById(R.id.grid_ico);
            //添加头部View
            listview.addHeaderView(headview);
            //添加数据
            mData = new ArrayList<>();
            mData.add(new ListItem(R.mipmap.dot, "这是自定义JavaBean"));
            //添加数据源
            mAdapter = new MyAdapter<ListItem>(mData, R.layout.item_grid_icon) {
                @Override
                public void bindView(ViewHolder holder, ListItem obj) {
                    holder.setImageResource(R.id.img_icon, obj.getId());
                    holder.setText(R.id.txt_js, obj.getContent());
                }
            };
            //把数据源set进视图
            listview.setAdapter(mAdapter);
            //监听点击事件
            listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(getActivity(), "你点击了~" + position + "~项", Toast.LENGTH_SHORT).show();
                    switch (position) {
                        case 1:
                            break;
                    }
                }
            });
            //监听长按事件
            listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(getActivity(), "你长按了第~" + position + "~项", Toast.LENGTH_SHORT).show();
       return true; //默认不处理返回false 
      } 
     });
     }
    

    版权声明

    【本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究。若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!】

  • 相关阅读:
    spring源码下载
    java资料共享
    《Linux命令行与shell脚本编程大全》读书笔记
    《微服务设计》读书笔记
    tornado关于AsyncHTTPClient的使用笔记
    java stackoverflowerror与outofmemoryerror区别(转)
    JAVA关于泛型的笔记
    在JAVA中返回类型使用泛型T和Object有什么区别?
    《构架师的12项修炼》读书笔记
    关于控制台输入的代码怎么跑单元测试的问题记录
  • 原文地址:https://www.cnblogs.com/onblog/p/13036374.html
Copyright © 2020-2023  润新知