使用notifyPropertyChanged(BR.xx)时,DataBinding不更新TextView

时间:2018-12-11 13:17:10

标签: java android data-binding

我有一个有效的dataBinding用于从XML调用的函数(即“ @ {user.mUCL}”)。但是,当我在任何方法或任何地方使用notifyPropertyChanged(BR.mUCL)时,都不会更新新值,我在下面提供了一个示例问题,其中for循环使用值更新了Text1,但是其他Text2无法使用相同的值来更新“不是-worked”。

JavaFragment

   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>

2 个答案:

答案 0 :(得分:0)

由于无法以编程方式完成数据绑定,因此我设法找到了答案。

public class Update_observable extends BaseObservable {

public String mUCL= "worked";

public Update_observable(String mUCL){
    this.mUCL=mUCL;
}

片段

 public void bindingLoop(){

        final int[] k = {0};


        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]));
                updateObservable = new Update_observable(String.valueOf(k[0]));
                fragmentTwoBinding.setUser(updateObservable);
                handler.postDelayed(this, delay);
            }
        }, delay);
    }

答案 1 :(得分:0)

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

ID | Product Type | curr_quantity
0  | Prod A       | 0
1  | Prod B       | 70
2  | Prod C       | 40 
3  | Prod A       | 0
4  | Prod B       | 90
5  | Prod C       | 10

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