1

I have a main activity that creates a fragment, which in turn has a TabLayout and a ViewPager in it. I found out that when I use an activity instead of a fragment, it works fine, but i want to utilize a fragment. My code for the fragment is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Android.Support.Design.Widget;
using Android.Support.V4.View;
using SupportFragmentManager = Android.Support.V4.App.FragmentManager;
using SupportFragment = Android.Support.V4.App.Fragment;
using Android.Support.V4.App;
using Java.Lang;

namespace Zrelya.Fragments
{
    public class Stats : FragmentSuper
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(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
            var view = inflater.Inflate(Resource.Layout.Stats, container, false);


            TabLayout tabs = view.FindViewById<TabLayout>(Resource.Id.tabs);
            ViewPager viewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
            SetUpViewPager(viewPager);
            tabs.SetupWithViewPager(viewPager);


            return view;
        }

        private void SetUpViewPager(ViewPager viewPager)
        {
            TabAdapter adapter = new TabAdapter(Activity.SupportFragmentManager);
            adapter.AddFragment(new Fragments.StatsTraining(), "Training");
            adapter.AddFragment(new Fragments.StatsDiet(), "Diet");

            viewPager.Adapter = adapter;
        }
    }



    public class TabAdapter : FragmentPagerAdapter
    {
        public List<SupportFragment> Fragments { get; set; }
        public List<string> FragmentNames { get; set; }

        public TabAdapter(SupportFragmentManager sfm) : base(sfm)
        {
            Fragments = new List<SupportFragment>();
            FragmentNames = new List<string>();
        }

        public void AddFragment(SupportFragment fragment, string name)
        {
            Fragments.Add(fragment);
            FragmentNames.Add(name);
        }

        public override int Count
        {
            get
            {
                return Fragments.Count;
            }
        }

        public override SupportFragment GetItem(int position)
        {
            return Fragments[position];
        }

        public override ICharSequence GetPageTitleFormatted(int position)
        {
            return new Java.Lang.String(FragmentNames[position]);
        }
    }
}

And the code for the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="#000000">
<!-- ScrollView Start -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">
            <LinearLayout
                android:orientation="vertical"
                android:minWidth="25px"
                android:minHeight="25px"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linearLayout1"
                android:layout_marginTop="20dp"
                android:paddingLeft="20dp"
                android:paddingRight="20dp">
                <View
                    android:layout_width="match_parent"
                    android:layout_height="100dp"
                    android:id="@+id/view1"
                    android:background="@drawable/stats" />
                <android.support.design.widget.TabLayout
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/tabs"
                    android:background="#ffffff" />
                <android.support.v4.view.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
<!-- ScrollView End -->
</LinearLayout>
Miko M
  • 29
  • 7
  • 1
    Try adding `android:fillViewport="true"` to your `ScrollView` – Roman Kolomenskii Jun 09 '17 at 10:18
  • I forgot to add, the tab layout does show, just not the viewpager. Your solutioon did not work ,sorry. – Miko M Jun 09 '17 at 10:31
  • Okay, can you try to set `android:layout_height="400dp"` on `ViewPager` and see if your fragments are shown? – Roman Kolomenskii Jun 09 '17 at 10:36
  • Yes now its working. Thanks. Why isnt it possible with match_parent? – Miko M Jun 09 '17 at 10:38
  • Because it's wrapped in `ScrollView` which turns `match_parent` into `wrap_content` unless `android:fillViewport="true"` is used. – Roman Kolomenskii Jun 09 '17 at 10:43
  • Now I have a new problem, when I switch to a different fragment and back to this one, the viewpager doesnt work correctly, it doesnt swipe all the way, i have to kind of force it, and the content of viewpager doesnt show either.. – Miko M Jun 09 '17 at 11:04
  • I think I need to override OnDestroy with some code, but I dont know what. Can we talk about this in chat? – Miko M Jun 09 '17 at 11:24
  • Probably `ScrollView` shenanigans. Check this question: https://stackoverflow.com/questions/8381697/viewpager-inside-a-scrollview-does-not-scroll-correclty – Roman Kolomenskii Jun 09 '17 at 11:42
  • I found out the solution here: https://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view?rq=1 – Miko M Jun 09 '17 at 11:45

0 Answers0