gpt4 book ai didi

android - 如何使用 livedata 进行数据绑定(bind)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:29:51 33 4
gpt4 key购买 nike

如何使用 livedata 进行数据绑定(bind)?

activity_user_detail.xml:

<data>
<variable
name="viewModel"
type="com.test.viewmodel.UserViewModel" />
</data>
<TextView
android:id="@+id/tv_amount"
android:layout_width="match_parent"
android:text="@{viewModel.age}"
....

用户 View 模型.java:

public class UserViewModel extends ViewModel {
public LiveData<User> user;
public void getUserById(UserDao userDao, String userId){
transaction = UserDao .load(userId);
}
}

用户道.java:

@Query("SELECT * FROM `user` WHERE id = :userId")
LiveData<User> load(String userId);

UserDetailActivity.java:

private ActivityUserDetailBinding binding;
binding = DataBindingUtil.setContentView(this, R.layout.activity_user_detail);
viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
viewModel.getUserById(userDao, userId);
viewModel.user.observe(this, user -> binding.setViewModel(user)); // How to bind livedata?

我也试过这个:

binding.setViewModel(viewModel);

最佳答案

这是一个示例,用于了解 LiveData 和 ObservableField 的工作原理。您需要使用 LiveData 更改 T 对象和 setValue(),或使用 ObservableField 更改 set(T)。更改对象 T 的属性不会更新 UI。

<?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.tutorial3livedata_databinding2objects.UserViewModel" />
</data>

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

<TextView
android:id="@+id/user_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text='@{viewModel.userMutableLiveData.name+ ", email " + viewModel.userMutableLiveData.email}'
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.1" />

<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
android:text="@={viewModel.userMutableLiveData.name}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/user_info" />

<EditText
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="28dp"
android:text="@={viewModel.userMutableLiveData.email}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_name" />

<Button
android:id="@+id/button_change_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:onClick="@{() -> viewModel.changeUserName()}"
android:text="Change Name"
app:layout_constraintEnd_toStartOf="@+id/button_change_user"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_email" />

<Button
android:id="@+id/button_change_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="@{() -> viewModel.changeUser()}"
android:text="Change User"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button_change_name"
app:layout_constraintTop_toTopOf="@+id/button_change_name" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:text="Display User"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_change_name" />

</android.support.constraint.ConstraintLayout>
</layout>

View 模型

public class UserViewModel extends ViewModel {

public MutableLiveData<User> userMutableLiveData = new MutableLiveData<>();
private User mUser;

public UserViewModel() {
mUser = new User("User", "asd@abc.com");
userMutableLiveData.setValue(mUser);

}

public void changeUserName() {

// Both updates LiveData but does not update UI
mUser.setName("Name is Changed");
// userMutableLiveData.getValue().setName("Updated Name");

// This one Updates UI
// userMutableLiveData.setValue(userMutableLiveData.getValue());
}

public void changeUser() {
mUser = new User("New User Name", "myemail@mail.com");
// Without setting new value UI is not updated and observe is not called
userMutableLiveData.setValue(mUser);
}
}

主要 Activity

/*
Without binding.setLifecycleOwner(this), liveData.setValue() does not update UI
liveData.setValue() updates UI
EditText changes LiveData but does not update UI
*/
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
UserViewModel userViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
// LiveData should call setValue() to update UI via binding
binding.setViewModel(userViewModel);

// Required to update UI with LiveData
binding.setLifecycleOwner(this);
}
}

此代码用于学习目的。

您可以从此代码获得的结果:

1- 使用 changeUserName() 更改用户名会更新 LiveData 现有用户的名称,但不会更新 UI。当您旋转设备时,UI 会更新。

2- 当您更改 LiveData 的 User 和 setValue() 数据绑定(bind)时。

3- 使用 EditText 2 向绑定(bind)更改用户属性用户仍然相同。

关于android - 如何使用 livedata 进行数据绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51288066/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com