I have this kind of problem. I have a simple app for wallpapers.
I am getting wallpapers from API
, there is a Tab
called Category
in which I have Sections
I would like to hide few of that Sections
inside Category
Tab
.
In the best scenario my backend developer have to hide or remove it from backend but as he is not available now. I have decided to do it myself.
So, in RecyclerView
for Category
Tab
in onBindViewHolder
I am checking if the title is equal the one that I want to be deleted I am calling removeAt
(you can see that part below):
if (categoryTitle.equals("Hot")) {
list.removeAt(holder.layoutPosition)
}
I need this to be called whenever the Category
Tab
shows so the user will not be able to see that Category
Section
. But it doesn't update it because I have to call this two methods as well:
if (categoryTitle.equals("Hot")) {
list.removeAt(holder.layoutPosition)
notifyItemRemoved(holder.layoutPosition)
notifyItemRangeChanged(holder.layoutPosition, list.size)
}
but while call them I got en error saying
"Cannot call this method while RecyclerView is computing a layout or scrolling . . ."
Here is the question. Where I have to call it to avoid the error ?
(link of my app: https://play.google.com/store/apps/details?id=com.backpaper)
(please do not suggest to use timer or delay or something similar (-_-)
)
Update:
override fun onResponse(categories: Categories) {
categories.categories!!.removeAt(5)
categories.categories!!.removeAt(1)
adapter = RecyclerViewAdapterC(categories.categories!!)}
Do not remove item in onBindViewHolder()
It will throw exception because item is not added yet . Exception will be as.
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
You should remove the item from dataset before adding them to adapter .
if (!categoryTitle.equals("Hot")) {
// Add item else do not add item
}
After filtering the list dataset then set adapter with filtered list .
below is an example .
ArrayList<String> list=new ArrayList();
for(String cat : categories.categories){
if (!categoryTitle.equals("Hot")) {
list.add(cat);
}
}
adapter = RecyclerViewAdapterC(list)