[ViewPager使用TabLayout时不加载片段

问题描述 投票:0回答:1

我使用TabLayoutPageViewer进行了简单的布局,但无法使它们一起使用

   <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"/>

在我的活动OnCreate中,初始化以下内容>>

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

Adapter很简单,如下所示>>

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

出现标签,但未加载片段,结果如下

enter image description here

应该在每个选项卡上加载RECENT和PEOPLE片段的地方,我的Fragment XML如下

<?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>

并且片段的代码如下(最近的代码)

public class Recent : AndroidX.Fragment.App.Fragment
    {
        public static Recent NewInstance()
        {
            var frag = new Recent { Arguments = new Bundle() };
            return frag;
        }

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View view = inflater.Inflate(Resource.Layout.fragment_emoji_recent, container, false);
            return view;
        }
    }

任何想法我在做什么错?

我使用TabLayout和PageViewer获得了简单的布局,但无法让它们一起使用] >

xamarin.android android-viewpager android-tablayout
1个回答
0
投票

此工作在转移到AndroidX之前完成了吗?

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

© www.soinside.com 2019 - 2024. All rights reserved.