如何在 Kotlin 片段中设置 OnSeekBarChangeListener

How to setOnSeekBarChangeListener in a Kotlin Fragment

我目前正在我的大学 类 开发一个移动应用程序,它利用 seekBar 让用户决定使用计时器进行测验。此应用程序使用 main 来托管所有片段。目前我只希望文本框显示用户将搜索栏滚动到的位置,但我正在努力寻找解决方案。任何建议将不胜感激。这是我在 TitleFragment.kt:

package com.example.android.guesstheword.screens.title

import android.os.Bundle
import android.view.DragEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import androidx.databinding.adapters.SeekBarBindingAdapter
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.android.guesstheword.R
import com.example.android.guesstheword.databinding.TitleFragmentBinding
import kotlinx.android.synthetic.main.title_fragment.*

/**
 * Fragment for the starting or title screen of the app
 */
class TitleFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View {

        // Inflate the layout for this fragment
        val binding: TitleFragmentBinding = DataBindingUtil.inflate(
                inflater, R.layout.title_fragment, container, false)

        binding.timerSwitch.setOnClickListener {

            if(binding.timerSwitch.isChecked){
                binding.timerBar.visibility = View.VISIBLE
            }
            else{
                binding.timerBar.visibility = View.INVISIBLE
            }
        }

        val seekBar = binding.timerBar
        seekBar.setOnSeekBarChangeListener(binding)
        binding.playGameButton.setOnClickListener {

            findNavController().navigate(TitleFragmentDirections.actionTitleToGame())
        }
        return binding.root
    }
}

private fun SeekBar.setOnSeekBarChangeListener(binding: TitleFragmentBinding) {
    val seconds = binding.timerSeconds
    seconds.visibility = View.VISIBLE
    seconds.text = binding.timerBar.progress.toString()
}

title_fragment.xml:

<?xml version="1.0" encoding="utf-8"?><!--
<layout 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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/title_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".screens.title.TitleFragment">

        <TextView
            android:id="@+id/get_ready_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:fontFamily="sans-serif"
            android:text="@string/get_ready"
            android:textColor="@color/black_text_color"
            android:textSize="14sp"
            android:textStyle="normal"
            app:layout_constraintBottom_toTopOf="@+id/title_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed" />

        <TextView
            android:id="@+id/title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:fontFamily="sans-serif"
            android:text="@string/title_text"
            android:textColor="@color/black_text_color"
            android:textSize="34sp"
            android:textStyle="normal"
            app:layout_constraintBottom_toTopOf="@+id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/get_ready_text"
            app:layout_constraintVertical_chainStyle="packed" />

        <Button
            android:id="@+id/play_game_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            android:text="@string/play_button"
            android:theme="@style/GoButton"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <Switch
            android:id="@+id/timer_switch"
            android:layout_width="95dp"
            android:layout_height="52dp"
            android:text="Timer"
            android:min="5"
            android:max="360"
            app:layout_constraintBottom_toTopOf="@+id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/title_text"
            app:layout_constraintVertical_bias="0.064"/>

        <SeekBar
            android:id="@+id/timerBar"
            android:layout_width="132dp"
            android:layout_height="85dp"
            android:clickable="false"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@+id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/timer_switch"
            app:layout_constraintVertical_bias="0.144" />

        <TextView
            android:id="@+id/timer_seconds"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@+id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/timerBar"
            app:layout_constraintVertical_bias="0.17" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

您创建了一个名为 setOnSeekBarChangeListener 的扩展函数,但实际上并没有设置它。

为了设置监听器,你需要做这样的事情:

binding.timerBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
    override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
        // here, you react to the value being set in seekBar
    }

    override fun onStartTrackingTouch(seekBar: SeekBar) {
        // you can probably leave this empty
    }

    override fun onStopTrackingTouch(seekBar: SeekBar) {
        // you can probably leave this empty
    }
})

但请记住,这不是您创建的扩展函数。