首页 > 代码库 > listview相关代码整理

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 =http://www.mamicode.com/= null || mData.size() > position;    }    protected final Object mLocker = new Object();    protected List<D> mData = http://www.mamicode.com/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相关代码整理