Wrong View Size In Decoration
If you follow this note, you will learn that we can set the in cell space using decoration. If we want to dynamically change the space based on the view’s (cell) size, then we will need to get the view.height property from the * getItemOffsets* function. However, I sometimes get a 0 when I visit view.height .
I saw the same problem in stackoverflow. So here is what I learned.
Solution
Write a fix layout class. You can probably copy and paste this class into your decorator.
1 | /** |
Now call this function once before you get the width/height from the view.
1 | override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) { |
Note that if you don’t notice any number that is wrong, but just some times have a 0 on height, call fixLayoutSize(view, parent) under if (view.height == 0) to speed up process.
More Readings
How to: Android RecyclerView Item Decorations. This could be a very decent reading to understanding this problem more.
