Kotlin 属性 在获取值 ($field) 时处于循环中

A Kotlin property is in looping when getting the value ($field)

当我将 'name' 属性 中的值绑定到 xml 中时,getter 似乎处于循环中,并且其中的值连接在屏幕直到我停止应用程序。

1 - 我不确定是否需要使用 notifyPropertyChanged() 或注解 @set and @get;

2 - 如果我在没有连接字符串的情况下设置 get,它会很好地工作:get() = field;

3 - 如果我尝试 return 获取大括号内的值,问题会不断发生:get(){return "Field: $field"};

这是型号:

class ContactModel : BaseObservable(){

    @set:Bindable
    @get:Bindable
    var name: String = ""
        get() = "Field: $field"
        set(value) {
            field = value
            notifyPropertyChanged(BR.name)
        }


    @set:Bindable
    @get:Bindable
    var email: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.email)
        }

    @set:Bindable
    @get:Bindable
    var phone: String = ""
        set(value) {
            field = value
            notifyPropertyChanged(BR.phone)
        }

}

这是activity:

class MainActivity : AppCompatActivity() {


    lateinit var binding: ActivityMainBinding
    var contactModel: ContactModel = ContactModel()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        contactModel = ContactModel(/*"Rômulo", "romulocoviel@gmail.com", "(19):98421-0821"*/)
        contactModel.name = "Rômulo"
        contactModel.email = "romulocoviel@gmail.com"
        contactModel.phone = "(19):98421-0821"

        binding.contactModel = contactModel
        binding.setLifecycleOwner(this)
    }

    fun changeSignatures(view: View) {
        Log.e("TESTING", "Testando!" + contactModel.name)
        val nameList: ArrayList<ContactModel> = ArrayList()

        contactModel.name = "asdasd"
        contactModel.email = "asdasda"
        contactModel.phone = "asdasd"

    }

}

这里是 XML,我有一个按钮,点击时会更改值和绑定视图:

<data>
    <variable
            name="contactModel"
            type="com.example.romulo.bindingmetricsconversor.ContactModel"/>
</data>

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:text="@={contactModel.name}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvName" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp" android:layout_marginStart="8dp"/>
    <TextView
            android:text="@={contactModel.email}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvEmail" android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/tvName" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp" android:layout_marginStart="8dp"/>
    <TextView
            android:text="@={contactModel.phone}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvPhone" android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/tvEmail" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp" android:layout_marginStart="8dp"/>
    <Button
            android:text="Change"
            android:layout_width="wrap_content"
            android:layout_height="49dp"
            android:id="@+id/btChange" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp" android:layout_marginRight="8dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp" android:onClick="changeSignatures"/>
</android.support.constraint.ConstraintLayout>

屏幕上的结果总是:

Field: asdasd

Field:Field: asdasd

Field:Field: asdasd

Field:Field:Field: asdasd

Field:Field:Field:Field: asdasd

... to the infinite

为了完整起见:

it seems like whenever the text view is updated by the property change listener it detects a change in its own content and thus tries to save back to the observable, triggering a loop, since you're using two-way binding.

问题可以通过使用单向绑定来解决 (@{}),因为在更改文本时,文本视图会触发自己的侦听器并尝试修改可观察对象,将其发送到无限递归。