为什么我的ViewPager没有显示?

时间:2017-06-09 09:23:06

标签: c# android android-viewpager

我有一个主要活动创建一个片段,其中包含一个TabLayout和一个ViewPager。我发现当我使用活动而不是片段时,它工作正常,但我想利用片段。我的片段代码如下:

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

布局代码:

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

0 个答案:

没有答案