Reputation: 51
In my routine (generateView()) I am creating a set of textViews (actually letter tiles) arranged in a constraint layout (reglette). To create the textviews I am using the expression 'val childView = TextView(requireActivity())' and then adjust the different parameters of the view. Elsewhere in the fragment I set onTouchListeners on those created views. And this works all fine.
Problem is when I call the routine for the second time and I want to get rid of the first set of views to replace them with a new one. I tried to use 'set.clear(view.id)' but the views remain on the screen and the new set overrides them.
I am doing something wrong ? Here the code of the routine:
private fun generateView(tirage:CharArray) {
val set = ConstraintSet()
val parentLayout = binding.reglette
var i = 0
set.clone(parentLayout)
for (v in parentLayout.children)
set.clear(v.id)
set.applyTo(parentLayout)
listLettre.clear()
val dummyLeft = TextView(requireActivity())
listLettre.add(dummyLeft)
listPos.add(i)
listIndex.add(i++)
for (lettre in tirage) {
val childView = TextView(requireActivity())
childView.id = View.generateViewId()
childView.width = LARGEUR_LETTRE.toInt()
childView.height = LARGEUR_LETTRE.toInt()
childView.setBackgroundResource(R.drawable.lettre)
childView.gravity = Gravity.CENTER
childView.text = lettre.toString()
childView.textSize = 34f
childView.setTypeface(null, Typeface.BOLD)
parentLayout.addView(childView, i - 1)
listLettre.add(childView)
listPos.add(i)
listIndex.add(i++)
}
val dummyRight = TextView(requireActivity())
listLettre.add(dummyRight)
listPos.add(i)
listIndex.add(i)
val premL = listLettre.drop(1).first()
val pad = (parentLayout.width - (tirage.size * LARGEUR_LETTRE.toInt())) / 2
set.clone(parentLayout)
set.connect(
premL.id,
ConstraintSet.START,
ConstraintSet.PARENT_ID,
ConstraintSet.START,
pad
)
val windowedList = listLettre.drop(1).dropLast(1).windowed(2, 1, false)
for (pair in windowedList)
set.connect(
pair.last().id,
ConstraintSet.START,
pair.first().id,
ConstraintSet.END
)
set.applyTo(parentLayout)
}
Upvotes: 0
Views: 26
Reputation: 51
Actually set.clear(view)
is removing all constraints from the view, not the view itself. The command to remove a view is
parentLayout.removeView(view)
or, to remove them all
parentLayout.removeAllViews()
Upvotes: 0