notifyPropertyChanged(BR.xx)を使用しているときのTextViewを更新していないデータバインディング

レイジーテディ:

私は(すなわち。「@ {user.mUCL}」)XMLから呼び出される機能のための作業のdataBindingを持っています。しかし、私は、任意の方法で(BR.mUCL)をnotifyPropertyChangedか、どこにでも新しい値が更新されますいけない使用する場合、私はforループは値を[テキスト1]を更新し、以下のサンプル問題を、提供してきたが、他のテキスト2は同じ値で更新に失敗「ではありません「-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>
マインドコントロール :

親切に必ず基本クラスで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 classDO

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

念のため、プロジェクトを再構築し@Bindable、確実に実行されます

今、あなたの要件ごとに、呼び出します。

    notifyPropertyChanged(BR.xxx);

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=220025&siteId=1