简体   繁体   English

双向数据绑定 SeekBar

[英]two way data binding SeekBar

I'm stuck at trying to use two way data binding for a seekbar.我一直在尝试对搜索栏使用两种方式的数据绑定。 What I'm trying to do is show the progress of the seekbar in a textview above it.我想要做的是在它上面的文本视图中显示搜索栏的进度。 I've looked for solutions but none seem to work.我一直在寻找解决方案,但似乎都没有奏效。

This is the viewModel这是视图模型

(the class extends from BaseObservable (该类从BaseObservable扩展

private int playbackSpeed;
private int playbackSpeedDisplay;

@Bindable
public int getPlaybackSpeed() {
    return playbackSpeed;
}

public void setPlaybackSpeed(int speed) {
    this.playbackSpeed = speed;
    notifyPropertyChanged(BR.playbackSpeed);
    setPlaybackSpeedDisplay(speed);
    Log.d("PlaybackSpeedViewModel", "Playback Speed set at " + speed);
}

@Bindable
public String getPlaybackSpeedDisplay() {
    return Integer.toString(playbackSpeedDisplay);
}

public void setPlaybackSpeedDisplay(int speed) {
    this.playbackSpeedDisplay = speed;
    notifyPropertyChanged(BR.playbackSpeedDisplay);
}

The layout布局

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>
    <variable
        name="viewModel"
        type="com.android.PlaybackSpeedViewModel"/>
</data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/playback_speed_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{viewModel.playbackSpeedDisplay}"
            tools:text="Playback Speed:" />

        <SeekBar
            android:id="@+id/playback_speed_seek_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:progress="@={viewModel.playbackSpeed}"/>
    </LinearLayout>
</layout>

It's probably very simple but I can't seem to get it right.这可能很简单,但我似乎无法正确理解。 Thanks!谢谢!

Make sure you have binded your viewModel properly .确保您已正确绑定您的 viewModel I have verified in activity it's working fine.我已经在活动中验证它工作正常。

    ActivityTestBinding binding = DataBindingUtil.bind(view);
    TestViewModel viewModel = new TestViewModel();
    binding.setViewModel(viewModel);

The below code worked fined for me to get the progress value from seekBar using data binding.下面的代码对我来说seekBar用, seekBar使用数据绑定从seekBar获取进度值。

fun onSeekBarChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
Logger.d("progress : $progress")
}

XML XML

            <androidx.appcompat.widget.AppCompatSeekBar
                android:id="@+id/seekBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:max="4"
                android:onProgressChanged="@{viewModel.onSeekBarChanged}"
                android:padding="@dimen/_8sdp"
                android:progress="1"
                android:progressTint="@color/colorAccent"
                android:thumbTint="@color/colorAccent"
                android:tickMarkTint="@color/colorAccent"
                app:layout_constraintTop_toBottomOf="@+id/textViewIntervalBetweenAttempts"
                tools:ignore="UnusedAttribute" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM