使用数据绑定(bind),我如何在自定义 View 中引用自定义方法?我正在尝试在我的自定义 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);
}
}
最佳答案
我在 colorPicker (CustomView) 中实现了一个简单的 colorChangeListener(CustomListener)。这可能会帮助您解决问题
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);
}
}
绑定(bind)适配器:
@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" />
事件处理器
public class EventHandler{
public void onColorChanged(int color){
Toast.makeText(HomeActivity.this,"Color "+color,Toast.LENGTH_SHORT).show();
}
}
设置事件处理器
eventHandler = new EventHandler();
binding.setEventCallBack(eventHandler);
关于android - 使用数据绑定(bind)在自定义 View 中调用自定义方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43220505/