Skip to content

Instantly share code, notes, and snippets.

@atasoglou
Last active February 24, 2019 18:12
Show Gist options
  • Save atasoglou/40924f2e5716dac0cfd20eae620ce30f to your computer and use it in GitHub Desktop.
Save atasoglou/40924f2e5716dac0cfd20eae620ce30f to your computer and use it in GitHub Desktop.
BubbleSeekBar with some Data-Binding support
import android.content.Context;
import android.util.AttributeSet;
import com.xw.repo.BubbleSeekBar;
import androidx.databinding.BindingAdapter;
import androidx.databinding.BindingMethod;
import androidx.databinding.BindingMethods;
import androidx.databinding.InverseBindingListener;
import androidx.databinding.InverseBindingMethod;
import androidx.databinding.InverseBindingMethods;
@BindingMethods({
@BindingMethod(
type = MySeekBar.class,
attribute = "app:bsb_enable",
method = "setEnabled"
)
})
@InverseBindingMethods({
@InverseBindingMethod(
type = MySeekBar.class,
attribute = "app:bsb_progress",
method = "getProgress", // public method of superclass BubbleSeekBar
event = "app:progressAttrChanged"
)
})
public class MySeekBar extends BubbleSeekBar {
public MySeekBar(Context context) {
super(context);
}
public MySeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MySeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
/**
* app:bsb_progress
*
* Progress two way data-biding
* In the layout xml use this as: app:bsb_progress="@={...}"
*/
@BindingAdapter(value = "app:bsb_progress", requireAll = false)
public static void setProgressBinding(MySeekBar bar, int progress) {
bar.setProgress(progress);
}
@BindingAdapter(value = "app:progressAttrChanged", requireAll = false)
public static void setProgressAtrrEventListener(MySeekBar bar, final InverseBindingListener listener) {
if (listener != null) {
bar.setOnProgressChangedListener(new OnProgressChangedListener() {
@Override
public void onProgressChanged(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
// If you use listener.onChange() here, it's same as using android:onProgressChanged
}
@Override
public void getProgressOnActionUp(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
listener.onChange();
}
@Override
public void getProgressOnFinally(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {/*empty*/}
});
}
}
/**
* app:bsb_enable
*
* Enable (View) one way data-biding
* In the layout xml use this as: app:bsb_enable="@{...}"
*/
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
setTrackColor(getResources().getColor(R.color.lightGray));
if (!enabled) {
setBubbleColor(getResources().getColor(R.color.lightGray));
setThumbColor(getResources().getColor(R.color.lightGray));
setSecondTrackColor(getResources().getColor(R.color.lightGray));
} else {
setBubbleColor(getResources().getColor(R.color.colorAccent));
setThumbColor(getResources().getColor(R.color.colorAccent));
setSecondTrackColor(getResources().getColor(R.color.colorAccent));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment