有没有办法以编程方式使用 ConstraintLayout 的 Flow 帮助器小部件?

hug*_*rde 7 android android-constraintlayout constraintlayout-flow

我想使用ConstraintLayoutFlow小部件,但是当我搜索时,我找不到任何有关以编程方式使用Flow小部件的示例。

如何以constraint_referenced_ids编程方式设置?

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.constraintlayout.helper.widget.Flow
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="text1,text2"
        app:flow_horizontalBias="0"
        app:flow_horizontalGap="10dp"
        app:flow_horizontalStyle="packed"
        app:flow_verticalBias="0"
        app:flow_wrapMode="chain"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

use*_*380 5

这对我有用:

我的自定义PerkFlow类:

class PerkFlow(context: Context, attrs: AttributeSet?) : Flow(context, attrs) {

fun setup(
    parentView: ViewGroup,
    perks: List<String>
) {
    val referencedIds = IntArray(perks.size)
    for (i in perks.indices) {
        val textView = createTextView(context)
        textView.text = perks[i]
        textView.id = View.generateViewId()

        parentView.addView(textView)
        referencedIds[i] = textView.id
    }
    this.referencedIds = referencedIds
}

private fun createTextView(context: Context): TextView {
    val textView = TextView(context)
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12F)
    textView.setTextColor(context.resources.getColor(R.color.gmm_white))
    return textView
}
}
Run Code Online (Sandbox Code Playgroud)

我的xml

  <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

..... (a lot of other code)

  <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/vendor_details_perks_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/vendor_details_favourite">

            <com.perkapp.mobile.views.PerkFlow
                android:id="@+id/vendor_details_perks"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:flow_horizontalBias="0"
                app:flow_horizontalGap="5dp"
                app:flow_horizontalStyle="packed"
                app:flow_verticalBias="0"
                app:flow_verticalGap="2dp"
                app:flow_wrapMode="chain"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

 ..... (a lot of other code)

 </androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

Fragment中PerkFlow的设置:

  binding.vendorDetailsPerks.setup(
            binding.vendorDetailsPerksContainer,
            listOf("apple, banana, blackberries, blueberries, cherries, grapes, lemon, orange, peaches, pear, pineapple, plums, raspberries, strawberries, watermelon ")
        )
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

还有一件事:如果您再次调用此函数(在 RecyclerView 项中,或新数据到达),请不要忘记清除父视图,否则元素将被重复。如果需要的话我也可以发送该代码。