Skip to content

Positions for section and item are wrong when set PinnedHeaderListView.OnItemClickListener #36

@bytebeats

Description

@bytebeats

I tried to track positions for section and item, so I added codes below into your sample project:

        listView.setOnItemClickListener(new PinnedHeaderListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int section, int position, long id) {
                Toast.makeText(MainActivity.this, "onItemClick: Section: " + section + " Position: " + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSectionClick(AdapterView<?> adapterView, View view, int section, long id) {
                Toast.makeText(MainActivity.this, "onSectionClick: Section: " + section, Toast.LENGTH_SHORT).show();
            }
        });

But, all toasted messages imply the section and position are wrong. For example,

  • when the HEADER 1 is clicked, "onSectionClick: Section: 0" is toasted;
  • when the HEADER 2 is clicked, "onSectionClick: Section: 0 Position: 0" is toasted;
  • when the Header for section 0 is clicked, "onSectionClick: Section: 0 Position: 1" is toasted;
  • when the Section 0 Item 14 is clicked, "onSectionClick: Section: 1 Position: 0" is toasted.

So I reckoned codes

        @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();
            }
            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);
            }
        }

may be not accurate, which is from OnItemClickListener implements AdapterView.OnItemClickListener.

So could you fix it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions