LiveData和ViewModel不会更新textview

时间:2018-05-07 11:48:11

标签: android runnable android-databinding android-livedata

我遇到一个小问题,当我在 onChanged 中注释 binding.setData(dataContainer); ,同时在我的Activity中观察Livedata时,我无法更新UI,当我取消注释时,UI会更新。 如果您有需要,请指导并做一些代码审查。

我有一个每x秒后重复运行的Runnable。这是我在我的活动中观察到的Livedata。

感谢。

public class MainActivity extends AppCompatActivity {

  TextOnScreen dataContainer;
  ActivityMainBinding binding;
  ViewModelClass modelClass;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    modelClass = ViewModelProviders.of(this).get(ViewModelClass.class);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    dataContainer = new TextOnScreen("hello");
    binding.setData(dataContainer);

    modelClass.getLiveData().observe(this, new Observer<String>() {
      @Override
      public void onChanged(String s) {
        Log.e("TAG", "--onChanged--" + s);
        dataContainer.onscreen.set(s);

        // this line allows the textview to update
        // other wise no change on UI is seen
        binding.setData(dataContainer);
      }
    });
  }
}


public class TextOnScreen {

  public final ObservableField<String> onscreen=new ObservableField<>();

  public TextOnScreen(String t) {
    onscreen.set(t);
  }

  public String getOnscreen() {
    return onscreen.get();
  }

  public void setOnscreen(String onscreen) {
    this.onscreen.set(onscreen);
  }
}


    public class ViewModelClass extends ViewModel{

      private MutableLiveData<String> liveData;

      public ViewModelClass() {
        ModelClass modelClass = new ModelClass();
        liveData= modelClass.generateName();
      }

      public LiveData<String> getLiveData() {
        return liveData;
      }
    }


public class ModelClass {

  MutableLiveData<String > data =new MutableLiveData<>();


  MutableLiveData<String> generateName(){
    final android.os.Handler handler=new android.os.Handler();
    Runnable runnable=new Runnable() {
      @Override
      public void run() {

        data.setValue(data.getValue()+"*");
        handler.postDelayed(this,1000);
      }
    };
    handler.postDelayed(runnable,2000);
    return data;
  }

}

<?xml version="1.0" encoding="utf-8"?>
<layout>
  <data>
    <variable
      name="data"
      type="dsfa.drish.com.livedatasample.TextOnScreen"/>
  </data>
<android.support.constraint.ConstraintLayout
  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"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{data.onscreen}"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

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

3 个答案:

答案 0 :(得分:22)

对我来说,它只是丢失了:

binding.setLifecycleOwner(this);

答案 1 :(得分:2)

通过删除 TextOnScreen 并将 MainActivity 修改为:

,我可以在保留大部分代码的同时获得相同的预期效果
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewModelClass modelClass = ViewModelProviders.of(this).get(ViewModelClass.class);

    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

    binding.setData(modelClass); // <-- set the ViewModel to be binded
    binding.setLifecycleOwner(this); // <-- this enables MutableLiveData to be update on your UI

    modelClass.getLiveData().setValue("hello");

    modelClass.getLiveData().observe(this, new Observer<String>() {
        @Override
        public void onChanged(String s) {
            Log.e("TAG", "--onChanged--" + s);
        }
    });
}

以及 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
    <variable
        name="data"
        type="com.rafaelfukuda.stackoverflow.ViewModelClass"/>
</data>
    <android.support.constraint.ConstraintLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{data.liveData}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    </android.support.constraint.ConstraintLayout>
</layout> 

其他代码改进:

我会保持模型非常简单(没有MutableLiveData属性的POJO)并将 generateName 移动到ViewModel。这将保持适当的 MVVM 结构,并且将来很容易理解和扩展您的代码。

<强>结果:

Screen record of the desired result

答案 2 :(得分:0)

如果你在 Fragment 中使用了视图绑定,请检查你是否在 onCreateView 方法中没有忘记返回

return binding.root

不是一个新的膨胀视图实例。