5

我使用Xamarin,這是我第二次嘗試讓ViewPager工作。我從零開始創建了一個參考此Web鏈接的應用程序:http://looksok.wordpress.com/2013/10/26/android-support-v4-viewpager-tutorial-including-source-code/ViewPager與FragmentPagerAdapter不顯示

應用程序編譯,部署但只顯示空白黑屏。

這裏是我的全碼:

MainActivity.cs

using System; 
using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 
using Android.Support.V4.App; 
using Android.Support.V4.View; 

namespace AndroidViewPagerTutorial 
{ 
    [Activity (Label = "AndroidViewPagerTutorial", MainLauncher = true)] 
    public class MainActivity : FragmentActivity 
    { 
     protected override void OnCreate (Bundle bundle) 
     { 
      base.OnCreate (bundle); 

      SetContentView (Resource.Layout.activity_main); 

      MyPagerAdapter pageAdapter = new MyPagerAdapter(SupportFragmentManager); 
      var pager = FindViewById<ViewPager>(Resource.Id.myViewPager); 
      pager.Adapter = pageAdapter; 

      pager.SetCurrentItem (0, true); 
     } 
    } 
} 

MyPagerAdapter.cs

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.Views; 
using Android.Widget; 
using Android.Support.V4.App; 

namespace AndroidViewPagerTutorial 
{ 
    public class MyPagerAdapter : FragmentPagerAdapter { 

     private List<Android.Support.V4.App.Fragment> fragments; 
     int fragmentCount; 

     public MyPagerAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm) 
     { 
      this.fragments = new List<Android.Support.V4.App.Fragment>(); 
      fragments.Add(new FragmentOrange()); 
      fragments.Add(new FragmentGreen()); 
      fragments.Add(new FragmentRed()); 

      fragmentCount = fragments.Count; 
     } 

     #region implemented abstract members of PagerAdapter 

     public override int Count 
     { 
      get 
      { 
       return fragmentCount; 
      } 
     } 

     #endregion 

     #region implemented abstract members of FragmentPagerAdapter 

     public override Android.Support.V4.App.Fragment GetItem (int position) 
     { 
      return fragments[position]; 
     } 

     #endregion 
    } 
} 

FragmentOrange.cs

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; 

namespace AndroidViewPagerTutorial 
{ 
    public class FragmentOrange : Android.Support.V4.App.Fragment 
    { 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

      View view = inflater.Inflate(Resource.Layout.fragment_orange, container, false); 
      return view; 
     } 
    } 
} 

fragment_green.axml

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; 

namespace AndroidViewPagerTutorial 
{ 
    public class FragmentGreen : Android.Support.V4.App.Fragment 
    { 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

      View view = inflater.Inflate(Resource.Layout.fragment_green, container, false); 
      return view; 
     } 
    } 
} 

fragment_red.axml

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; 

namespace AndroidViewPagerTutorial 
{ 
    public class FragmentRed : Android.Support.V4.App.Fragment 
    { 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

      View view = inflater.Inflate(Resource.Layout.fragment_red, container, false); 
      return view; 
     } 
    } 
} 

activity_main.axml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity"> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/myViewPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</RelativeLayout> 

fragment_orange.axml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

fragment_green.axml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/holo_green_dark" /> 

fragment_red.axml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/holo_red_light" /> 

我可以請有一定的幫助得到這個工作?

在此先感謝

回答

3

我有代碼工作。

這是每一個片段改變什麼需要:

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

我有同樣的問題。 OnCreateView方法的解決方案是什麼?您的答案缺失。 – Jeremy

+0

@Jeremy我認爲不同的是'重寫'關鍵字 –

相關問題