ViewPager视图没有显示



我正在使用Xamarin,我有一些代码问题,涉及到我的ViewPager不显示我的视图。

下面是我的代码:

MainActivity:

public class MainActivity : FragmentActivity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.activity_main);
        var pager = FindViewById<ViewPager>(Resource.Id.viewPager);
        pager.Adapter = new MyPagerAdapter(SupportFragmentManager);
    }
}

MyPagerAdapter:

public class MyPagerAdapter : FragmentPagerAdapter 
{
    int count;
    public override int Count 
    {
        get 
        {
            return count;   
        }   
    }
    public override Android.Support.V4.App.Fragment GetItem (int position)
    {
        switch(position) 
        {
        case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
        case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
        case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
        case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2");
        case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3");
        default: return ThirdFragment.newInstance("ThirdFragment, Default");
        }       
    }
    public MyPagerAdapter (Android.Support.V4.App.FragmentManager fm) : base (fm)
    {
        count = 5;
    }     
}

FirstFragment:

public class FirstFragment : Android.Support.V4.App.Fragment
{
    string text;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        View v = inflater.Inflate(Resource.Layout.first_frag, container, false);
        TextView tv = (TextView) v.FindViewById(Resource.Id.tvFragFirst);
        tv.Text = Arguments.GetString("msg");
        return v;
    }
    public static FirstFragment newInstance(String text) 
    {
        FirstFragment f = new FirstFragment();
        Bundle b = new Bundle();
        b.PutString("msg", text);
        f.Arguments = (b);
        return f;
    }
}

SecondFragment:

public class SecondFragment : Android.Support.V4.App.Fragment
{
    string text;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.Inflate(Resource.Layout.second_frag, container, false);
        TextView tv = (TextView) v.FindViewById(Resource.Id.tvFragSecond);
        tv.Text = Arguments.GetString("msg");
        return v;
    }
    public static SecondFragment newInstance(String text) 
    {
        SecondFragment f = new SecondFragment();
        Bundle b = new Bundle();
        b.PutString("msg", text);
        f.Arguments = (b);
        return f;
    }
}

ThirdFragment:

public class ThirdFragment : Android.Support.V4.App.Fragment
{
    string text;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.Inflate(Resource.Layout.third_frag, container, false);
        TextView tv = (TextView) v.FindViewById(Resource.Id.tvFragThird);
        tv.Text = Arguments.GetString("msg");
        return v;
    }
    public static ThirdFragment newInstance(String text) 
    {
        ThirdFragment f = new ThirdFragment();
        Bundle b = new Bundle();
        b.PutString("msg", text);
        f.Arguments = (b);
        return f;
    }
}

编辑

这是我的activity_main.axml布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3F3F4"
android:orientation="vertical">
<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="9dp"
    android:paddingRight="9dp" />
</LinearLayout>

当应用程序加载时,什么都不显示。没有错误发生。

请帮我把视图显示出来好吗?

Thanks in advance

EDIT2

下面是片段布局:

first_frag.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_orange_dark">
    <TextView
        android:id="@+id/tvFragFirst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="26dp"
        android:text="TextView" />
</RelativeLayout>

second_frag.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_dark">
    <TextView
        android:id="@+id/tvFragSecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="26dp"
        android:text="TextView" />
</RelativeLayout>

third_frag.axml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_red_light">
    <TextView
        android:id="@+id/tvFragThird"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="26dp"
        android:text="TextView" />
</RelativeLayout>

另外,我从以下链接得到了这个代码:如何实现不同片段/布局的ViewPager

Thanks in advance

android:layout_height改为其他值0dp。

最新更新