android - 获取值 ($field) 时,Kotlin 属性处于循环中
问题描述
当我在 xml 中绑定“名称”属性中的值时,getter 似乎处于循环中,并且其中的值在屏幕中连接,直到我停止应用程序。
1 - 我不确定是否需要使用notifyPropertyChanged()
或注释@set and @get;
2 - 如果我在没有连接字符串的情况下设置 get,它会很好地工作:get() = field;
3 - 如果我尝试在大括号内返回获取值,问题会不断出现: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)
}
}
这是活动:
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>
屏幕上的结果始终是:
领域:asdasd
字段:字段:asdasd
字段:字段:asdasd
字段:字段:字段:asdasd
字段:字段:字段:字段:asdasd
...到无限
解决方案
只是为了完整起见:
似乎每当属性更改侦听器更新文本视图时,它都会检测到其自身内容的更改,因此会尝试保存回可观察对象,从而触发循环,因为您使用的是双向绑定。
这个问题可以通过使用单向绑定@{}
(
推荐阅读
- c# - 迭代 linq 查询中的数组/列表
- gcc - 针对特定平台的交叉编译——链接 libc
- xamarin - 如何在 Xamarin.Android 中异步实现 Android In App BillingClient
- javascript - api调用后猫头鹰轮播2不工作
- vue.js - Vue.js prop 同步修饰符不更新父组件
- javascript - javascript将变量放入模块
- matlab - MATLAB fft 与数学傅立叶
- qt - QAction 作为 QGraphicsItem 上的键盘快捷键
- html - 在 Bootstrap 中垂直居中所有表格行元素
- python - 无法通过 Airflow DAG 连接到 Docker Postgres 服务器