android中的LayoutTransition / Animation仅在无限while循环中工作

问题描述

我正在像这样在android中动态创建一些按钮。

private fun createButton() {
        val randomHeight = (100..1000).random()
        val button = Button(this,null,R.attr.borderlessButtonStyle)
        val lp = RelativeLayout.LayoutParams((1080 / 20),randomHeight)
        lp.setMargins(0,0)
        button.id = btnId + 1
        button.setBackgroundColor(ContextCompat.getColor(this,R.color.colorAccent))
        if (btnId > 0)
            lp.addRule(RelativeLayout.RIGHT_OF,btnId)
        root_layout.addView(button,lp)
        btnId++
        button.text = lp.height.toString()
        buttonList.add(button)
    }

现在在onCreate方法中,我将循环调用此createButton函数,直到布局的宽度达到这样的设备的宽度为止。root_layout的宽度是自动换行的内容,因此当按钮强制布局扩展时,它会产生非常酷的效果。 ..

 root_layout.layoutTransition.enableTransitionType(LayoutTransition.CHANGE_APPEARING)
    val displayWidth = getDisplayWidth()
    GlobalScope.launch(Dispatchers.Main) {
        //  while (root_layout.width < displayWidth -1) finite but does not give animation
        //infinite but gives animation
        while (root_layout.width <= displayWidth) {
            Log.d("TAG","onCreate: ${root_layout.width} + $displayWidth ")
            createButton()
            delay(100)
        }
    }

也单击按钮,我会像这样随机更改所有按钮的高度

 btn_ran -> {
            for (i:Button in buttonList) {
                if (!isRunningSearch) {
                    var r = (100..1000).random()
                    i.layoutParams.height = r
                    i.text = r.toString()
                    i.requestLayout()
                }
            }
        }

现在的问题是onCreate中的while循环将变为无限,但是我的btn_ran函数可以很好地工作,因为按钮的高度随动画而变化,但是只有当我的while循环以某种方式中断了而我的while循环变为无限时,这种情况才会发生btn_ran函数有效,但没有任何动画。

我想将while循环运行到仅设备宽度或固定的时间,但是我也希望按钮像动画一样在无限循环中改变高度。

这里有两个gif,一个有无限循环和动画,另一个是没有无限循环而没有动画...

Animated buttons but infinite loop..On btn_ran cliclk

Limited loop but without animation ...On btn_ran cliclk

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...