Skip to content

Layout Recipes

Rows

A Row is simply a DpadRecyclerView with RecyclerView.HORIZONTAL as its orientation. Do the following either in XML or Kotlin:

1
2
3
4
5
<com.rubensousa.dpadrecyclerview.DpadRecyclerView 
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" />
recyclerView.setOrientation(RecyclerView.HORIZONTAL)

Note

To center views vertically inside a horizontal DpadRecyclerView, you can use the gravity attribute like so:

1
2
3
4
5
6
<com.rubensousa.dpadrecyclerview.DpadRecyclerView 
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="260dp"
    android:orientation="horizontal"
    android:gravity="center" />

Columns

A Column is simply a DpadRecyclerView with RecyclerView.VERTICAL as its orientation. Do the following either in XML or Kotlin:

1
2
3
4
5
<com.rubensousa.dpadrecyclerview.DpadRecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" />
recyclerView.setOrientation(RecyclerView.VERTICAL)

Grids

The API is similar to the one of GridLayoutManager from androidx.recyclerview:

1
2
3
4
5
<com.rubensousa.dpadrecyclerview.DpadRecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:spanCount="5" />
recyclerView.setSpanCount(5)

Different span sizes

To customise the size of each span, use DpadSpanSizeLookup.

This example would create a full size header for the item at the first position:

1
2
3
4
5
6
7
8
9
recyclerView.setSpanSizeLookup(object : DpadSpanSizeLookup() {
    override fun getSpanSize(position: Int): Int {
        return if (position == 0) {
            recyclerView.getSpanCount()
        } else {
            1
        }
    }
})

Looping adapter contents

You can enable infinite scrolling by using setLoopDirection to loop the adapter contents:

1
2
3
4
5
// This will loop when scrolling towards both the start and end edges
recyclerView.setLoopDirection(DpadLoopDirection.MIN_MAX)

// This will loop only when scrolling towards the end
recyclerView.setLoopDirection(DpadLoopDirection.MAX)

Note

Looping is only supported when there's enough items to fill the viewport

Extra layout space

DpadRecyclerView won't layout any extra space by default, however, you might want to create extra views in case you're aligning items to an edge.

The example below would create half a page of extra items at the start of the layout:

1
2
3
4
5
recyclerView.setExtraLayoutSpaceStrategy(object : ExtraLayoutSpaceStrategy {
    override fun calculateStartExtraLayoutSpace(state: RecyclerView.State): Int {
        return recyclerView.width / 2
    }
})