是否有类似的功能,就像Android中带有verticalSeekBar控件的SeekBar的onStopTrackingTouch?

问题描述 投票:4回答:1

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

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

当我停下拇指时,希望在

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>
android android-seekbar
1个回答
3
投票
根据代码的检查,很明显

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) } }
基于GitHub存储库中用于

VerticalSeekBar

的示例应用程序,使用MyVerticalSeekBar的示例: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() } }
© www.soinside.com 2019 - 2024. All rights reserved.