PinnedHeaderListView icon indicating copy to clipboard operation
PinnedHeaderListView copied to clipboard

Wrong section & position in onSectionClick/onItemClick

Open michalciolek opened this issue 12 years ago • 1 comments

When I add a header to the ListView by code e.g: PinnedHeaderListView listView = (PinnedHeaderListView) view.findViewById(R.id.pinnedListView); LinearLayout header1 = (LinearLayout) inflater.inflate(R.layout.menu_left_header_profile, null); listView.addHeaderView(header1);

the position / section in onSectionClick/onItemClick is invalid....

In PinnedHeaderListView.OnItemClickListener you do not take into account the numbers of headers added to the ListView

rawPosition in public void onItemClick(AdapterView<?> adapterView, View view, int rawPosition, long id) contains the number of rows and headers.

I fix it by add line:

rawPosition -= ((ListView) adapterView).getHeaderViewsCount();

in:

public static abstract class OnItemClickListener implements AdapterView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int rawPosition, long id) {
        SectionedBaseAdapter adapter;
        if (adapterView.getAdapter().getClass().equals(HeaderViewListAdapter.class)) {
            HeaderViewListAdapter wrapperAdapter = (HeaderViewListAdapter) adapterView.getAdapter();
            adapter = (SectionedBaseAdapter) wrapperAdapter.getWrappedAdapter();
        } else {
            adapter = (SectionedBaseAdapter) adapterView.getAdapter();
        }

        rawPosition -= ((ListView) adapterView).getHeaderViewsCount();

        int section = adapter.getSectionForPosition(rawPosition);
        int position = adapter.getPositionInSectionForPosition(rawPosition);

        if (position == -1) {
            onSectionClick(adapterView, view, section, id);
        } else {
            onItemClick(adapterView, view, section, position, id);
        }
    }

    public abstract void onItemClick(AdapterView<?> adapterView, View view, int section, int position, long id);

    public abstract void onSectionClick(AdapterView<?> adapterView, View view, int section, long id);

}

michalciolek avatar May 20 '13 20:05 michalciolek

Thank you very much

superbool avatar Apr 27 '15 03:04 superbool