使用数据绑定在自定义视图中调用自定义方法

时间:2017-04-05 01:51:41

标签: android android-databinding

使用数据绑定,如何在自定义视图中引用自定义方法?我正在尝试在自定义视图中设置一个监听器......

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);
    }
}

2 个答案:

答案 0 :(得分:2)

我在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);
    }
}

<强> 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" />

<强>事件处理程序

public class EventHandler{
        public void onColorChanged(int color){
            Toast.makeText(HomeActivity.this,"Color "+color,Toast.LENGTH_SHORT).show();
        }
    }

设置EventHandler

eventHandler = new EventHandler();
binding.setEventCallBack(eventHandler);

答案 1 :(得分:0)

您可以使用ID在布局中使用自定义视图来实现此目的。

<package.of.MyCustomView
        android:id="@+id/my_custom_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

与此类似,数据绑定将在绑定中定义类型为myCustomView的成员MyCustomView。因此,您只需在其上调用setVisibilityListener()即可。

binding.myCustomView.setVisibilityListener(this);