addisonElliott / SegmentedButton

Segmented Control/Button with animation for Android API 16+
Apache License 2.0
150 stars 39 forks source link

Add Remove View Dynamically #47

Open sagarnayak opened 3 years ago

sagarnayak commented 3 years ago

I want to add views at runtime. then remove all of them and then add another set of views.

Once i do addView() it works. but when i do removeAllView(). after that addView does not work. any one have a suggestion ??

dattran-pt19 commented 2 years ago

I have same problem, I suggest you should remove SegmentedButtonGroup and replace with the other. My code down here (parent view in my case I use a LinearLayout)

` fun setup( listValue: ArrayList<Pair<Int?, String?>>, sizeButton: SizeButton = SizeButton.Normal ) { this.removeAllViews() val segmentGroup = SegmentedButtonGroup(context).apply { val layout = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) layoutParams = layout setBackgroundResource(R.color.searchBackground) setRipple(false) radius = 8f.getSizePixel(context) setBorder( if (sizeButton == SizeButton.Big) 2f.getSizePixel(context) else 1f.getSizePixel( context ), ContextCompat.getColor(context, R.color.searchBackground), borderDashWidth, borderDashGap ) selectionAnimationDuration = 96 isEnabled = disableIndex == null isDraggable = disableIndex == null selectedButtonRadius = 8f.getSizePixel(context) setSelectedBackgroundColor(ContextCompat.getColor(context, R.color.white))

        setOnPositionChangedListener {
            onChangeIndex?.invoke(it)
        }
    }
    this.addView(segmentGroup)
    this.segmentGroup = segmentGroup

    for ((index, item) in listValue.withIndex()) {
        val segmentedButton = SegmentedButton(context).apply {
            val layout = LayoutParams(0, sizeButton.getSize(context), 1f)
            layoutParams = layout
            textTypeface = ResourcesCompat.getFont(context, R.font.roboto_regular)
            drawableTint = ContextCompat.getColor(context, R.color.placeHolder)
            selectedDrawableTint = ContextCompat.getColor(context, R.color.primary)
            drawableWidth = 24f.getSizePixel(context)
            drawableHeight = 24f.getSizePixel(context)
            textColor = ContextCompat.getColor(context, R.color.lightText)
            selectedTextColor = ContextCompat.getColor(context, R.color.primary)
            textSize = context.resources.getDimension(R.dimen.text_title)
            item.first?.let { drawable = ContextCompat.getDrawable(context, it) }
            item.second?.let { text = it }
            disableIndex?.let {
                if (index == it) {
                    textColor = ContextCompat.getColor(context, R.color.disable)
                    drawableTint = ContextCompat.getColor(context, R.color.disable)
                }
            }
        }
        segmentGroup.addView(segmentedButton)
    }
}`