How to customize Leanback's ListRows?
Asked Answered
D

1

6

Android's leanback library provides a few standard ways to customize a RowFragment, both the Rows and the Cards themselves, but I'm not sure how to add custom elements outside of what is provided on the framework.

What I am specifically trying to achieve is to add a FooterItem to a row, similar to how a each Row has a a HeaderItem.

I'd like to get the existing functionality in ListRowView / ListRowPresenter, but I'm not sure how to do it "properly".

Decrepitate answered 16/2, 2017 at 19:39 Comment(0)
N
8

As with almost all things with Leanback, there's more than one way to skin the cat.

We needed to customize the ListRow to add more padding to the header. We've achieved this level of customization by overriding onBindRowViewHolder of ListRowPresenter, grabbing the View for the header (via View header = holder.getHeaderViewHolder().view;). And then we changed the height of it with header.getLayoutParams().height = rowItem.getRowHeaderHeight();.

With this same technique, you could dynamically add your footer view to holder.view (with addView).

Another approach would be following the what the documentation for RowPresenter recommends. The relevant quote is:

When a subclass of RowPresenter adds UI widgets, it should subclass RowPresenter.ViewHolder and override createRowViewHolder(ViewGroup) and initializeRowViewHolder(ViewHolder). The subclass must use layout id "row_content" for the widget that will be aligned to the title of any HeadersFragment that may exist in the parent fragment.

So with this new recommended approach, you'll basically be supplying your own xml. I believe you can even subclass ListRowPresenter. To see how the code works, look at ListRowView to see that it inflates the xml for R.layout.lb_list_row. And then it just uses the view for R.id.row_content.

Then that'll be the actual view that's supplied by holder.view and then you can set text on it and do whatever you want.

Let me know if you need any further clarification.

Natator answered 17/2, 2017 at 15:52 Comment(3)
I ended up extending ListRowPresenter, and overriding the onBindViewHolder, though I had to add a check to see if my extra views had already been added or not (since for some reason onCreateView is marked as final). I really wish I could do it onCreate so its for sure only done once and not checked every time, but I guess this is fine. As for customizing the Header, do you know if there's a way to do this by extending the leanback theme, override some values, and setting that theme to your activity?Decrepitate
This actually works, I did exactly what both of you suggested and it worked.Boutin
can you please show me some code you did in createRowViewHolder(ViewGroup)Giddy

© 2022 - 2024 — McMap. All rights reserved.