j2e*_*nue 2 data-binding android 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的精彩回答我写了一篇关于dataBinding的博客来帮助别人.
更新绑定当然需要某种通知.
这是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);
   }
}
| 归档时间: | 
 | 
| 查看次数: | 9031 次 | 
| 最近记录: |