Defining ViewPager with TabHost Inside a Fragment and not in AppCompatActivity

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 2

Sep 30, 2016, 7:36:09 PM via Website

I have this Layout:

1) An AppCompatActivity with a ToolBar and a fragment container

2) Some simple Fragments

3) A Fragment that coantains TabLayout with ViewPager

When i use the fragments that are supposed to be used on the tabs the fragments are showing nicely.

One of the fragments that are supposed to be on the tabs has inside a ViewPager. This is how i try to implement the TabLayout inside the Fragment

The Fragment with the TabLayout and the ViewPager:

public class StaffProfileFragment : Fragment
    public override void OnCreate(Bundle savedInstanceState)

        // Create your fragment here

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

        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);
        var view = inflater.Inflate(Resource.Layout.StaffProfilesWithTabs, container, false);

        var fragments = new Fragment[]
            new StaffList(),
            new MessagesFragment(),
            new SuggestionsMessagesFragment()

        var titles = CharSequence.ArrayFromStringArray(new[]

        var viewPager = view.FindViewById<ViewPager>(Resource.Id.ProfileViewpager);
        viewPager.Adapter = new TabsFragmentPagerAdapter(ChildFragmentManager, fragments, titles);

        //var myActivity = (MainActivity)this.Activity;
        //myActivity.SetActionBarTitle("Select Your Location");

        // Give the TabLayout the ViewPager
        var tabLayout = view.FindViewById<TabLayout>(Resource.Id.SlidingTabsTabLayout);

        return view;

And the Adapter i use:

public class TabsFragmentPagerAdapter : FragmentPagerAdapter
    private readonly Fragment[] fragments;

    private readonly ICharSequence[] titles;

    public TabsFragmentPagerAdapter(FragmentManager fm, Fragment[] fragments, ICharSequence[] titles) : base(fm)
        this.fragments = fragments;
        this.titles = titles;
    public override int Count
            return fragments.Length;

    public override Fragment GetItem(int position)
        return fragments[position];

    public override ICharSequence GetPageTitleFormatted(int position)
        return titles[position];

(Im putting only this code for now if it is enough for the problem to be solved, else i will put the xmls and the Fragments code.)

The problem is that the fragment appears but it doesnt load the data correctly, tha tabs cannot be swyped and if i click on the fragment somewhere the app crash with crushing in Debugger.

What am i doing wrong? is it bad idea to implement tablayout inside a fragment? maybe i should implement it on the AppCompatActivity and show/hide the elements i want to use on every occasion?

Two solutions that work as i have test them are:

  1. Create a seperate Activity with toolbar and tabhost.

    2.In the activity with the toolbar to add tabhost and when i want to show only one fragment, i would use tabhost and set visibility to gone.

But im trying to understand which is the best way to do this, Thanks

Recommended articles