基于Android DataBinding的InverseBindingMethod改进SeekBar双向绑定(正向/反向)数据绑定
在前文《使用Android BindingAdapter与InverseBindingAdapter实现SeekBar双向(正向/反向)数据绑定》(链接:http://blog.csdn.net/zhangphil/article/details/77839555 )基础上,用InverseBindingMethod改进双向绑定实现方案。
老规矩,先建立数据模型,PhilProgress.java:
package zhangphil.test; import android.databinding.BaseObservable; import android.databinding.ObservableInt; /** * Created by Phil on 2017/9/4. */ public class PhilProgress extends BaseObservable { public ObservableInt porgress = new ObservableInt(); }
重写SeekBar,PhilSeekBar.java:
package zhangphil.test; import android.content.Context; import android.databinding.InverseBindingListener; import android.databinding.InverseBindingMethod; import android.databinding.InverseBindingMethods; import android.util.AttributeSet; import android.util.Log; import android.widget.SeekBar; /** * Created by Phil on 2017/9/4. */ @InverseBindingMethods({@InverseBindingMethod(type = PhilSeekBar.class, attribute = "progress", event = "progressAttrChanged")}) public class PhilSeekBar extends SeekBar { private String TAG = "调试输出"; private int progress = 0; private 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) { progress = i; //触发反向数据的传递 if (mInverseBindingListener != null) { mInverseBindingListener.onChange(); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } public void setProgress(int p) { Log.d(TAG, "setProgress:" + p); if (progress != p) { progress = p; //这一句代码可以解决初始化阶段进度条显示的值正确,但没有及时更新UI的问题。 //super.setProgress(progress); } } public int getProgress() { Log.d(TAG, "getProgress:" + progress); return progress; } public void setProgressAttrChanged(InverseBindingListener inverseBindingListener) { if (inverseBindingListener != null) { mInverseBindingListener = inverseBindingListener; } } }
写布局activity_main.xml使用PhilSeekBar:
<?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.PhilProgress" /> </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:progress="@={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>
测试的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); final PhilProgress progress = new PhilProgress(); binding.setProgress(progress); //设置一个初始值作为演示 数据 -> View //这是最常见的进度设置。 progress.porgress.set(21); } }
代码初始运行结果:
注意到虽然进度条显示到了21,但是却没有彩色着色,在PhilSeekBar的setProgress方法里面,启用super.setProgress可以解决这个问题。
手指拖动后的结果显示:
作者:zhangphil 发表于2017/9/6 14:02:41 原文链接
阅读:86 评论:0 查看评论