基于 viewpager 不显示片段将 tabitem 添加到 tablayout

Adding tabitems to tablayout based on viewpager not showing fragment

我有一个带有基于列表的标签页的应用程序。 tablayout是基于viewpager生成的。但是当我 select 一个选项卡时,该片段无法显示。

这是我的活动代码:

List<string> categories = new List<string>
            {
                "Books",
                "Fruits",
                "Vegetables"
            };
    
 var view_pager = FindViewById<ViewPager>(Resource.Id.view_pager_pos);
 var tabLayout = FindViewById<TabLayout>(Resource.Id.tablayout_pos);
    
   _categoriesPagerAdapter = new CategoriesFragmentPagerAdapter(SupportFragmentManager,
                 FragmentPagerAdapter.BehaviorResumeOnlyCurrentFragment,
                 tabLayout.TabCount,
                 categories);      
                
        TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        view_pager.Adapter = _categoriesPagerAdapter;
        tabLayout.SetupWithViewPager(view_pager, true);

这是我的活动xml:

<LinearLayout
     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:orientation="horizontal"
     android:minWidth="25px"
     android:minHeight="25px"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/linearLayout_pos">
    
    
     <FrameLayout
         android:id="@+id/fragment_container_pos"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_weight="0.7" />
    
     <com.google.android.material.tabs.TabLayout
         android:layout_weight="0.3"
         android:minWidth="25px"
         android:minHeight="25px"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         app:tabGravity="fill"
         android:id="@+id/tablayout_pos">
                    
     </com.google.android.material.tabs.TabLayout>
    
     <androidx.viewpager.widget.ViewPager
         android:id="@+id/view_pager_pos"
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_weight="1"
         android:background="@android:color/white" />
    
    
 </LinearLayout>

这是我的 CategoriesFragmentPagerAdapter:

internal class CategoriesFragmentPagerAdapter : FragmentPagerAdapter
     {
         private int _tabNr;
         private List<string> categories;
    
         public CategoriesFragmentPagerAdapter(FragmentManager fm, int behavior, int tabs, List<string> categories) : base(fm, behavior)
         {
             _tabNr = tabs;
             this.categories = categories;
         }
                  
         public override int Count => _tabNr;
    
         public override Fragment GetItem(int position)
         {
             var category = categories[position];
             return new ProductsByCategoryFragment(category.Id);
         }
     }

这是我的 ProductsByCategoryFragment(基于类别的标签页片段):

internal class ProductsByCategoryFragment : AndroidX.Fragment.App.Fragment
{
    private readonly string category;
    

    public ProductsByCategoryFragment(string category)
    {
        this.category = category;
    }

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

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        return inflater.Inflate(Resource.Layout.pos_products_per_category, container, false);
    }

    public override void OnViewCreated(View view, Bundle savedInstanceState)
    {
        base.OnViewCreated(view, savedInstanceState);
    }
}

这是我的 pos_products_per_category.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="match_parent"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     tools:showIn="@layout/app_bar_main">
    
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerInParent="true"
         android:text="Category fragment" />
    
 </RelativeLayout>

我可以看到标签页,但是片段没有显示在标签页上。我在这里遗漏了什么吗?

我已经在我的设备上测试了您的代码。我更改了一些代码以使其 运行 成功。当我将 android:orientation="horizo​​ntal" 更改为垂直并将 android:layout_height="match_parent" 更改为包装内容时,该片段将 show.Such 为:

<LinearLayout
 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:orientation="vertical"
 android:minWidth="25px"
 android:minHeight="25px"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/linearLayout_pos">


 <FrameLayout
     android:id="@+id/fragment_container_pos"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="0.7" />

 <com.google.android.material.tabs.TabLayout
     android:layout_weight="0.3"
     android:minWidth="25px"
     android:minHeight="25px"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:tabGravity="fill"
     android:id="@+id/tablayout_pos">
                
 </com.google.android.material.tabs.TabLayout>

 <androidx.viewpager.widget.ViewPager
     android:id="@+id/view_pager_pos"
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="1"
     android:background="@android:color/white" />