【问题标题】:ConstraintLayout by Code - Set Constraint too soonConstraintLayout by Code - 过早设置约束
【发布时间】:2018-12-19 19:08:37
【问题描述】:

早安,

我需要显示一个保真卡列表,一张在其他卡上,边距很小,点击它时,它会在滑动时完全显示它。

在一个片段中,我在屏幕中的某处使用了ConstraintLayout,并通过代码填充。 我将所有子项(在 WS 调用后已知)添加到 ConstraintLayout,然后我使用 ConstraintSet 设置初始约束。

但是在最初的片段创建时,我发现我必须在ConstraintLayout 群体和创建Constraints 之间应用一个超时(恶心,对吧?)

添加每张卡片...

val card = FrequencyCardView()
fid_card_container.addView(card as View)
listCard.add(card)

然后创建约束:

val set = ConstraintSet()
val containerId = fid_card_container.id
set.clone(fid_card_container)

listCard.forEachIndexed { index, card ->
    val marginTop = when (index) {
        0 -> 0
        else -> (listCard[index -1] as ViewGroup).height / 4
    }

    val rootId = if (index == 0) containerId else (index - 1)

    set.connect(card.getId(), TOP, rootId, TOP, marginTop)
    set.connect(card.getId(), ConstraintSet.START, containerId, ConstraintSet.START, 0)
    set.connect(card.getId(), ConstraintSet.END, containerId, ConstraintSet.END, 0)
    card.setOpen(false)
}

// Apply the changes
set.applyTo(fid_card_container)

任何想法,为什么我需要在将 Childs 添加到 ConstraintLayout 然后添加 Constraint 之间应用任何超时?

任何更好的解决方案,这个丑陋的 TimeOut? 谢谢

【问题讨论】:

    标签: android kotlin constraint-layout-chains


    【解决方案1】:

    我找到了更好的方法。

    似乎必须确保在 UI 线程上完成约束创建。这就是为什么它可以与timeout 一起使用。

    所以,我把 timeout 的调用改成了 Anko 调用:

    doAsync {
      uiThread {
        resetCardLayout()
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      相关资源
      最近更新 更多