Using Data Binding, how would I reference a custom method inside a custom view? I'm attempting to set a listener inside my custom view...
public class MyCustomView extends LinearLayout {
//...view init stuff
private VisibilityListener mListener;
public void setVisibilityListener(VisibilityListener listener) {
mListener = listener;
}
public interface VisibilityListener {
void onVisibilityChanged(boolean isVisible);
}
}
public class MainActivity extends AppCompatActivity implements VisibilityListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate();
MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
// What I'm trying to accomplish...
binding.myCustomView.setVisibilityListener(this);
}
}
I implemented a simple colorChangeListener(CustomListener) in an colorPicker (CustomView). This might help you to solve your problem
ColorPicker.java
public class ColorPicker extends View {
private ArrayList<OnColorChangeListener> mColorChangeListeners = new ArrayList<>();
private int mColor;
public ColorPicker(Context context) {
super(context);
}
public ColorPicker(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ColorPicker(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public ColorPicker(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public void setColor(int color){
mColor = color;
setBackgroundColor(mColor);
if (mColorChangeListeners != null && !mColorChangeListeners.isEmpty()){
for (OnColorChangeListener onColorChangeListener: mColorChangeListeners) {
onColorChangeListener.onColorChangeListener(this,mColor);
}
}
}
public int getColor() {
return mColor;
}
public void removeListener(OnColorChangeListener oldListener) {
mColorChangeListeners.remove(oldListener);
}
public void addListener(OnColorChangeListener newListener) {
mColorChangeListeners.add(newListener);
}
public interface OnColorChangeListener {
void onColorChangeListener(ColorPicker colorPicker, int newColor);
}
}
BindingAdapter:
@BindingAdapter(value={"event:onColorChange", "colorAttrChanged"}, requireAll = false)
public static void setColorChangeListener(ColorPicker view,
final ColorPicker.OnColorChangeListener onColorChangeListener,
final InverseBindingListener inverseBindingListener) {
ColorPicker.OnColorChangeListener newListener;
if (inverseBindingListener == null) {
newListener = onColorChangeListener;
} else {
newListener = new ColorPicker.OnColorChangeListener() {
@Override
public void onColorChangeListener(ColorPicker colorPicker,
int newColor) {
if (onColorChangeListener != null) {
onColorChangeListener.onColorChangeListener(colorPicker,
newColor);
}
inverseBindingListener.onChange();
}
};
}
ColorPicker.OnColorChangeListener oldListener =
ListenerUtil.trackListener(view, newListener,
R.id.onColorChangedListner);
if (oldListener != null) {
view.removeListener(oldListener);
}
if (newListener != null) {
view.addListener(newListener);
}
}
XML:
<variable
name="eventCallBack"
type="bytes.wit.databinding.HomeActivity.EventHandler"/>
<bytes.wit.databinding.ColorPicker
android:id="@+id/color_picker"
android:layout_width="0dp"
android:layout_height="24dp"
app:color="@={placeModel.color}"
app:onColorChange="@{(v, color) -> eventCallBack.onColorChanged(placeModel.color)}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
EventHandler
public class EventHandler{
public void onColorChanged(int color){
Toast.makeText(HomeActivity.this,"Color "+color,Toast.LENGTH_SHORT).show();
}
}
Setting EventHandler
eventHandler = new EventHandler();
binding.setEventCallBack(eventHandler);
You can achieve exactly this by using your custom view in the layout itself with an id.
<package.of.MyCustomView
android:id="@+id/my_custom_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Like this data bindings will define a member myCustomView of type MyCustomView within your binding. And thus you can simply call setVisibilityListener() on it.
binding.myCustomView.setVisibilityListener(this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With