Java LiveData对象在XML中是否可见?

Java LiveData对象在XML中是否可见?,java,android,kotlin,android-databinding,android-livedata,Java,Android,Kotlin,Android Databinding,Android Livedata,当我将observefield对象绑定到XML视图时,通过set()对值的更改会立即反映在视图中。但是,当我在XML中绑定LiveData对象时,会呈现初始值,但通过value=进行的更改对视图没有影响。它们被传递给科特林的观察员 我假设LiveData的工作方式类似于XML绑定中的Observable*类。不是这样吗?如果我需要在XML和Kotlin中观察一个值,那么我真的需要创建两个观察值吗?您可以使用数据绑定 通过数据绑定,当LiveData发生更改时,您的xml将收到通知。您还可以将观察

当我将
observefield
对象绑定到XML视图时,通过
set()
对值的更改会立即反映在视图中。但是,当我在XML中绑定
LiveData
对象时,会呈现初始值,但通过
value=
进行的更改对视图没有影响。它们被传递给科特林的观察员


我假设
LiveData
的工作方式类似于XML绑定中的
Observable*
类。不是这样吗?如果我需要在XML和Kotlin中观察一个值,那么我真的需要创建两个观察值吗?

您可以使用数据绑定

通过数据绑定,当LiveData发生更改时,您的xml将收到通知。您还可以将观察者附加到java代码中的相同实时数据


希望这有帮助

您可以使用数据绑定

通过数据绑定,当LiveData发生更改时,您的xml将收到通知。您还可以将观察者附加到java代码中的相同实时数据


希望这有帮助

这对我来说是通过数据绑定实现的,我假设您正在使用数据绑定

您没有提供代码,因此我只能猜测您可能没有对绑定对象调用
setLifecycleOwner()
(例如,
ActivityMainBinding
用于
activity\u main
布局资源)。否则,数据绑定将无法注册观察者

显示在
TextView
上使用
android:text=“@{viewModel.sensorLiveData}”
。在使用此布局的活动中,我使用
setLifecycleOwner()
教授有关我的
片段活动的绑定:

/***
  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());
  }
}

它的工作原理与champ类似,假设您的设备有一个工作的环境光传感器。

这对我来说是通过数据绑定工作的,我假设您正在使用数据绑定

您没有提供代码,因此我只能猜测您可能没有对绑定对象调用
setLifecycleOwner()
(例如,
ActivityMainBinding
用于
activity\u main
布局资源)。否则,数据绑定将无法注册观察者

显示在
TextView
上使用
android:text=“@{viewModel.sensorLiveData}”
。在使用此布局的活动中,我使用
setLifecycleOwner()
教授有关我的
片段活动的绑定:

/***
  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());
  }
}
假设你的设备有一个可以工作的环境光传感器,它的工作原理就像一个champ