【问题标题】:Ripple Effect gone after adding TapGestureRecognizer to ViewCell将 TapGestureRecognizer 添加到 ViewCell 后波纹效果消失
【发布时间】:2016-11-05 17:17:01
【问题描述】:
我在 ViewCell 的根布局中添加了一个自定义的LongPressGestureRecognizer 来处理某些情况,但是添加之后,我发现点击 ViewCell 时的涟漪效应在 Android 上消失了。我尝试通过获取本机视图来添加动画,使用以下代码将背景可绘制设置为Android.Resource.Attribute.SelectableItemBackground
int[] attrs = { Android.Resource.Attribute.SelectableItemBackground };
var ta = CrossCurrentActivity.Current.Activity.ObtainStyledAttributes(attrs);
var drawable = ta.GetDrawable(0);
nativeView.SetBackgroundDrawable(drawable);
ta.Recycle();
即使这样也行不通。还有什么方法可以让它工作吗?
【问题讨论】:
标签:
xamarin
xamarin.forms
【解决方案1】:
对于那些想知道的人,我放弃了实现目标的自定义长按手势识别器方式,因为它是错误的做事方式。在 Android 上,我们应该使用 ItemLongClick 事件。这是我做的,首先通过某种方法找出原生的ListView,我的方法是先得到ListView的渲染器,然后得到底层的ListView。另一种方法是使用下面的代码找到ListView,但是如果您有多个ListView,这种方法需要更多的工作
public static List<T> FindViews<T>(this ViewGroup viewGroup) where T : View
{
var result = new List<T>();
var count = viewGroup.ChildCount;
for (int i = 0; i < count; i++)
{
var child = viewGroup.GetChildAt(i);
var item = child as T;
if (item != null)
{
result.Add(item);
}
else if (child is ViewGroup)
{
var innerResult = FindViews<T>(child as ViewGroup);
if (innerResult != null)
{
result.AddRange(innerResult);
}
}
}
return result;
}
var rootView =(ViewGroup)CurrentActivity.Window.DecorView.RootView
var nativeListView = rootView.FindView<Android.Widget.ListView>();
然后重写Page的OnAppearing方法,在其中附加ItemLongClick事件处理函数。还覆盖OnDisappearing 方法,在其中分离ItemLongClick 事件处理程序。这个很重要。只需在构造函数中添加 ItemLongClick 事件处理程序似乎不起作用。