3

How can I create a custom Arrangement for LazyRow to add additional spacing at beginning and end, but have even spacing in between?

Start|< more space> Item 1 Item 2 Last Item |End

object CustomArrangement : Arrangement.Horizontal {
  override fun Density.arrange(
    totalSize: Int,
    sizes: IntArray,
    layoutDirection: LayoutDirection,
    outPositions: IntArray
  ) {

  }
}

https://developer.android.com/jetpack/compose/lists#custom-arrangements

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Sudhir Singh Khanger
  • 1,598
  • 2
  • 17
  • 34

2 Answers2

3

It is not the answer to your question, but you can achieve the same adding a header and footer items, or using a horizontal PaddingValues.

A simple LazyRow:

LazyRow(
    horizontalArrangement =   Arrangement.spacedBy(8.dp),
    contentPadding = PaddingValues(horizontal = 50.dp),
    modifier= Modifier.fillMaxSize(),
) {

    items(itemsList) {
        Text("Item is $it")
    }
}

or something like:

LazyRow(
    horizontalArrangement =   Arrangement.spacedBy(8.dp),
    modifier= Modifier.fillMaxSize()
) {
    //Header
    item(){
        Spacer(modifier = Modifier.width(40.dp))
    }
    items(itemsList) {
        Text("Item is $it")
    }
    //Footer
    item(){
        Spacer(modifier = Modifier.width(40.dp))
    }
}

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1

Maybe you can get the effect you're looking for with one of the paramenters for LazyRow: contentPadding = PaddingValues(horizontal = "your_value")

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 28 '22 at 11:18