gpt4 book ai didi

android - 带有 FragmentPagerAdapter 的 ViewPager 不显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:55:47 26 4
gpt4 key购买 nike

我正在使用 Xamarin,这是我第二次尝试让 ViewPager 正常工作。我引用此 Web 链接从头开始创建了一个应用程序:http://looksok.wordpress.com/2013/10/26/android-support-v4-viewpager-tutorial-including-source-code/

应用程序编译、部署,但只显示空白、黑屏。

这是我的完整代码:

主 Activity .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 _绿色.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 _橙色.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 _绿色.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" />

我可以得到一些帮助来让它工作吗?

提前致谢

最佳答案

我已经让代码工作了。

这是每个 fragment 需要更改的内容:

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

关于android - 带有 FragmentPagerAdapter 的 ViewPager 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22629016/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com