Customer
它需要将其组件绑定到
我创建了这样的类:
class Customer constructor(
val name: LiveData<String>
val email: LiveData<String>
)
在我的
ViewModel
MutableList
客户
:
class CustomerViewModel: ViewModel() {
val customers: MutableList<Customer> = mutableListOf()
}
然后动态创建片段:
for (i in 0..customerCount) {
val fragment = FragmentCustomer.newInstance(viewModel.customers.get(i))
supportFragmentManager.beginTransaction()
.add(R.id.fragmentContainer, fragment, "FragmentCustomer_$i")
.commit()
}
客户实例存储在片段中的一个变量中,然后我执行
DataBindingUtil.inflate
:
var binding: FragmentCustomerBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_customer, container, false)
var view = binding.root
binding.model = palavra
return view
在碎片的布局中:
<EditText
android:id="@+id/nameTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="@{model.name}"
android:onTextChanged="@={model.name}" />
****/ data binding error ****msg:The expression modelName.getValue() cannot be inverted: There is no inverse for method getValue, you must add an @InverseMethod annotation to the method to indicate which method should be used when using it in two-way binding expressions
.
我怎样才能解决这个问题?还有一个“额外”的问题,我是否还必须在像这样的事件上使用savedState存储视图内容
onDestroy