如何解决如何初始化片段中的绑定属性以使双向数据绑定工作
所以我正在做一个项目,我完全迷失了。我已经看到如何使用 TextViews 进行数据绑定,但我被要求使用具有双向数据绑定的 EditText 视图来进行数据绑定。到目前为止,我已经到了这里。
XML 文件。
<?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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="myShoe"
type="com.udacity.shoestore.product.Shoe" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<TextView
android:id="@+id/title_detail_view"
style="@style/title_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="@string/add_shoe_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/shoe_name"
style="@style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:hint="@string/shoe_name_string"
android:inputType="text"
android:textSize="30sp"
android:text="@={myShoe.name}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_detail_view" />
<EditText
android:id="@+id/shoe_size"
style="@style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/size_string"
android:inputType="number|numberDecimal"
android:textSize="15sp"
android:text="@={myShoe.size}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shoe_name" />
<EditText
android:id="@+id/company_name"
style="@style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/company_string"
android:inputType="text"
android:textSize="15sp"
android:text="@={myShoe.company}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shoe_size" />
<EditText
android:id="@+id/shoe_description"
style="@style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/description_string"
android:inputType="text"
android:textSize="15sp"
android:text="@={myShoe.description}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/company_name" />
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorPrimaryDark"
android:text="@string/cancel_string"
android:textColor="@android:color/white"
app:layout_constraintBaseline_toBaselineOf="@+id/savee_button"
app:layout_constraintEnd_toStartOf="@+id/savee_button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/savee_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="88dp"
android:backgroundTint="@color/colorPrimaryDark"
android:text="@string/save_string"
android:textColor="@android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shoe_description" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
有人告诉我将它实现为一个片段,它应该可以工作。但我不确定如何。这是片段
class ShoeDetailsFragment : Fragment() {
private val viewModel: ActivityViewModel by activityViewModels()
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View? {
val binding: FragmentShoeDetailsBinding = DataBindingUtil.inflate(
inflater,R.layout.fragment_shoe_details,container,false
)
//initializing the button and clearing the views once canceled
binding.cancelButton.setOnClickListener { v: View ->
v.findNavController().navigateUp()
binding.shoeName.text.clear()
binding.shoeSize.text.clear()
binding.companyName.text.clear()
binding.shoeDescription.text.clear()
}
//initializing the button and saving the info to transfer to the shoeList
binding.saveeButton.setOnClickListener { v: View ->
v.findNavController().navigateUp()
val name = shoe_name.text.toString()
val size = shoe_size.text.toString()
val brand = company_name.text.toString()
val details = shoe_description.text.toString()
viewModel.addShoe(name,size,brand,details)
}
return binding.root
}
}
我对初始化绑定属性的任何想法持开放态度,因此我可以在布局和片段中使用它。还是我看错了?
附言XML 文件在此片段中表示
解决方法
我想我也为我的 NanoDegree 做过这个项目,你的代码给我留下了深刻的印象。
在我的 ViewModel
中,我为每个 EditText
创建了 3 个变量
-
MutableLiveData - 更新 viewModel 中的值
-
LiveData 在 viewModel 之外公开值,例如在一个片段中(你真的不需要这个)
-
公共变量,用于监控 MutableLiveData 的值并将其公开给您的 xml,从而实现 2 向绑定。
然后我将创建一个 Shared ViewModel 以在 ShoeDetailsFragment
和 ShoeListingFragment
之间共享数据。
在 SharedViewModel 内部
我为每个 EditText 创建了 3 个变量(这只是前 2 个 EditText)
class MySharedViewModel : ViewModel() {
private val _name = MutableLiveData<String>()
val name: LiveData<String>
get() = _name
var edShoeName = ""
private val _size = MutableLiveData<Double>()
val size: LiveData<Double>
get() = _size
var edSize = ""
......}
对于 xml,我完全按照您所做的做了,但是使用了 3rd 变量用于 2-Way Data Binding
<EditText
android:id="@+id/shoe_name"
style="@style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:hint="@string/shoe_name_string"
android:inputType="text"
android:textSize="30sp"
android:text="@={mySharedViewModel.edShoeName}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title_detail_view" />
<EditText
android:id="@+id/shoe_size"
style="@style/login_style"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/size_string"
android:inputType="number|numberDecimal"
android:textSize="15sp"
android:text="@={mySharedViewModel.edCompany}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shoe_name" />
我看到您在 ShoeDetailFragment
代码中包含了这行代码
binding.saveeButton.setOnClickListener { v: View -> ....}
就我而言,我是在 SharedViewModel 中完成的。
fun onSaveButtonClick() {
//check value entered for size and set it to 0 if blank
if (edSize == "") {
edSize = "0"
}
//update MutableLiveData with values read live from the EditText
_name.value = edShoeName
_size.value = edSize.toDouble()
//save shoeObject to the _shoeList MutableLiveData
_shoeList.value?.add(Shoe(edShoeName,edSize.toDouble(),edCompany,edDescription))
}
使用 DataBinding 我将 onClick 位移动到 xml
<Button
android:id="@+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="75dp"
android:layout_marginBottom="75dp"
android:background="@drawable/custom_button_background"
android:onClick="@{()->sharedViewModel.onSaveButtonClick()}"
祝您的项目一切顺利,you can also refer to my project if need be
快乐的编程伙伴
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。