Textview 和 Seekbar 绑定到相同的 属性,但 textview 不显示更新后的值

Textview and Seekbar are bound to the same property, but the textview doesn´t show the updated value

我用 MutableLiveData 创建了一个 viemodel

val duration = MutableLiveData(2)

在我的Activity中设置Databinding

binding = DataBindingUtil.setContentView(this, R.layout.activity_ticket)
    binding.viewmodel = this.viewModel

这是我的布局

<data>
    <variable
            name="viewmodel"
            type="com.text.viewmodel.TicketViewModel"/>
</data>
 ...
  <TextView
            android:id="@+id/textview_duration"
            android:layout_margin="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{String.valueOf(viewmodel.duration)}"
    />
    <SeekBar
            android:id="@+id/seekBar_duration"
            android:max="8"
            android:min="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:progress="@={viewmodel.duration}"
    />

当用户更改搜索栏时,ViewModel 中的值会正确更新,但文本视图不会自行刷新。

我自己找到了解决方案。

我忘记将 activity 设置为 lifecycleowner

binding = DataBindingUtil.setContentView<ActivityTicketBinding>(this, R.layout.activity_ticket)
binding.lifecycleOwner = this
binding.viewmodel = this.viewModel