Search code examples
androidcustom-controlsxamarin.androidandroid-galleryxamarin.mobile

Custom Gallery Not Scrolling


I am following this question: How to stop scrolling in a Gallery Widget?

I tried to implement my custom gallery like so:

public class CustomGallery : Gallery
    {
        public CustomGallery(Context context, IAttributeSet attrs) :
            base(context, attrs)
        {
            Initialize();
        }

        public CustomGallery(Context context, IAttributeSet attrs, int defStyle) :
            base(context, attrs, defStyle)
        {
            Initialize();
        }

        private void Initialize()
        {
        }

        public override bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
        {
            Keycode kEvent;
            if (isScrollingLeft(e1, e2))
            { //Check if scrolling left
                kEvent = Keycode.DpadLeft;
            }
            else
            { //Otherwise scrolling right
                kEvent = Keycode.DpadRight;
            }

            OnKeyDown(kEvent, null);

            return true;
        }

        private bool isScrollingLeft(MotionEvent e1, MotionEvent e2)
        {
            return e2.GetX() > e1.GetX();


        }
    }

I have referenced the custom gallery in my layout and I see the first image but nothing happens when I try scroll to the next image.

Strangely if I remove the override for OnFling I still don't get any scrolling in my gallery? Surely if I don't override anything my custom component should behave like the normal gallery component?

PS: All I am trying to do is prevent the Gallery from scrolling through multiple images when I swipe left or right, I want to scroll one image at a time.

Any help is appreciated!


Solution

  • Scratch this post, figured it out, I was ovveriding the OnTouch event in my Activity ><