0

Good day,

I need to show a list of fidelity cards, one on the others with a small border margin, and while clicking on it, it reveal it completely while sliding.

In a fragment, I use a ConstraintLayout somewhere in the screen that I populate by code. I add all child's (total known after WS called) into the ConstraintLayout, then I am using ConstraintSet to set the initial constraints.

But at the initial fragment creation, I have found out that I must apply a TimeOut between the ConstraintLayout population and creating the Constraints (disgusting, right?)

adding each card...

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

Then create the constraints:

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)

Any idea, why I need to apply any timeout between adding the Childs into the ConstraintLayout and then add the Constraints?

Any better solution, that this ugly TimeOut? Thanks

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
Tweety B
  • 65
  • 6

1 Answers1

0

I have found a better way.

It seems that the constraints creation must be ensured to be done on the UI Thread. This is why it does work with the timeout.

So, I have changed the timeout call by the Anko call:

doAsync {
  uiThread {
    resetCardLayout()
  }
}
Tweety B
  • 65
  • 6