Closed yccheok closed 5 years ago
I made a pull request - https://github.com/luizgrp/SectionedRecyclerViewAdapter/pull/137
Thank you.
Just for your reference, this is our final use case in production.
import io.github.luizgrp.sectionedrecyclerviewadapter.Section;
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionedRecyclerViewAdapter;
import static android.support.v7.widget.RecyclerView.NO_ID;
// https://stackoverflow.com/questions/33851370/applying-statelistanimator-in-recylerviews-item-will-cause-flickering-effect-wh
public class StableIdSectionedRecyclerViewAdapter extends SectionedRecyclerViewAdapter {
private static final long ID_FOR_MESSAGE_SECTION = Long.MAX_VALUE;
private static final long BASED_ID_FOR_HEADER = Long.MAX_VALUE - 1;
public StableIdSectionedRecyclerViewAdapter() {
this.setHasStableIds(true);
}
@Override
public long getItemId(int position) {
Section section = this.getSectionForPosition(position);
if (SectionedRecyclerViewAdapter.VIEW_TYPE_ITEM_LOADED != this.getSectionItemViewType(position)) {
// We can have multiple headers.
return BASED_ID_FOR_HEADER - indexOf(section);
}
if (section instanceof NoteSection) {
int positionInSection = this.getPositionInSection(position);
return ((NoteSection) section).getNotes().get(positionInSection).getPlainNote().getId();
} else if (section instanceof MessageSection) {
// So far, we only have max 1 MessageSection per adapter.
return ID_FOR_MESSAGE_SECTION;
}
return NO_ID;
}
}
Is your feature request related to a problem? Please describe. Currently, in
SectionedRecyclerViewAdapter
, if I want to have stable Id behavior (So that I can achieve animation without flickering - https://stackoverflow.com/questions/33851370/applying-statelistanimator-in-recylerviews-item-will-cause-flickering-effect-wh), here's my current code.Consider the following case
If you refer to my above code, we can return correct Id for Loaded item for NoteSection (which is based on the primary key of SQLite). But, we aren't able to return correct Id for Header for NoteSection
If we have
indexOf(Section section)
inSectionedRecyclerViewAdapter
, we can write the code asDescribe the solution you'd like Add function
indexOf(Section section)
inSectionedRecyclerViewAdapter
However, there's one thing I isn't sure. Should I consider the visibility of Section during index calculation? Or, visibility consideration (on/off) will become method parameter of
indexOf
.Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.
Additional context Add any other context or screenshots about the feature request here.