【问题标题】:Show Label in Material Slider when user stop to drag [KOTLIN]当用户停止拖动 [KOTLIN] 时在 Material Slider 中显示标签
【发布时间】:2020-11-21 19:09:33
【问题描述】:

这个可以吗?

我正在使用com.google.android.material.slider.Slider,我知道setLabelFormatter 事件允许您在拖动材质滑块时查看或修改标签,但我想在用户停止拖动时获取值以了解当前Material Slider 的值

类似这样的东西(没有被逼着拿ss哈哈哈哈哈)

非常感谢

【问题讨论】:

    标签: android kotlin slider material-design


    【解决方案1】:

    我查看了材质组件 Slider 源代码,以了解它如何/何时决定显示其标签。

    • 似乎没有任何公共 API 允许我们显示它....
    • 它在收到触地事件时显示....

    我编写了一些函数来欺骗材质滑块,使其认为它被触摸,从而触发标签显示。

    fun Slider.showLabelForever()
    {
        // start showing the label immediately
        showLabelUntilNextTouchUp()
    
        // send a touch event when user stops touching the slider to trigger the
        // label to show up again
        addOnSliderTouchListener(
            object:Slider.OnSliderTouchListener
            {
                override fun onStartTrackingTouch(slider:Slider) = Unit
                override fun onStopTrackingTouch(slider:Slider) = showLabelUntilNextTouchUp()
            }
        )
    }
    
    fun Slider.showLabelUntilNextTouchUp()
    {
        // sent a touch event to cause the label for the slider to show.
        // a side effect of calling onTouchEvent is that it will cause
        // the slider to change the progress value; we save the original
        // progress value before calling onTouchEvent, then restore the
        // value after calling onTouchEvent.
        val originalValue = value
        onTouchEvent(
            MotionEvent.obtain(0L,0L,MotionEvent.ACTION_DOWN,0f,0f,0)
        )
        value = originalValue
    }
    

    【讨论】:

    • 对我不起作用 :( 它只在开始时显示拇指周围的光环,在停止拖动分散器后,并没有显示标签
    • @OlenaY hmmmmmmm 请确保在标签上调用 showLabelForever() 后,这不会发生:app:labelBehavior="gone",并且滑块上永远不会调用 clearOnSliderTouchListeners()....这行得通吗?
    • 我有 app:labelBehavior="withinBounds",并且不清理听众... =(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    相关资源
    最近更新 更多