corochann
corochann

Reputation: 1624

Set margins between items on BrowseFragment

Developing an application for Android TV, I faced a problem for the usage of BrowseFragment provided by Leanback support library. Can we change the margin between items of RowsFragment?

Upvotes: 13

Views: 5509

Answers (5)

Tarun Anchala
Tarun Anchala

Reputation: 2712

In Presenter class onCreateViewHolder method,we can set parameters using setItemSpacing,setPadding e.t.c

class TVPresenter() : Presenter() {

override fun onCreateViewHolder(parent: ViewGroup): ViewHolder {
     parent.findViewById<HorizontalGridView>(R.id.row_content).run {
                setItemSpacing(10)
                setPadding(40,10,40,10)
            }
     val cardViewLayout = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_layout, parent, false)
     return ViewHolder(cardViewLayout)
}

Upvotes: 0

ILLIA N.
ILLIA N.

Reputation: 107

I had a case when there was a need to implement an endless scroll in ListRow. Also, I needed to add extra space after each last item. So I solved this issue by adding custom presenter to adapter's presenterSelector:

class EmptySpacePresenter : Presenter() {

override fun onCreateViewHolder(parent: ViewGroup): ViewHolder {
    val emptySpaceView = FrameLayout(parent.context).apply {
        val width = context.resources.getDimension(R.dimen.gallery_carousel_horizontal_gap).toInt()
        layoutParams = FrameLayout.LayoutParams(width, FrameLayout.LayoutParams.MATCH_PARENT)
        isClickable = false
        isFocusable = false
        isFocusableInTouchMode = false
    }

    return ViewHolder(emptySpaceView)
}

override fun onBindViewHolder(viewHolder: ViewHolder?, item: Any?) = Unit

override fun onUnbindViewHolder(viewHolder: ViewHolder?) = Unit

}

class MyObjectAdapter() : ArrayObjectAdapter() {

    init {
        presenterSelector = ClassPresenterSelector()
            .addClassPresenter(ListItem::class.java, ListItemPresenter())
            .addClassPresenter(EmptySpace::class.java, EmptySpacePresenter())
    }
}

Upvotes: 0

gfunk
gfunk

Reputation: 381

Ideally override the style details as @Billy answered. It can unfortunately take a while to figure out whats what. If what you are looking for is not styleable you can apparently override the built-in leanback resources (not sure if reliable/safe though). eg:

<dimen name="lb_browse_rows_margin_top">167dp</dimen>
<dimen name="lb_browse_item_vertical_spacing">8dp</dimen>
<dimen name="lb_browse_expanded_row_no_hovercard_bottom_padding">28dp</dimen>

Upvotes: 2

Billy
Billy

Reputation: 1374

You can override some styles to achieve this, for example:

<style name="AppTheme.Leanback" parent="Theme.Leanback">
  ..
  <item name="rowHorizontalGridStyle">@style/TvHorizontalGridView</item>
  ..
</style>

<style name="TvHorizontalGridView" parent="Widget.Leanback.Row.HorizontalGridView">
    <item name="horizontalMargin">@dimen/margin_medium</item>
    <item name="verticalMargin">@dimen/margin_medium</item>
</style>

where @dimen/margin_medium is the size of the margin that you want.

Upvotes: 12

corochann
corochann

Reputation: 1624

enter image description here

I found the answer by myself. The key was HorizontalGridView which stores the each rows item list. We can get the reference of this HorizontalGridView by R.id.row_content. Finally, setItemMargin method was the solution for this. Below is a sample code, and I could get top image.

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
    HorizontalGridView horizontalGridView = (HorizontalGridView) parent.findViewById(R.id.row_content);
    horizontalGridView.setItemMargin(400); // You can set item margin here.

    ...

    };

Upvotes: 14

Related Questions