Edwin Sulaiman
Edwin Sulaiman

Reputation: 739

Xamarin.Android viewpager swipe not working

My viewpager can't use the swipe feature, changing the view by pressing the button in the layout tab works fine, but not swipe in the viewpager. My initial guess is that because I use drawer layout and drawer navigation, after I try not to use them, my view pager still can't be swiped

here's my code

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <com.google.android.material.tabs.TabLayout
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout" />
    <androidx.viewpager.widget.ViewPager
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/viewPager"
        android:fitsSystemWindows="true" />

</LinearLayout>

fragment_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:text="Hello !!"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minWidth="25px"
        android:minHeight="25px"
        android:id="@+id/textView1" />

</LinearLayout>

ViewPagerAdapter.cs

using System;
using System.Collections.Generic;
using AndroidX.Fragment.App;
using Java.Lang;

namespace Project
{
    public class PagerFragmentAdapter : FragmentStatePagerAdapter
    {

        List<Fragment> fragments;
        List<Java.Lang.String> fragmentNames;

        public PagerFragmentAdapter(FragmentManager fm) : base(fm)
        {
            fragments = new List<Fragment>();
            fragmentNames = new List<Java.Lang.String>();
        }

        public void AddFragment(Fragment fragment, Java.Lang.String title)
        {
            fragments.Add(fragment);
            fragmentNames.Add(title);
        }

        public override int Count
        {
            get
            {
                return fragments.Count;
            }
        }

        public override Fragment GetItem(int position)
        {
            return fragments[position];
        }

        public override ICharSequence GetPageTitleFormatted(int position)
        {
            return fragmentNames[position];
        }
    }
}

MainActivity.cs

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

using Android.OS;
using Project.Adapters;
using Google.Android.Material.Tabs;
using AndroidX.ViewPager.Widget;
using Project.Fragments;
using Android.App;
using AndroidX.AppCompat.App;

namespace Project
{
    [Activity(Label = "MainActivity")]
    public class MainActivity : AppCompatActivity
    {
        ViewPager viewPager;
        TabLayout tabLayout;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_layanan);

            ConnectView();
        }

        void ConnectView()
        {
            viewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
            tabLayout = FindViewById<TabLayout>(Resource.Id.tabLayout);

            viewPager.OffscreenPageLimit = 3;
            viewPager.BeginFakeDrag();

            SetUpViewPager();
        }

        void SetUpViewPager()
        {

            PagerFragmentAdapter adapter = new PagerFragmentAdapter(SupportFragmentManager);
            adapter.AddFragment(new ViewFragment(), new Java.Lang.String("View 1"));
            adapter.AddFragment(new ViewFragment(), new Java.Lang.String("View 2"));
            viewPager.Adapter = adapter;
            tabLayout.SetupWithViewPager(viewPager);
        }
    }
}

is there anything i missed?

Upvotes: 0

Views: 315

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 14956

You just need to remove the viewPager.BeginFakeDrag();

void ConnectView()
  {
      viewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
      tabLayout = FindViewById<TabLayout>(Resource.Id.tabLayout);

      viewPager.OffscreenPageLimit = 3;
      //viewPager.BeginFakeDrag();     //remove this line

      SetUpViewPager();
  }

Upvotes: 1

Related Questions