zoukankan      html  css  js  c++  java
  • listview相关代码整理

    虽然listview已经慢慢被替代了,  不过还是整理下 , 留作纪念吧



     /**
         * 获取 listview 实际滚动的距离. [ 相对于listview的第一个项目左上角.]
         *
         * @return
         */
        public static synchronized int getScrollY(AbsListView listView) {
            if (null == listView)
                return 0;
    
            View c = listView.getChildAt(0);
            if (c == null) {
                return 0;
            }
    
            int firstVisiblePosition = listView.getFirstVisiblePosition();
            int top = c.getTop();
    
            int mMax = 0;
            for (int i = 0; i < firstVisiblePosition; i++) {
                View v = listView.getChildAt(i);
                if (null != v) {
                    mMax += v.getHeight();
                }
            }
    
            return (-top + mMax);
        }

    一个封装过的通用adapter

    public abstract class BaseAdapterEx<D, VH extends BaseAdapterEx.ViewHolder> extends BaseAdapter {
    
        protected int[] mResIds = null;
    
        protected Context mContext;
    
        protected LayoutInflater mInflater;
    
        /**
         * 支持创建多种格式的布局文件. 默认获取资源对应方式为: resIds[ position % resIds.length ]
         *
         * @param context
         * @param resIds
         */
        public BaseAdapterEx(Context context, int... resIds) {
            this.mContext = context;
            this.mResIds = resIds;
    
            mInflater = LayoutInflater.from(mContext);
        }
    
        @Override
        public boolean isEnabled(int position) {
            return mData == null || mData.size() > position;
        }
    
        protected final Object mLocker = new Object();
    
        protected List<D> mData = new ArrayList<D>();
    
        public void clear() {
            synchronized (mLocker) {
                if (!mData.isEmpty()) {
                    mData.clear();
                    notifyDataSetChanged();
                }
            }
        }
    
        public List<D> getData() {
            return mData;
        }
    
        public void setData(Collection<D> data) {
            synchronized (mLocker) {
                mData.clear();
                mData.addAll(data);
                notifyDataSetChanged();
            }
        }
    
        public void setData(D[] data) {
            setData(Arrays.asList(data));
        }
    
        public D getItem(int position) {
            if (position >= 0 && mData != null && position < mData.size()) {
                return mData.get(position);
            }
            return null;
        }
    
        @Override
        public int getCount() {
            return (null == mData ? 0 : mData.size());
        }
    
        @Override
        public boolean areAllItemsEnabled() {
            return super.areAllItemsEnabled();
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            VH holder = null;
    
            if (null == convertView) {
                convertView = onCreateView(position, parent);
    
                holder = onCreateViewHolder(position, convertView);
                if (null != convertView) {
                    convertView.setTag(holder);
                }
    
            } else {
                holder = (VH) convertView.getTag();
            }
    
            onBindViewHolder(position, holder);
    
            return convertView;
        }
    
        /**
         * bind data to the view.
         *
         * @param position
         * @param holder
         */
        protected abstract void onBindViewHolder(int position, VH holder);
    
        /**
         * create ViewHolder, never return null.
         *
         * @param position
         * @param convertView
         * @return
         */
        protected abstract VH onCreateViewHolder(int position, View convertView);
    
    
        protected View onCreateView(int position, ViewGroup parent) {
    
            int resId = getResId(position);
    
            if (0 != resId) {
                return mInflater.inflate(resId, parent, false);
            }
            return null;
        }
    
        /**
         * 获取具体的资源文件ID.
         *
         * @param position
         * @return
         */
        protected int getResId(int position) {
            if (null != mResIds && mResIds.length > 0) {
                return mResIds[position % mResIds.length];
            }
    
            return 0;
        }
    
        public static class ViewHolder {
            public View itemView;
    
            public ViewHolder() {
            }
    
            public ViewHolder(View itemView) {
                this.itemView = itemView;
            }
        }
    
    }
    作者:闵天
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    Hadoop学习---Zookeeper+Hbase配置学习
    Hadoop学习---Hadoop的HBase的学习
    Hadoop学习---Hadoop的MapReduce的原理
    Hadoop学习---Hadoop的深入学习
    Hadoop学习---Eclipse中hadoop环境的搭建
    Hadoop学习---CentOS中hadoop伪分布式集群安装
    Hadoop学习---Ubuntu中hadoop完全分布式安装教程
    大数据学习---大数据的学习【all】
    Java实例---flappy-bird实例解析
    UML类图详细介绍
  • 原文地址:https://www.cnblogs.com/checkway/p/6054966.html
Copyright © 2011-2022 走看看