2

I have a recyclerView

<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

and I load items asynchronously using AsyncLayoutInflater as explained in this answer The problem is that my recyclerview has wrap_content height and it's not aware of items height before they are loaded and it takes arbitrary height sometimes. I tried to hardcode item height but it's not a good solution. I can also try to calculate the hight of the view in runtime but I'm afraid I'll lose in performance and async loading will be useless. Is there a better way to let recyclerView what height should it be when all the items are loaded?

Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • I'm finding it hard to understand what you're trying to achieve with this setup. Can you further explain why you would want to use `wrap_content`? What is the orientation, placement, and interaction this RecyclerView is supposed to fit within your layout? If you have an image of your desired layout/effect, I would suggest editing your post to include it. – Sammy T Jan 09 '21 at 18:10
  • The problem is that when I add an item to the recyclerview asynchronously I add an empty frame layout with zero height as a placeholder until tge real item is loaded. Therefore recyclerview doesn't know which height it should take. I'm looking for a way how to tell recyclerview what height should it be when all items are loaded. It should wrap content according to my requirements. – Rainmaker Jan 10 '21 at 02:37
  • Yes, this is what doesn't make sense to me. A vertically oriented RecyclerView, for example, typically has its height defined according to the space of the Activity/Fragment layout instead of by its item contents. Additionally, adding an empty view with no visual impact as a placeholder sounds like unnecessary logic. – Sammy T Jan 10 '21 at 04:30
  • Usually recyclerview has height defined according to space of fragment -yes. But it's not my case. Have recyclerview inside cardview which is defined by height of recyclerview items. – Rainmaker Jan 10 '21 at 05:46
  • I add empty view just as a placeholder. When the real is loaded asynchronously I add it as a child to that placeholder. That's the way how it works when you load items asynchronously to recyclerview. – Rainmaker Jan 10 '21 at 05:48
  • You can read the post from stackoverflow that I mentioned in the question for more info – Rainmaker Jan 10 '21 at 05:50

0 Answers0