Xamarin.Android viewpager 滑动不工作

Xamarin.Android viewpager swipe not working

我的 viewpager 无法使用滑动功能,通过按布局选项卡中的按钮更改视图可以正常工作,但不能在 viewpager 中滑动。我最初的猜测是因为我使用了drawer layout和drawer navigation,在我尝试不使用它们之后,我的view pager仍然无法滑动

这是我的代码

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);
        }
    }
}

有什么我遗漏的吗?

您只需删除 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();
  }