【发布时间】:2014-03-19 07:43:24
【问题描述】:
我正在使用 Xamarin,但在我的 ViewPager 不显示我的视图方面遇到了一些代码问题。
这是我的代码:
主活动:
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;
}
}
第一个片段:
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;
}
}
第二个片段:
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;
}
}
第三个片段:
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>
当应用程序加载时,什么都不会显示。没有发生错误。
我可以帮忙显示视图吗?
提前致谢
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>
另外,我从以下链接获得了此代码: How to implement a ViewPager with different Fragments / Layouts
提前致谢
【问题讨论】:
-
你能把你的布局代码放在viewpager上吗?
-
@choper:你能看看我的代码吗?
-
显示片段的布局。
-
请看我的edit2
标签: android android-fragments android-viewpager xamarin fragmentpageradapter