如何从viewModel中的xml获取属性视图

huangapple go评论83阅读模式
英文:

how can get attribute view in xml from viewModel

问题

我有3个文件(Activity、ViewModel、XML)

我使用MVVM将XML连接到ViewModel。

<?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">

    <data>
        <variable
            name="viewModel"
            type="com.example.MainViewModel"/>
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/priceTv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={viewModel.price}"/>

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

ViewModel.class

open class MainViewModel : ViewModel() {

    val price = MutableLiveData<String>()
    val priceTextViewWidth = MutableLiveData<Int>()

}

在活动中,我使用以下代码将XML连接到ViewModel:

binding.viewModel = viewModel

我可以通过更改ViewModel中的价格变量来更新TextView,但是我需要将TextView的宽度传递给ViewModel中的priceTextViewWidth

英文:

I have a 3 files(Activity, ViewModel,XML)

I use MVVM and connect XML to ViewModel.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;layout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;

    &lt;data&gt;
        &lt;variable
            name=&quot;viewModel&quot;
            type=&quot;com.example.MainViewModel&quot;/&gt;
    &lt;/data&gt;

    &lt;androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

        &lt;androidx.appcompat.widget.AppCompatTextView
            android:id=&quot;@+id/priceTv&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;@={viewModel.price}&quot;/&gt;
        
    &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/layout&gt;

ViewModel.class

open class MainViewModel : ViewModel() {

    val price = MutableLiveData&lt;String&gt;()
    val priceTextViewWidth = MutableLiveData&lt;Int&gt;()

}

and in the activity, I connect XML to view model with

 binding.viewModel = viewModel

I can update Textview with change price variable in View Model but I need to pass TextView width to priceTextViewWidth in ViewModel.

答案1

得分: 1

将:

android:layout_width="wrap_content"

替换为:

android:layout_width="@{viewModel.priceTextViewWidth}"
英文:

Replace :

android:layout_width=&quot;wrap_content&quot;

with

android:layout_width=&quot;@{viewModel.priceTextViewWidth}&quot;

huangapple
  • 本文由 发表于 2020年8月18日 16:42:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63464945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定