【问题标题】:Unable to Update text from fragment in data binding无法从数据绑定中的片段更新文本
【发布时间】:2018-05-05 20:30:16
【问题描述】:
<?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>
 <import type="com.demo.Model" />
 <variable
            name="model"
            type="Model" />
</data>
  <Textview
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"                                                          
    android:backgroundTint="@color/colorLableBg"
    android:ems="10"                                                                           
    android:text="@={model.name}"
    android:textCursorDrawable="@null/>
</layout>

这里是片段

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_authentication, container, false);
    Model model = getModel()// get model return model data from server
    binding.setModel(model);
    binding.text.setText("This is demo")
    }

我从 api 调用中获取数据并设置 model.everything 以防万一,但是当我设置文本时,它注意到会发生变化。

【问题讨论】:

    标签: android data-binding


    【解决方案1】:

    您需要在设置模型后设置该值,否则值将被覆盖。

     @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            binding = DataBindingUtil.inflate(inflater, R.layout.fragment_authentication, container, false);
            Model model = getModel()// get model return model data from server
    
            binding.setModel(model);
            binding.getModel.setName("this is demo");
    
            }
    

    【讨论】:

      【解决方案2】:

      你可以试试:

      Model model = getModel()// get model return model data from server
      binding.setModel(model);
      model.name.set("This is demo")
      

      在您的模型中确保名称为:

      public ObservableField<String> name = new ObservableField<>();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多