PinnedHeaderListView icon indicating copy to clipboard operation
PinnedHeaderListView copied to clipboard

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

Open bytebeats opened this issue 9 years ago • 6 comments

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?

bytebeats avatar Feb 17 '16 08:02 bytebeats

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

ma-jahn avatar Mar 11 '16 08:03 ma-jahn

Just found this: https://github.com/JimiSmith/PinnedHeaderListView/issues/26

ma-jahn avatar Mar 11 '16 08:03 ma-jahn

Did that solve this problem?

bytebeats avatar Mar 11 '16 08:03 bytebeats

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;`

ma-jahn avatar Mar 11 '16 08:03 ma-jahn

Nice. I'll try later.

bytebeats avatar Mar 11 '16 08:03 bytebeats

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?

bytebeats avatar Mar 29 '16 07:03 bytebeats