ShamylZakariya / StickyHeaders

Adapter and LayoutManager for Android RecyclerView which enables sticky header positioning.
MIT License
1.4k stars 185 forks source link

Get position in section index does not update #66

Open DS-Dj opened 7 years ago

DS-Dj commented 7 years ago

I have a list view which I want to remove items from when the user interacts.

The code I am running is like this:

dismiss.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int sectionIndex = getSection();
        int itemIndex = getPositionInSection();

        items.get(getKeyForIndex(sectionIndex)).remove(itemIndex);

        notifySectionItemRemoved(sectionIndex, itemIndex);
        if (items.get(getKeyForIndex(sectionIndex)).size() == 0) {
            items.remove(sectionIndex);
            notifySectionRemoved(sectionIndex);
        }
    }
});

The problem I am having is getPositionInSection() does not return the correct index after removing items from the list, it seems that the value returns is set when the view holder is bound and never changed. Meaning on successive clicks on different list items causes indexOutOfBounds exception. Any help would be appreciated.

DS-Dj commented 7 years ago

Managed to find a work around, but still has crashes when a section has been removed and removing the last item in the list. For anyone who is stuck try this code

int i = getAdapterPosition();
int sectionIndex = getSectionForAdapterPosition(i);
int itemIndex = getPositionOfItemInSection(sectionIndex, i);
pazbo commented 7 years ago

Hey, Are you going to fix this issue?