在Android中有没有类似seekBar的onStopTrackingTouch和verticalSeekBar控件的功能?
Is there a similar function just like onStopTrackingTouch of SeekBar with verticalSeekBar control in Android?
VerticalSeekBar 控件是一个垂直的 seekBar,您可以在 https://github.com/lukelorusso/VerticalSeekBar 查看更多详细信息。
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>
通过检查代码,很明显 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()
}
}
VerticalSeekBar 控件是一个垂直的 seekBar,您可以在 https://github.com/lukelorusso/VerticalSeekBar 查看更多详细信息。
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>
通过检查代码,很明显 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()
}
}