VerticalSeekBar控件是垂直的seekBar,您可以在https://github.com/lukelorusso/VerticalSeekBar上查看更多详细信息。

像SeekBar控件中的onStopTrackingTouch一样,VerticalSeekBar控件中是否有事件函数?

当我停下来拇指时,我希望在VerticalSeekBar中做一些事情。

代码A

horizontalSeekBar?.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
                // Display the current progress of SeekBar
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                // Do something
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                requireContext().toast(seekBar.progress.toString() +"M")
            }
})


verticalSeekBar?.setOnProgressChangeListener { progressValue ->
      requireContext().toast(  progressValue.toString()+"X")
}


布局A

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/camera_ui_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <SeekBar
            android:id="@+id/horizontalSeekBar"
            android:layout_width="100dp"
            android:layout_height="30dp"

            android:background="@color/white"

            android:max="10"
            android:thumb="@drawable/ic_thumb"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/verticalSeekBar"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    <com.lukelorusso.verticalseekbar.VerticalSeekBar
            android:id="@+id/verticalSeekBar"
            android:layout_width="30dp"
            android:layout_height="150dp"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:vsb_progress="2" />

</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

从代码检查来看,很明显VerticalSeekBar没有提供SeekBar.OnSeekBarChangeListener的所有功能。具体来说,尽管支持进度更改侦听器,但缺少模仿onStartTrackingTouch()和onStopTrackingTouch()功能的回调。

但是,可以通过如下扩展VerticalSeekBar来添加此缺少的功能:

class MyVerticalSeekBar(context: Context, attrs: AttributeSet) : VerticalSeekBar(context, attrs) {

    private var mChangeListener: OnVerticalSeekBarChangeListener? = null

    fun setOnVerticalSeekBarChangeListener(listener: OnVerticalSeekBarChangeListener?) {
        mChangeListener = listener
        // We will use the progress listener as defined in VerticalSeekBar.
        if (listener == null) {
            super.setOnProgressChangeListener(null)
        } else {
            super.setOnProgressChangeListener { newProgress ->
                listener.onProgressChanged(this, newProgress, true)
            }
        }
    }

    // Spy on touch events targeting the vertical seek bar to handle
    // onStartTrackingTouch() and onStopTrackingTouch()
    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        if (mChangeListener != null) {
            when (ev.actionMasked) {
                MotionEvent.ACTION_DOWN -> mChangeListener?.onStartTrackingTouch(this)
                MotionEvent.ACTION_UP,
                MotionEvent.ACTION_CANCEL -> mChangeListener?.onStopTrackingTouch(this)
            }
        }
        return super.onInterceptTouchEvent(ev)
    }

    interface OnVerticalSeekBarChangeListener {
        fun onProgressChanged(seekBar: MyVerticalSeekBar, progress: Int, fromUser: Boolean)
        fun onStartTrackingTouch(seekBar: MyVerticalSeekBar)
        fun onStopTrackingTouch(seekBar: MyVerticalSeekBar)
    }
}


一个基于MyVerticalSeekBar的示例,该示例基于GitHub存储库中VerticalSeekBar的示例应用程序:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mainVerticalSeekBar.progress = 75

        mainVerticalSeekBar.setOnVerticalSeekBarChangeListener(
            object : MyVerticalSeekBar.OnVerticalSeekBarChangeListener {
                override fun onProgressChanged(
                    seekBar: MyVerticalSeekBar, progress: Int, fromUser: Boolean
                ) {
                    Log.d("MainActivity", "<<<<onProgressChanged (V)")
                    mainProgressValue.text = progress.toString()
                }

                override fun onStartTrackingTouch(seekBar: MyVerticalSeekBar) {
                    Log.d("MainActivity", "<<<<onStartTrackingTouch (V)")
                }

                override fun onStopTrackingTouch(seekBar: MyVerticalSeekBar) {
                    Log.d("MainActivity", "<<<<onStopTrackingTouch (V)")
                }
            }
        )

        mainProgressValue.text = mainVerticalSeekBar.progress.toString()

    }
}

09-27 15:03