Xamarin.android 使用TabLayout时ViewPager未加载片段

Xamarin.android 使用TabLayout时ViewPager未加载片段,xamarin.android,android-viewpager,android-tablayout,Xamarin.android,Android Viewpager,Android Tablayout,我用TabLayout和PageViewer获得了一个简单的布局,但无法让它们一起工作 <com.google.android.material.tabs.TabLayout android:id="@+id/sliding_tabs_emoji" android:layout_width="match_parent" android:layout_height="wrap_content" android:backgr

我用
TabLayout
PageViewer
获得了一个简单的布局,但无法让它们一起工作

   <com.google.android.material.tabs.TabLayout
    android:id="@+id/sliding_tabs_emoji"    
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:tabMode="scrollable"
        app:tabGravity="fill"    
        app:tabIndicatorColor="@color/gray"
        app:tabIndicatorHeight="1dp"
        android:paddingBottom="2dp"/>
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager_emoji"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/red"/>
其中,
适配器
非常简单,如下所示

//Fragment array
            var fragments = new AndroidX.Fragment.App.Fragment[]
            {
                Library.Fun.Emoji.Fragments.Recent.NewInstance(),
                Library.Fun.Emoji.Fragments.People.NewInstance(),               
            };
            //Tab title array
            var titles = Android.Runtime.CharSequence.ArrayFromStringArray(new[] {
                "Recent" ,
                "People"
            });

            var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager_emoji);
            //viewpager holding fragment array and tab title text
            viewPager.Adapter = new EmojiTabsPagerAdapter(SupportFragmentManager, fragments, titles);            
            // Give the TabLayout the ViewPager
            TAB_Layout.SetupWithViewPager(viewPager);
            Toast.MakeText(this, "SET", ToastLength.Short);
            TAB_Layout.GetTabAt(0).SetIcon(Resource.Drawable.ic_camera);
            TAB_Layout.GetTabAt(1).SetIcon(Resource.Drawable.ic_camera);
public class EmojiTabsPagerAdapter : FragmentPagerAdapter
    {
        private readonly AndroidX.Fragment.App.Fragment[] fragments;
        private readonly ICharSequence[] titles;

        public EmojiTabsPagerAdapter(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment[] fragments, ICharSequence[] titles) : base(fm)
        {
            this.fragments = fragments;
            this.titles = titles;
        }
        public override int Count
        {
            get
            {
                return fragments.Length;
            }
        }

        public override AndroidX.Fragment.App.Fragment GetItem(int position)
        {          
            return fragments[position];            
        }
        public override ICharSequence GetPageTitleFormatted(int position)
        {
            //return titles[position];
            return null;
        }
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="300dp"    
   android:background="@color/green">
  <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="RECENTS"/>
</RelativeLayout>
出现选项卡,但未加载片段,结果如下

//Fragment array
            var fragments = new AndroidX.Fragment.App.Fragment[]
            {
                Library.Fun.Emoji.Fragments.Recent.NewInstance(),
                Library.Fun.Emoji.Fragments.People.NewInstance(),               
            };
            //Tab title array
            var titles = Android.Runtime.CharSequence.ArrayFromStringArray(new[] {
                "Recent" ,
                "People"
            });

            var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager_emoji);
            //viewpager holding fragment array and tab title text
            viewPager.Adapter = new EmojiTabsPagerAdapter(SupportFragmentManager, fragments, titles);            
            // Give the TabLayout the ViewPager
            TAB_Layout.SetupWithViewPager(viewPager);
            Toast.MakeText(this, "SET", ToastLength.Short);
            TAB_Layout.GetTabAt(0).SetIcon(Resource.Drawable.ic_camera);
            TAB_Layout.GetTabAt(1).SetIcon(Resource.Drawable.ic_camera);
