美好的一天,
我需要显示一张保真卡列表,一个保真卡的边框很小,在单击时,它会在滑动时完全显示出来。
在一个片段中,我在通过代码填充的屏幕上的某个地方使用了ConstraintLayout
。
我将所有子项(在WS调用之后已知的总数)添加到ConstraintLayout中,然后使用ConstraintSet
设置初始约束。
但是在创建初始片段时,我发现我必须在ConstraintLayout
总体和创建Constraints
之间应用超时(令人作呕,对吗?)< / p>
添加每张卡...
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之后再添加Constraints之间应用任何超时?
还有更好的解决方案,这个丑陋的超时时间? 谢谢
答案 0 :(得分:0)
我找到了一种更好的方法。
似乎必须确保在UI线程上完成约束创建。这就是为什么它可以与timeout
一起使用的原因。
因此,我通过Anko呼叫更改了timeout
呼叫:
doAsync {
uiThread {
resetCardLayout()
}
}