首页 > 解决方案 > 使用 notifyPropertyChanged(BR.xx) 时 DataBinding 不更新 TextView

问题描述

我有一个用于从 XML 调用的函数的工作数据绑定(即“@{user.mUCL}”)。但是当我在任何方法或任何地方使用 notifyPropertyChanged(BR.mUCL) 新值没有得到更新时,我在下面提供了一个示例问题,其中 for-loop 使用值更新了 Text1,但其他 Text2 无法使用相同的值更新“不是-工作”。

Java片段

   public class FragmentTwo extends Fragment {

    Update_observable updateObservable;
    TextView fragment_quote_open_val;

    public FragmentTwo() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        Fragment2Binding fragmentTwoBinding = DataBindingUtil.inflate(inflater,R.layout.fragment2,null,false);
        View view = fragmentTwoBinding.getRoot();
        updateObservable = new Update_observable();
        fragmentTwoBinding.setUser(updateObservable);

        fragment_quote_open_val=(TextView)view.findViewById(R.id.fragment_quote_open_val);

        bindingLoop();

        return view;
    }

    @BindingAdapter({"mUCL"})
    public static void runMe(TextView view, String message) {
        if (message != null)
            view.setText(message);
    }


    public void bindingLoop(){

        final int[] k = {0};

        final Update_observable ss=new Update_observable();

        final Handler handler = new Handler();
        final int delay = 3000; //milliseconds

        handler.postDelayed(new Runnable(){
            public void run(){
                //do something
                k[0]++;
                fragment_quote_open_val.setText(String.valueOf(k[0]));
                ss.setmUCL(String.valueOf(k[0]));

                handler.postDelayed(this, delay);
            }
        }, delay);
    }
}

BaseObservable

 public class Update_observable extends BaseObservable {

    public String mUCL= "not-worked";

    @Bindable
    public String getmUCL() {
        return this.mUCL;
    }

    public void setmUCL(String mUCL) {
        this.mUCL = mUCL;
        notifyPropertyChanged(BR.mUCL);
    }

}

XML

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:bind="http://schemas.android.com/apk/res-auto">

<data>

    <variable
        name="user"
        type="com.journaldev.androidmvvmbasics.fragments.Update_observable"/>

</data>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff04"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.journaldev.androidmvvmbasics.fragments.FragmentTwo">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/fragment_quote_open_val"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:textColor="#000000"
        android:textSize="16dp" />
    <TextView
        android:id="@+id/fragment_quote_ucl_val"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{user.mUCL}"
        android:layout_margin="20dp"
        bind:mUCL="@{user.mUCL}"
        android:textColor="#000000"
        android:textSize="16dp" />
</LinearLayout>

</layout>

标签: javaandroiddata-binding

解决方案


请确保您在 Base 类中执行 DataBindingUtil.inflate。

Fragment1Binding fragmentOneBinding = DataBindingUtil.inflate(inflater,R.layout.fragment1,null,false);
 View view = fragmentOneBinding.getRoot();
 fragmentOneBinding.setUsermodel(new UserModel("XXXX","XXXX"));

或者

 ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, 
 R.layout.activity_main);
  activityMainBinding.setUser(new User());
  activityMainBinding.executePendingBindings();

然后在BaseObservable class

@Bindable
    public String mEmail= "....";

重建项目,只是为了确保确定@Bindable完成

现在打电话,根据您的要求。

    notifyPropertyChanged(BR.xxx);

推荐阅读