public class EmojiTabsPagerAdapter : FragmentPagerAdapter
    {
        private readonly AndroidX.Fragment.App.Fragment[] fragments;
        private readonly ICharSequence[] titles;

        public EmojiTabsPagerAdapter(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment[] fragments, ICharSequence[] titles) : base(fm)
        {
            this.fragments = fragments;
            this.titles = titles;
        }
        public override int Count
        {
            get
            {
                return fragments.Length;
            }
        }

        public override AndroidX.Fragment.App.Fragment GetItem(int position)
        {          
            return fragments[position];            
        }
        public override ICharSequence GetPageTitleFormatted(int position)
        {
            //return titles[position];
            return null;
        }
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="300dp"    
   android:background="@color/green">
  <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="RECENTS"/>
</RelativeLayout>

在每个选项卡上加载最近的和人物片段的地方,我的片段XML如下

//Fragment array
            var fragments = new AndroidX.Fragment.App.Fragment[]
            {
                Library.Fun.Emoji.Fragments.Recent.NewInstance(),
                Library.Fun.Emoji.Fragments.People.NewInstance(),               
            };
            //Tab title array
            var titles = Android.Runtime.CharSequence.ArrayFromStringArray(new[] {
                "Recent" ,
                "People"
            });

            var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager_emoji);
            //viewpager holding fragment array and tab title text
            viewPager.Adapter = new EmojiTabsPagerAdapter(SupportFragmentManager, fragments, titles);            
            // Give the TabLayout the ViewPager
            TAB_Layout.SetupWithViewPager(viewPager);
            Toast.MakeText(this, "SET", ToastLength.Short);
            TAB_Layout.GetTabAt(0).SetIcon(Resource.Drawable.ic_camera);
            TAB_Layout.GetTabAt(1).SetIcon(Resource.Drawable.ic_camera);
public class EmojiTabsPagerAdapter : FragmentPagerAdapter
    {
        private readonly AndroidX.Fragment.App.Fragment[] fragments;
        private readonly ICharSequence[] titles;

        public EmojiTabsPagerAdapter(AndroidX.Fragment.App.FragmentManager fm, AndroidX.Fragment.App.Fragment[] fragments, ICharSequence[] titles) : base(fm)
        {
            this.fragments = fragments;
            this.titles = titles;
        }
        public override int Count
        {
            get
            {
                return fragments.Length;
            }
        }

        public override AndroidX.Fragment.App.Fragment GetItem(int position)
        {          
            return fragments[position];            
        }
        public override ICharSequence GetPageTitleFormatted(int position)
        {
            //return titles[position];
            return null;
        }
    }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="300dp"    
   android:background="@color/green">
  <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="RECENTS"/>
</RelativeLayout>

知道我做错了什么吗?

在迁移到AndroidX之前做过这项工作吗


我认为您可能需要将ChildFragmentManager而不是FragmentManager传递到PagerAdapter。

我使用两个片段:
TabFragment1
TabFragment2
进行测试。在代码中,您确实提供了关于
SetupWithViewPager
的代码

下面的代码对我很有用

 void setupViewPager(Android.Support.V4.View.ViewPager viewPager)
    {
        var adapter = new Adapter(SupportFragmentManager);
        adapter.AddFragment(new TabFragment1(), "First Fragment");
        adapter.AddFragment(new TabFragment2(), "Second Fragment");
                   viewPager.Adapter = adapter;
        viewpager.Adapter.NotifyDataSetChanged();
        //viewpager.OffscreenPageLimit(4);
    }
在MainActivity中,可以设置如下所示

  var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs);
        tabLayout.SetupWithViewPager(viewpager);
var tabLayout=findviewbyd(Resource.Id.tabs);
tabLayout.SetupWithViewPager(viewpager);


您可以从以下链接下载源文件:

我首先在AndroidX中实现了它,查看ChildFragmentManagerMy活动是FragmentActivity
公共类my_活动:FragmentActivity
这无关紧要。启动活动并设置视图。您可以下载该项目以进行检查。如果您不知道如何做,您可以提供更多带有问题的代码。我在我的活动中添加了初始化适配器的位置,请在我使用
var adapter=new adapter(SupportFragmentManager)时查看我的活动中的
,然后创建初始化以下
方法它甚至不设置选项卡。我们使用SupportFragmentManager在setupViewPager下面创建新的适配器类。在布局中设置选项卡,然后将布局添加到TabFragment。最后,使用代码
adapter.AddFragment(新选项卡fragment1(),“First Fragment”)将片段添加到适配器。如果你对此仍有疑问,请随时告诉我。