Adding tabpages based on view_pager not showing fragment.

Youssef 46 Reputation points
2022-02-04T15:13:22.77+00:00

I have an app with tabcontrol and tabitems. The tabitems are generated based on a list. But when I start the app the content isn't showing.

Here is my activity code:

List<string> catagories;

var view_pager = FindViewById<ViewPager>(Resource.Id.view_pager_pos);
var tabLayout = FindViewById<TabLayout>(Resource.Id.tablayout_pos);

   _categoriesPagerAdapter = new CategoriesFragmentPagerAdapter(SupportFragmentManager,
            FragmentPagerAdapter.BehaviorResumeOnlyCurrentFragment,
            categories.Count,
            categories);

        view_pager.Adapter = _categoriesPagerAdapter;
        tabLayout.SetupWithViewPager(view_pager, true);

Here is my acitivity 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>

Here is my 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 ICharSequence GetPageTitleFormatted(int position)
    {
        return new Java.Lang.String(categories[position]);
    }

    public override int Count => _tabNr;

    public override Fragment GetItem(int position)
    {
        var category = categories[position];
        return new ProductsByCategoryFragment(category);//this is a normal fragment with just a textview
    }
}

Here is my pos_products_per_category.xml (ProductsByCategoryFragment):

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

But when I start the app, i can see the tabs but not the content of the tabitem.
What am I doing wrong?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2022-02-07T07:07:25.797+00:00

    Hello yaza,​

    Welcome to our Microsoft Q&A platform!

    when I start the app, i can see the tabs but not the content of the tabitem.

    This is because you set android:layout_height to match_parent for the FrameLayout and set android:layout_height to 0dp for the ViewPager. To use android:layout_weight, please set the android:layout_weight for the views to wrap_content instead.

       <LinearLayout  
            android:orientation="vertical"  
            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:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                ...>                    
            </com.google.android.material.tabs.TabLayout>  
             
            <androidx.viewpager.widget.ViewPager  
                android:id="@+id/view_pager_pos"  
                android:layout_width="match_parent"  
                android:layout_height="wrap_content"  
                android:layout_weight="1"/>  
             
        </LinearLayout>  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.