Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bytebeats opened this issue Feb 17, 2016 · 6 comments

Comments

@bytebeats
Copy link

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?

@ma-jahn
Copy link

ma-jahn commented Mar 11, 2016

I am also having this issue.
Is there a quick fix for this?

@ma-jahn
Copy link

ma-jahn commented Mar 11, 2016

Just found this:
#26

@bytebeats
Copy link
Author

Did that solve this problem?

@ma-jahn
Copy link

ma-jahn commented Mar 11, 2016

Yes it did. onItemClick now returns the proper position for each element. The last element now really is the last element instead of the next Section Header.

The code provided doesn't work since the abstract class can not access getHeaderViewsCount.
But since we have a reference to adapterView we can simply cast it and access the method.

The code below is working for me:

`PinnedHeaderListView mPinnedHeaderListView = (PinnedHeaderListView) adapterView;

rawPosition = rawPosition - mPinnedHeaderListView.getHeaderViewsCount();

if(rawPosition<0)return;`

@bytebeats
Copy link
Author

Nice. I'll try later.

@bytebeats
Copy link
Author

Yes, as of #26 , PinnedHeaderListView returns the right position of section and item. But if I want to listen to click event from the pinned section view, what should I do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants