Closed douglasalipio closed 5 years ago
I have found the problem. I should set sectionAdapter.addSection
before instantiate the section adapter `allFormRecycleView.adapter = sectionAdapter
How can I add a dynamic section since I can't addSection after instantiating the adapter? Am I missing something? `
@douglasalipio
From my experience, to avoid NPE issue, it is easier to add all Sections during onCreateView
(Doesn't matter whether they should be shown during the entire app life-cycle), and make all Sections as member variables of your Fragment
(or Activity
).
With such, you need not to concern, when I should instantiate a Section, and when I should nullify it. This greatly reduce the chance of being beaten by NPE.
Also, adding all Section
s at once, even they should be shown on screen, seem redundant. However, the way of SectionedRecyclerViewAdapter
handling Section
visibility is pretty efficient. The following code block is commonly seen in SectionedRecyclerViewAdapter
source code
// ignore invisible sections
if (!section.isVisible()) {
continue;
}
I would argue that the overhead is neglect-able.
In the middle of your app life-cycle, if you decide to show/ hide a Section
, simply use the following code snippet.
public static void showSection(SectionedRecyclerViewAdapter sectionedRecyclerViewAdapter, Section section) {
if (section.isVisible()) {
// Already visible.
return;
}
section.setVisible(true);
sectionedRecyclerViewAdapter.notifySectionChangedToVisible(section);
}
public static void hideSection(SectionedRecyclerViewAdapter sectionedRecyclerViewAdapter, Section section) {
if (false == section.isVisible()) {
// Already invisible.
return;
}
int previousSectionPosition = sectionedRecyclerViewAdapter.getSectionPosition(section);
section.setVisible(false);
sectionedRecyclerViewAdapter.notifySectionChangedToInvisible(section, previousSectionPosition);
}
The above is for case, where you doesn't use DiffUtil
. If you are using DiffUtil
, you need to
this.messageSection.setVisible(false);
NoteDiffUtilCallback noteDiffUtilCallback = new NoteDiffUtilCallback(
....
);
DiffUtil.calculateDiff(noteDiffUtilCallback).dispatchUpdatesTo(this.sectionedRecyclerViewAdapter);
Using DiffUtil
is a bit tricky as mentioned in https://github.com/luizgrp/SectionedRecyclerViewAdapter/issues/108 . However, once you get DiffUtil
done right, using SectionedRecyclerViewAdapter
is a lot easier. So far, I don't really have a generalized way, to develop DiffUtil
for SectionedRecyclerViewAdapter
. Some basic understanding on how SectionedRecyclerViewAdapter
work internally, is required to implement a correct DiffUtil
.
@douglasalipio
If you still insist to call addSection
/ removeSection
dynamically, you still can do so. However, you need to make sure you call it in UI thread, and also perform correct notify
on the adapter. If not, there will not be any add/ remove animation.
@luizgrp
I think currently, we don't have methods called notifySectionAdded
and notifySectionRemoved
to assist developer. Do you think we should add both?
Hey guys,
I'm trying to show only two section in my recycleView but the items in my section don't show. Just got a blank view. BTW I put a breakpoint on onBindItemViewHolder nothing happens. Here is the code.
As I'm using Conductor lib, I have the inicialization in onAttach.