簡體   English   中英

Xamarin.Android viewpager 滑動不起作用

[英]Xamarin.Android viewpager swipe not working

我的 viewpager 無法使用滑動功能,通過按下布局選項卡中的按鈕來更改視圖工作正常,但不能在 viewpager 中滑動。 我最初的猜測是因為我使用了抽屜布局和抽屜導航,在我嘗試不使用它們之后,我的視圖尋呼機仍然無法滑動

這是我的代碼

活動_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];
        }
    }
}

主Activity.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();
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM