Are LiveData objects observable in XML?

删除回忆录丶 提交于 2019-12-11 15:37:50

问题


When I bind ObservableField<> objects to a view in XML, changes to the value via set() are immediately reflected in the view. When I bind LiveData<> objects in XML, however, the initial value is rendered but changes via value= have no effect on the view. They are passed to Kotlin observers.

I assumed LiveData would work like Observable* classes in XML bindings. Is that not the case? If I need to observe a value in both XML and Kotlin, do I really need to create two observables?


回答1:


You can make use of data-binding. https://developer.android.com/topic/libraries/data-binding/

With data binding, your xml will be notified when there is a change in your LiveData. You can also attach an observer to the same live data in your java code.

Hopefully this helps!




回答2:


This works for me via data binding, which I assume is what you are using.

You did not provide your code, so I can only guess that perhaps you are not calling setLifecycleOwner() on your binding object (e.g., ActivityMainBinding for an activity_main layout resource). Without that, data binding cannot register an observer.

This sample project shows a layout that uses android:text="@{viewModel.sensorLiveData}" on a TextView. In the activity that uses this layout, I use setLifecycleOwner() to teach the binding about my FragmentActivity:

/***
  Copyright (c) 2013-2017 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  Covered in detail in the book _Android's Architecture Components_
    https://commonsware.com/AndroidArch
 */

package com.commonsware.android.livedata;

import android.arch.lifecycle.ViewModelProviders;
import android.databinding.BindingAdapter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import com.commonsware.android.livedata.databinding.MainBinding;

public class MainActivity extends FragmentActivity {
  @BindingAdapter("android:text")
  public static void setLightReading(TextView tv, SensorLiveData.Event event) {
    if (event==null) {
      tv.setText(null);
    }
    else {
      tv.setText(String.format("%f", event.values[0]));
    }
  }

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

    MainBinding binding=MainBinding.inflate(getLayoutInflater());
    SensorViewModel vm=ViewModelProviders.of(this).get(SensorViewModel.class);

    binding.setViewModel(vm);
    binding.setLifecycleOwner(this);
    setContentView(binding.getRoot());
  }
}

And it works like a champ, assuming your device has an ambient light sensor that works.



来源:https://stackoverflow.com/questions/53658476/are-livedata-objects-observable-in-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!