UI没有更新Android Databinding + ViewModel



我试图做一个非常简单的应用程序使用MVVM android架构。基本上我编辑的是"EditText"文本应该显示在textview上。然而"TextView"没有得到更新(它的空白文本,所以它不显示)

我使用databinding + ViewModel + LiveData

但是当属性改变时,UI并没有改变

代码

public class Fragment1ViewModel extends ViewModel {
MutableLiveData<String> name = new MutableLiveData<String>("test");
public MutableLiveData<String> getName() {
return name;
}
public void setName(MutableLiveData<String> name) {
this.name = name;
}

}

Fragment1.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="viewmodel" type="com.example.viewmodel2practice.Fragment1ViewModel" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment1">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{viewmodel.name}"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.159"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.157" />
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="19dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="17dp"
android:layout_marginBottom="468dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@={viewmodel.name}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.777"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
</layout>

Fragment1.java

package com.example.viewmodel2practice;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.viewmodel2practice.databinding.Fragment1Binding;
/**
* A simple {@link Fragment} subclass.
* Use the {@link Fragment1#newInstance} factory method to
* create an instance of this fragment.
*/
public class Fragment1 extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private Fragment1Binding binding;
private Fragment1ViewModel state;
public Fragment1() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Fragment1.
*/
// TODO: Rename and change types and number of parameters
public static Fragment1 newInstance(String param1, String param2) {
Fragment1 fragment = new Fragment1();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}

}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = Fragment1Binding.inflate(inflater,container, false);
binding.setLifecycleOwner(this);
state= new ViewModelProvider(this).get(Fragment1ViewModel.class);
binding.setViewmodel(state);
// Inflate the layout for this fragment
return binding.getRoot();
}
}

有什么问题吗?谢谢你

我认为问题在于如何在视图模型中操纵可变的实时数据。对于LiveData,您需要更新其中的值,而不是对象本身,因此,而不是这样做:

public void setName(MutableLiveData<String> name) {
this.name = name;
}

这样做是为了更新字符串值:

public void setName(String name) {
// you can use postValue or setValue depending on what you need
this.name.postValue(name);
}

相关内容

  • 没有找到相关文章