【发布时间】:2018-12-27 03:11:21
【问题描述】:
我在 Xamarin.Forms 中实现了一个自定义可点击标签类以及一个自定义渲染器,它添加了一个 RippleDrawable 作为控件 Foreground。我正在使用以下代码创建RippleDrawable:
public static Drawable CreateRippleDrawable(Context context)
{
var typedValue = new TypedValue();
context.Theme.ResolveAttribute(Resource.Attribute.SelectableItemBackground, typedValue, true);
var rippleDrawable = context.Resources.GetDrawable(typedValue.ResourceId, context.Theme);
return rippleDrawable;
}
在我的自定义渲染器中,我分配了可绘制对象
this.Control.Foreground = DrawableHelper.CreateRippleDrawable(this.Context);
并在用户触摸控件时更新波纹
private void LinkLabelRenderer_Touch(object sender, TouchEventArgs e)
{
if (e.Event.Action == MotionEventActions.Down)
{
this.Pressed = true;
}
if (e.Event.Action == MotionEventActions.Cancel)
{
this.Pressed = false;
}
if (e.Event.Action == MotionEventActions.Up)
{
this.Ripple.SetHotspot(e.Event.GetX(), e.Event.GetY());
this.Pressed = false;
// raise the event of the Xamarin.Forms control
}
}
现在,每当我单击控件时,都会显示波纹,这是预期的行为,但如果我触摸(点击或长按)控件的父级(例如 StackLayout、Grid 或任何包含标签的布局,包括它们的父级Layout、Page 或View)都会触发波纹动画。无论如何,在这种情况下不会调用事件处理程序LinkLabelRenderer_Touch,只有在实际控件被触摸时才会调用。
我可以通过向各自的父级添加一个空的GestureRecognizer 来解决此问题,但我真的不喜欢这个解决方案,因为这只是一个 hack。更糟糕的是,每当我使用控件时,我都必须记住这是一个 hack。
如何防止在触摸父母时显示RippleDrawable?
【问题讨论】:
-
您是否计划支持 Android API 19 及以下版本?
-
没有,不过谢谢,我自己解决了。
-
如果您将其发布为答案,那么肯定会更好,以便其他人知道您做了什么
-
@G.hakim 我打算这样做,但还没有时间。
标签: xamarin.forms xamarin.android