Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView items change size after refresh

I have a RecyclerView as the following image presents:

RecyclerView

This is the setup code for this RecyclerView:

RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
dataRecyclerView.setLayoutManager(layoutManager);
dataRecyclerView.addItemDecoration(new GridSpacingItemDecoration(...);
dataRecyclerView.setItemAnimator(new DefaultItemAnimator());

after starting a new activity where I update the selected item of the RecyclerView, the RecyclerView is re-populated with new data to reflect the new change, but I get the following result:

Result 1

And when I scroll down, I get this gap:

Result 2

I tried calling RecyclerView#invalidate(), #invalidateItemDecorations(), #requestLayout() after the refresh but to no avail; & I'm also not calling setHasFixedSize(true) on the RecyclerView.

What is the problem exactly ? How can I resize the child items so as they wrap their content precisely after refreshing?

(P.S: I tried refreshing without making any changes & nothing happened, which proves my theory that the issue arises only when a child item's height changes.)

like image 406
Mohammed Aouf Zouag Avatar asked May 31 '16 13:05

Mohammed Aouf Zouag


1 Answers

That's quite late but I have also experienced this problem. This could be helpful for those still having the same bug.

In my case the solution was checking if there are any item decoration for recyclerview:

recyclerView.addItemDecoration(new SpacesItemDecoration(8));

This line was called everytime recyclerview is updated and was make recyclerview items getting smaller. Hope it helps the other.

Happy Coding, Baki

like image 132
Baki Kocak Avatar answered Nov 19 '22 19:11

Baki Kocak