PinnedHeaderListView
PinnedHeaderListView copied to clipboard
Positions for section and item are wrong when set PinnedHeaderListView.OnItemClickListener
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?
I am also having this issue. Is there a quick fix for this?
Just found this: https://github.com/JimiSmith/PinnedHeaderListView/issues/26
Did that solve this problem?
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;`
Nice. I'll try later.
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?