3

I'm new to xamarin and I'm trying to disable swipe by creating a custom Viewpager but I cant find touch listener interface in Viewpager. Is there any way to do this?

public class CustomViewPager : ViewPager   
 {
        Context _context;

        public CustomViewPager(Context context) : base(context)
        {
            _context = context;
        }

        public CustomViewPager(Context context, IAttributeSet arg1) : base(context, arg1)
        {
            _context = context;
        }

        public bool IsPagerDisabled { get; set; }

        /*public override Boolean OnTouchEvent(MotionEvent event)
        {
            if (!IsPagerDisabled)
            {
                return base.OnTouchEvent(event);
            }
            else
            {
                return false;
            }
        }*/
    }
  • what have you done so far can you share some code – FreakyAli May 30 '18 at 06:59
  • @G.hakim , I updated the question, I can't find the touch event listener in ViewPager. So the commented lines do not work – Tharindu Wanninayake May 30 '18 at 07:10
  • I have answered your question have a look @TharinduWanninayake – FreakyAli May 30 '18 at 08:06
  • Possible duplicate of [How do disable paging by swiping with finger in ViewPager but still be able to swipe programmatically?](https://stackoverflow.com/questions/9650265/how-do-disable-paging-by-swiping-with-finger-in-viewpager-but-still-be-able-to-s) – Lepidopteron Jan 16 '19 at 09:01

3 Answers3

2

I recently did a similar implementation and this seems to work like a charm:

using Android.Content;
using Android.Support.V4.View;
using Android.Views;

public class LockableViewPager : ViewPager
{
    public bool SwipeLocked { get; set; }

    Context mContext;

    public LockableViewPager(Context context) : base(context)
    {
        Init(context, null);
    }
    public LockableViewPager(Context context, Android.Util.IAttributeSet attrs) : base(context, attrs)
    {
        Init(context, attrs);
    }
    private void Init(Context ctx, Android.Util.IAttributeSet attrs)
    {
        mContext = ctx;
    }

    public override bool OnTouchEvent(MotionEvent ev)
    {
        return !SwipeLocked && base.OnTouchEvent(ev);
    }

    public override bool OnInterceptTouchEvent(MotionEvent e)
    {
        return !SwipeLocked && base.OnInterceptTouchEvent(e);
    }

    public override bool CanScrollHorizontally(int direction)
    {
        return !SwipeLocked && base.CanScrollHorizontally(direction);
    }

}

How to use it?

In xml:

<_yourNameSpace.LockableViewPager 
  auto:SwipeLocked="true"

where auto is

xmlns:auto="http://schemas.android.com/apk/res-auto"

In code :

LockableViewPager viewpager= new LoackableViewpager();
Viewpager.SwipeLocked=true;
FreakyAli
  • 13,349
  • 3
  • 23
  • 63
0

You have to write class for that as below.

public class OnTouchListener : Java.Lang.Object, View.IOnTouchListener
{
    ViewPager pager;

    public OnTouchListener(ViewPager _pager)
    {
        pager = _pager;
    }
    public bool OnTouch(View v, MotionEvent e)
    {
        var view = v as ViewPager;


        pager.SetCurrentItem(0, false);


        return false;
    }
}

Then set this class to viewPager.

OnTouchListener onTouchListener = new OnTouchListener(viewPager);
viewPager.SetOnTouchListener(onTouchListener);
Tatson Baptista
  • 445
  • 3
  • 6
  • 18
0

I chose to name the attribute "ScrollEnabled". Because iOS just uses the excat same naming. So, you have equal naming across both platforms, makes it easier for developers.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V4.View;
using Android.Util;

namespace YourNameSpace.ViewPackage {

    // Need to disable swiping for ViewPager, if user performs Pre DSA and the dsa is not completed yet
    // http://stackoverflow.com/questions/9650265/how-do-disable-paging-by-swiping-with-finger-in-viewpager-but-still-be-able-to-s
    public class CustomViewPager: ViewPager {
        public bool ScrollEnabled;

        public CustomViewPager(Context context, IAttributeSet attrs) : base(context, attrs) {
            this.ScrollEnabled = true;
        }

        public override bool OnTouchEvent(MotionEvent e) {
            if (this.ScrollEnabled) {
                return base.OnTouchEvent(e);
            }
            return false;
        }

        public override bool OnInterceptTouchEvent(MotionEvent e) {
            if (this.ScrollEnabled) {
                return base.OnInterceptTouchEvent(e);
            }
            return false;
        }

        // For ViewPager inside another ViewPager
        public override bool CanScrollHorizontally(int direction) {
            return this.ScrollEnabled && base.CanScrollHorizontally(direction);
        }

        // Some devices like the Galaxy Tab 4 10' show swipe buttons where most devices never show them
        // So, you could still swipe through the ViewPager with your keyboard keys
        public override bool ExecuteKeyEvent(KeyEvent evt) {
            return this.ScrollEnabled ? base.ExecuteKeyEvent(evt) : false;
        }
    }
}

In .axml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <YourNameSpace.ViewPackage.CustomViewPager
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@android:color/white"
      android:layout_alignParentTop="true" />
</LinearLayout>
Lepidopteron
  • 6,056
  • 5
  • 41
  • 53