如何在Compose中交换行和列中的项目

huangapple go评论75阅读模式
英文:

How to swap the items inside Rows and Columns in Compose

问题

以下是翻译好的内容:

这段代码将有助于我们通过找到一些自定义的RowsColumnsArrangement来减少这个逻辑。

Column() {
    val swap = someCalculation() // 或者 val swap = true
    if (swap) {
        TextView("底部", Color.Red)
        TextView("顶部", Color.Magenta)
    } else {
        TextView("顶部", Color.Red)
        TextView("底部", Color.Magenta)
    }
}

我的意思是减少的代码类似于这样:

Row(horizontalArrangement = CustomArrangement) {
    TextView("左侧", Color.Red)
    TextView("右侧", Color.Magenta)
}

Column(verticalArrangement = CustomArrangement) {
    TextView("顶部", Color.Red)
    TextView("底部", Color.Magenta)
}

以展示这样的效果,首先是右边,然后是左边,或者首先是底部,然后是顶部。

英文:

It will be helpful if we can reduce this logic by finding some custom Arrangement for RowsandColumns` to swap the order of items.

Column() {
    val swap = someCalculation() //  or val swap = true
    if (swap) {
        TextView("Bottom", Color.Red)
        TextView("Top", Color.Magenta)
    } else {
        TextView("Top", Color.Red)
        TextView("Bottom", Color.Magenta)
    }
}

I mean the reduced code something like this

Row(horizontalArrangement = CustomArrangement) {
    TextView("Left", Color.Red)
    TextView("Right", Color.Magenta)
}

Column(verticalArrangement = CustomArrangement) {
    TextView("Top", Color.Red)
    TextView("Bottom", Color.Magenta)
}

to show this Right first and Left Second or Bottom first and Top second.

答案1

得分: 2

以下是您要翻译的内容:

排列 https://developer.android.com/reference/kotlin/androidx/compose/foundation/layout/Arrangement

在参考了这个来自HorizontalOrVerticalCenter后,创建了一个新的排列方式Reverse

@Stable
val Reverse = object : Arrangement.HorizontalOrVertical {

   override fun toString() = "Arrangement#Reverse"
 
   override fun Density.arrange(
        totalSize: Int,
        sizes: IntArray,
        layoutDirection: LayoutDirection,
        outPositions: IntArray,
    ) = if (layoutDirection == LayoutDirection.Ltr) {
        order(sizes, outPositions, reverseInput = true)
    } else {
        order(sizes, outPositions, reverseInput = true)
    }

    override fun Density.arrange(totalSize: Int, sizes: IntArray, outPositions: IntArray) {
        order(sizes, outPositions, reverseInput = true)
    }

    private fun order(size: IntArray, outPosition: IntArray, reverseInput: Boolean) {
        var current = 0
        size.forEachIndexed(reverseInput) { index, it ->
            outPosition[index] = current
            current += it
        }
    }

    private inline fun IntArray.forEachIndexed(reversed: Boolean, action: (Int, Int) -> Unit) {
        if (!reversed) {
            forEachIndexed(action)
        } else {
            for (i in (size - 1) downTo 0) {
                action(i, get(i))
            }
        }
    }
}

我可以在内部使用它

@Composable
fun TextView(text: String, color: Color) {
    Column {
        Text(text = text, fontSize = 30.sp, color = color)
    }
}

 Row(horizontalArrangement = Reverse) {
    TextView("Right", Color.Red)
    TextView("Left", Color.Blue)
}

Column(verticalArrangement = Reverse) {
    TextView("Right", Color.Red)
    TextView("Left", Color.Blue)
}
英文:

> Arrangement https://developer.android.com/reference/kotlin/androidx/compose/foundation/layout/Arrangement

After referring this Center from HorizontalOrVertical created a new Arrangement Reverse.

@Stable
val Reverse = object : Arrangement.HorizontalOrVertical {

   override fun toString() = "Arrangement#Reverse"
 
   override fun Density.arrange(
        totalSize: Int,
        sizes: IntArray,
        layoutDirection: LayoutDirection,
        outPositions: IntArray,
    ) = if (layoutDirection == LayoutDirection.Ltr) {
        order(sizes, outPositions, reverseInput = true)
    } else {
        order(sizes, outPositions, reverseInput = true)
    }

    override fun Density.arrange(totalSize: Int, sizes: IntArray, outPositions: IntArray) {
        order(sizes, outPositions, reverseInput = true)
    }

    private fun order(size: IntArray, outPosition: IntArray, reverseInput: Boolean) {
        var current = 0
        size.forEachIndexed(reverseInput) { index, it ->
            outPosition[index] = current
            current += it
        }
    }

    private inline fun IntArray.forEachIndexed(reversed: Boolean, action: (Int, Int) -> Unit) {
        if (!reversed) {
            forEachIndexed(action)
        } else {
            for (i in (size - 1) downTo 0) {
                action(i, get(i))
            }
        }
    }
}

I can use this inside

@Composable
fun TextView(text: String, color: Color) {
    Column {
        Text(text = text, fontSize = 30.sp, color = color)
    }
}

 Row(horizontalArrangement = Reverse) {
    TextView("Right", Color.Red)
    TextView("Left", Color.Blue)
}

Column(verticalArrangement = Reverse) {
    TextView("Right", Color.Red)
    TextView("Left", Color.Blue)
}

答案2

得分: 1

// 通过在列表中添加可组合项并在需要时反转列表来解决此问题。

val list = ArrayList<@Composable () -> Unit>()

list.add @Composable {...}
list.add @Composable {...}
list.add @Composable {...}

if(swap) {
    list.reverse()
}

list.forEach { item ->
    item.invoke()
}

// 因此,您的代码应该类似于:

val list = ArrayList<@Composable () -> Unit>()

list.add @Composable {
    TextView("Bottom", Color.Red)
}
list.add @Composable {
    TextView("Top", Color.Magenta)
}

if(swap) {
    list.reverse()
}

list.forEach { item ->
    item.invoke()
}
英文:

I fix this by adding composable items in a list and reversing the list if needed.

val list = ArrayList&lt;@Composable () -&gt; Unit&gt;()

list.add @Composable {...}
list.add @Composable {...}
list.add @Composable {...}

if(swap) {
    list.reverse()
}

list.forEach { item -&gt;
    item.invoke()
}

So your code should look something like this:

val list = ArrayList&lt;@Composable () -&gt; Unit&gt;()

list.add @Composable {
    TextView(&quot;Bottom&quot;, Color.Red)
}
list.add @Composable {
    TextView(&quot;Top&quot;, Color.Magenta)
}

if(swap) {
    list.reverse()
}

list.forEach { item -&gt;
    item.invoke()
}

huangapple
  • 本文由 发表于 2023年5月17日 20:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定