使用数据绑定在自定义视图中调用自定义方法
问题描述:
使用数据绑定,我将如何引用自定义视图内的自定义方法?我试图把我的自定义视图中的监听器...使用数据绑定在自定义视图中调用自定义方法
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);
}
}
答
您可以通过使用自定义视图的布局本身具有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);
答
我实现了在取色(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 = new EventHandler();
binding.setEventCallBack(eventHandler);
+1
此代码几乎与[George Mounts博客文章](https://medium.com/google-developers/android-data-binding-2-way-your-way-ccac20f6313)有关双向数据绑定的精确副本相同,以防你需要更多信息。 :) – yennsarah
'binding.myCustomView.setVisibilityListener(this);' - 你试过了吗?有没有任何错误..?我可以看到我的自定义视图中声明的方法。 – yennsarah