JimiSmith / PinnedHeaderListView

A ListView with pinned section headers for Android
663 stars 306 forks source link

A little bug! if the listview have a headerview ,onItemClick give a wrong position value ! #26

Open zzhangxiaoyun opened 9 years ago

zzhangxiaoyun commented 9 years ago
public void setOnItemClickListener(final PinnedHeaderListView.OnItemClickListener listener) {
    super.setOnItemClickListener(new android.widget.ListView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int rawPosition, long id) {
            PinnedBaseAdapter adapter;
            if (adapterView.getAdapter().getClass().equals(HeaderViewListAdapter.class)) {
                HeaderViewListAdapter wrapperAdapter = (HeaderViewListAdapter) adapterView.getAdapter();
                adapter = (PinnedBaseAdapter) wrapperAdapter.getWrappedAdapter();
            } else {
                adapter = (PinnedBaseAdapter) adapterView.getAdapter();
            }
            rawPosition = rawPosition - getHeaderViewsCount();
            if (rawPosition < 0||rawPosition >= adapter.getCount())//if have headerViews or FooterViews They didn't click event
                return;
            int section = adapter.getSectionForPosition(rawPosition);
            int position = adapter.getPositionInSectionForPosition(rawPosition);

            if (position == -1) {
                listener.onSectionClick(adapterView, view, section, id);
            } else {
                listener.onItemClick(adapterView, view, section, position, id);
            }
        }
    });
}
ma-jahn commented 8 years ago

You saved my day. Thanks!