0
votes

I added a custom LongPressGestureRecognizer to the ViewCell's root layout to handle certain cases, but after adding it, I find that the ripple effect when tapping the ViewCell is gone on Android. I tried to add back the animation by getting the native view, set background drawable to Android.Resource.Attribute.SelectableItemBackground by using below code

            int[] attrs = { Android.Resource.Attribute.SelectableItemBackground };

            var ta = CrossCurrentActivity.Current.Activity.ObtainStyledAttributes(attrs);

            var drawable = ta.GetDrawable(0);
            nativeView.SetBackgroundDrawable(drawable);
            ta.Recycle();

Even this doesn't work. Any other way to make it work?

1

1 Answers

0
votes

For those who want to know, I discarded the custom long press gesture recognizer way of achieving the goal, since it's the wrong way of doing things. On Android, we should use ItemLongClick event instead. Here is what I did, first, find out the native ListView through some method, my way is to first get the renderer of the ListView, then get underlying ListView. Another way is to use below code to find the ListView, but this way requires more work if you have multiple 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>();

Then override the OnAppearing method of the Page, in it, attach ItemLongClick event handler. Also override OnDisappearing method, in it, detach the ItemLongClick event handler. This is important. Simply add ItemLongClick event handler in constructor seems not working.