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

List加入headView后点击position错乱问题

时间:2015-05-04 22:16:00      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:listview

技术分享

1.问题描述

当ListView中加入了headerView或者footerView之后,调用ListView的OnItemClick监听事件之后,获取到的position不再是
我们期望的值,比如,当我点击的是第一行,结果它显示的是第二行的position。

2.问题分析

从ListView的源码中可以得到我们想要的答案:
1.addHeaderView(View v, Object data, boolean isSelectable):
/**
     * Add a fixed view to appear at the top of the list. If addHeaderView is
     * called more than once, the views will appear in the order they were
     * added. Views added using this call can take focus if they want.
     * <p>
     * NOTE: Call this before calling setAdapter. This is so ListView can wrap
     * the supplied cursor with one that will also account for header and footer
     * views.
     *
     * @param v The view to add.
     * @param data Data to associate with this view
     * @param isSelectable whether the item is selectable
     */
    public void addHeaderView(View v, Object data, boolean isSelectable) {

        if (mAdapter != null && ! (mAdapter instanceof HeaderViewListAdapter)) {
            throw new IllegalStateException(
                    "Cannot add header view to list -- setAdapter has already been called.");
        }

        FixedViewInfo info = new FixedViewInfo();
        info.view = v;
        info.data = data;
        info.isSelectable = isSelectable;
        mHeaderViewInfos.add(info);
        // in the case of re-adding a header view, or adding one later on,
        // we need to notify the observer
        if (mAdapter != null && mDataSetObserver != null) {
            mDataSetObserver.onChanged();
        }
    }
2.setAdapter(ListAdapter adapter):
 /**
     * Sets the data behind this ListView.
     *
     * The adapter passed to this method may be wrapped by a {@link WrapperListAdapter},
     * depending on the ListView features currently in use. For instance, adding
     * headers and/or footers will cause the adapter to be wrapped.
     *
     * @param adapter The ListAdapter which is responsible for maintaining the
     *        data backing this list and for producing a view to represent an
     *        item in that data set.
     *
     * @see #getAdapter() 
     */
    @Override
    public void setAdapter(ListAdapter adapter) {
        if (mAdapter != null && mDataSetObserver != null) {
            mAdapter.unregisterDataSetObserver(mDataSetObserver);
        }

        resetList();
        mRecycler.clear();

        if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0) {
            mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, adapter);
        } else {
            mAdapter = adapter;
        }

        .....
    }

3.问题解决

1.从源码中可以分析得到,addHeaderView(View v, Object data, boolean isSelectable)方法必须要在setAdapter之前调用,而
addFooterView(View v, Object data, boolean isSelectable)方法不需要必须在setAdapter之前调用。

2.当有headView的时候,就不能再用adapter.getItem(position),而需要通过parent获取到adapter,再进行操作,parent.getAdapter().getItem(position)
这样获取到的Item,就是我们想要的结果了。

List加入headView后点击position错乱问题

标签:listview

原文地址:http://blog.csdn.net/u011771755/article/details/45485069

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