有没有办法以编程方式使用constraintlayout helper widget Flow? [英] Is there any way to using constraintlayout helper widget Flow programmatically?

查看:37
本文介绍了有没有办法以编程方式使用constraintlayout helper widget Flow?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用约束布局 androidx.constraintlayout.helper.widget.Flow 小部件,但是当我搜索时,我找不到任何关于以编程方式使用流小部件的示例.如何以编程方式设置constraint_referenced_ids?

I want to use constraints layout androidx.constraintlayout.helper.widget.Flow widget,but When I search I coluldn't find any example about using flow widget programmatically. How can I set constraint_referenced_ids programmatically?

<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"

    />

推荐答案

这对我有用:

我的自定义 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
}
}

我的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>

Fragment 中 PerkFlow 的设置:

The setup of the PerkFlow in Fragment:

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

希望这会有所帮助!

还有一件事:如果你再次调用它(在 RecyclerView 项目中,或者新数据到达),不要忘记清除父视图,否则元素将被复制.如果需要,我也可以发送该代码.

And one more thing: if you call this again (in RecyclerView item, or new data arrives), don't forget to clear the parent view or the elements will be duplicated. I can send that code too if needed.

这篇关于有没有办法以编程方式使用constraintlayout helper widget Flow?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