码迷,mamicode.com
首页 > 其他好文 > 详细

listview相关代码整理

时间:2016-11-11 17:34:04      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:rac   pre   prot   out   布局文件   创建   settag   ati   dap   

虽然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;
        }
    }

}

 

listview相关代码整理

标签:rac   pre   prot   out   布局文件   创建   settag   ati   dap   

原文地址:http://www.cnblogs.com/checkway/p/6054966.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!