Android dataBinding - 无法通知视图的更改

时间:2016-01-28 22:25:27

标签: android data-binding timer

我正在尝试将数据绑定用于某个目的。我创建了一个启动倒数计时器的项目。如果时间是素数,它应该在xml布局中更新TextView。计时器工作正常,但textview永远不会更新。

这是我的计时器工作正常:

public class MyCountDownTimer {
private CountDownTimer countDownTimer;
private boolean isExecuting =false;
private ICountDownListener listener;
private final String TAG = getClass().getSimpleName();

public MyCountDownTimer(ICountDownListener listener){
this.listener = listener;
}

public void startTimer(long timeLeftMillis) {
if (!isExecuting) {
    isExecuting = true;
    countDownTimer = new CountDownTimer(timeLeftMillis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
           ;
           if(isPrime(millisUntilFinished/1000)){
            listener.doSomethingWithPrimeCountDown(millisUntilFinished / 1000);

           }
        }

        @Override
        public void onFinish() {
            isExecuting = false;
            listener.doSomethingWithPrimeCountDown(0L);
        }
    };
    countDownTimer.start();
} else {
    Log.i(TAG, "Timer already started");
}

} M

public void cancelTimer() {
    if (isExecuting) {
        countDownTimer.cancel();
        isExecuting = false;
    }
}

public void restartTimer(Long milli) {
    cancelTimer();
    startTimer(milli);
}

//checks whether an int is prime or not.
boolean  isPrime(Long n) {
    //check if n is a multiple of 2
    if (n%2==0) return false;
    //if not, then just check the odds
    for(int i=3;i*i<=n;i+=2) {
        if(n%i==0)
            return false;
    }
    return true;
}

}

我有一个界面,用于将计时器滴答发送给正在收听的人:

public interface ICountDownListener {
void doSomethingWithPrimeCountDown(Long count);

}

和我实际绑定到数据的主Activity类看起来像这样:

public class MainActivity extends FragmentActivity {
CountdownBinder mCountdownBinder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

   mCountdownBinder = DataBindingUtil.setContentView(this, R.layout.activity_main);

    //Lets reference our textview just for fun
    mCountdownBinder.tvGreen.setText("initial text");


     ViewModel vModel = ViewModel.instance();
//now tell databinding about your viewModel below
    mCountdownBinder.setViewModel(viewModel);
    vModel.startCounting(200000L); //start a countdown

}

}

这是我创建的ViewModel非常重要的部分。它是一个单例类,它扩展了BaseObservable并实现了ICountDownListener,因此它可以监听ticks:

    package com.databindingexample.mycompany.databindingexample.ViewModels;


import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.databinding.BindingAdapter;
import android.graphics.Color;
import android.util.Log;
import android.widget.TextView;

import com.databindingexample.mycompany.databindingexample.Interfaces.ICountDownListener;
import com.databindingexample.mycompany.databindingexample.MyCountDownTimer;

//notice we are subclassing BaseObservable
public class ViewModel extends BaseObservable implements ICountDownListener{

    private static ViewModel instance;
    private long countDownTime;
    private MyCountDownTimer mCountDownTimer;

    //lock the constructor as this is a singleton
    private ViewModel(){
        mCountDownTimer=new MyCountDownTimer(this);

    }

    public static ViewModel instance() {
        if (instance == null) {
            instance = new ViewModel();
        }
        return instance;
    }

    @Bindable
    public long getCountDownTime() {
        return countDownTime;
    }

    public void setCountDownTime(long countDownTime) {
        this.countDownTime = countDownTime;

        //this seems to not notify the UI, nothings changing
        notifyPropertyChanged((int) countDownTime);

        //this log prints out
        Log.d("TAG","prime tick:"+countDownTime);
    }

    @BindingAdapter({"app:primeColor"})
    public static void setTextColor(TextView view, String color) {

        if("green".equals(color))
            view.setTextColor(Color.parseColor("#63f421"));

        else  if("pink".equals(color))
            view.setTextColor(Color.parseColor("#ffc0cb"));
    }

    public void startCounting(Long milli){
        mCountDownTimer.restartTimer(milli);
    }

    @Override
    public void doSomethingWithPrimeCountDown(Long count) {
        setCountDownTime(count);
    }
}

注意:app:primeColor属性bindableAdapter正常工作只是可绑定的getCountdownTime()无法正常工作。

这是xml文件:

    <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data class="CountdownBinder">
        <variable name="viewModel" type="com.databindingexample.mycompany.databindingexample.ViewModels.ViewModel"/>
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.databindingexample.mycompany.databindingexample.MainActivity"
        tools:showIn="@layout/activity_main">

        <TextView
            android:id="@+id/tv_green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:primeColor='@{"pink"}'
            android:text="@{Long.toString(viewModel.getCountDownTime)}" />
    </RelativeLayout>

</layout>

更新: 感谢SO的精彩回答我写了blog on dataBinding来帮助别人。

1 个答案:

答案 0 :(得分:6)

更新绑定当然需要某种通知。

这是notifyPropertyChanged的用武之地。 每个@Bindable Annotated属性都会在BR类中生成一个id(如果你真的想要,可以改变这种行为)。

因此,您不会通过调用notifyPropertyChanged(BR.fieldName)来触发变量。

查看http://developer.android.com/tools/data-binding/guide.html或更高版本的文档,查看生成的代码,了解“魔法”的含义。的工作原理。

一个可观察的对象应该(或者可以,因为你可以完全独立于分配值来触发属性更改通知):

public class User extends BaseObservable {
   private String firstName;
   private String lastName;
   @Bindable
   public String getFirstName() {
       return this.firstName;
   }
   @Bindable
   public String getLastName() {
       return this.lastName;
   }
   public void setFirstName(String firstName) {
       this.firstName = firstName;
       notifyPropertyChanged(BR.firstName);
   }
   public void setLastName(String lastName) {
       this.lastName = lastName;
       notifyPropertyChanged(BR.lastName);
   }
}