使用Android BindingAdapter与InverseBindingAdapter实现SeekBar双向(正向/反向)数据绑定
在我之前写的系列文章中,继续深化Android数据绑定技术的使用。
结合常用的SeekBar,实现用Android DataBinding数据绑定技术,设置从数据模型的值修改SeekBar的进度,以及实现常见的SeekBar拖动时候的进度回写到数据model中。也就是说,当用户手动拖动SeekBar这一View时候,View产生的结果,回写到我们建立的model中。
先任意建立一个数据模型Progress.java:
package zhangphil.test;
import android.databinding.BaseObservable;
import android.databinding.ObservableInt;
/**
* Created by Phil on 2017/9/4.
*/
public class Progress extends BaseObservable {
public final ObservableInt porgress = new ObservableInt();
}
写布局,activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="progress"
type="zhangphil.test.Progress" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<zhangphil.test.PhilSeekBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="30dp"
app:philprogress="@={progress.porgress}" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@{String.valueOf(progress.porgress)}"
android:textColor="@android:color/holo_blue_light"
android:textSize="50dp" />
</LinearLayout>
</layout>
关键的PhilSeekBar.java:
package zhangphil.test;
import android.content.Context;
import android.databinding.BindingAdapter;
import android.databinding.InverseBindingAdapter;
import android.databinding.InverseBindingListener;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.SeekBar;
/**
* Created by Phil on 2017/9/4.
*/
public class PhilSeekBar extends SeekBar {
private static InverseBindingListener mInverseBindingListener;
public PhilSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
//触发反向数据的传递
if (mInverseBindingListener != null) {
mInverseBindingListener.onChange();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
@BindingAdapter(value = "philprogress", requireAll = false)
public static void setPhilProgress(PhilSeekBar seekBar, int progress) {
if (getPhilProgress(seekBar) != progress) {
seekBar.setProgress(progress);
}
}
@InverseBindingAdapter(attribute = "philprogress", event = "philprogressAttrChanged")
public static int getPhilProgress(PhilSeekBar seekBar) {
return seekBar.getProgress();
}
@BindingAdapter(value = {"philprogressAttrChanged"}, requireAll = false)
public static void setPhilProgressAttrChanged(PhilSeekBar seekBar, InverseBindingListener inverseBindingListener) {
if (inverseBindingListener == null) {
Log.e("错误!", "InverseBindingListener为空!");
} else {
mInverseBindingListener = inverseBindingListener;
}
}
}
测试的MainActivity.java:
package zhangphil.test;
import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import zhangphil.test.databinding.ActivityMainBinding;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
Progress progress = new Progress();
binding.setProgress(progress);
//设置一个初始值作为演示 数据 -> View
//这是最常见的进度设置。
progress.porgress.set(21);
}
}
代码运行结果,初始化:
当手指拖动SeekBar时候,进度值回写到Progress的progress中,也再TextView中得到反应: