片段内的ViewPager显示空白的Xamarin Android

时间:2018-04-05 11:25:14

标签: android android-fragments xamarin.android android-viewpager fragment

我正在开发一个Xamarin Android应用程序。我创建了一个抽屉布局,在框架布局中加载片段。在一个片段中,我想显示带有标签布局的视图寻呼机。但片段并没有显示任何内容并保持空白。

以下是包含ViewPager的片段的代码。

public class HomeFeedFragment : Android.Support.V4.App.Fragment
{
    ViewPager viewPager;
    TabLayout tabLayout;

    ICharSequence[] titles = CharSequence.ArrayFromStringArray(new string[] 
    { "Business", "Activity", "Place", "Tracking" });
    Android.Support.V4.App.Fragment[] fragments;
    Adapters.ViewPagerAdapter adapter;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate(Resource.Layout.layout_feed_tabs, container, false);
        tabLayout = view.FindViewById<TabLayout>(Resource.Id.tabLayout);
        viewPager = view.FindViewById<ViewPager>(Resource.Id.viewPager);

        fragments = new Android.Support.V4.App.Fragment[]
        {
            new FeedItemsFragment(Service.DbService.FeedItems.Where(p => p.PostType == PostType.Business.ToString()).ToList()),
            new FeedItemsFragment(Service.DbService.FeedItems.Where(p => p.PostType == PostType.Activity.ToString()).ToList()),
            new FeedItemsFragment(Service.DbService.FeedItems.Where(p => p.PostType == PostType.Place.ToString()).ToList()),
        };
        adapter = new Adapters.ViewPagerAdapter(ChildFragmentManager, fragments, titles);
        viewPager.Adapter = adapter;
        tabLayout.SetupWithViewPager(viewPager);

        return base.OnCreateView(inflater, container, savedInstanceState);
    }
}

绑定片段的适配器如下所示:

public class ViewPagerAdapter : Android.Support.V4.App.FragmentPagerAdapter
{
    Android.Support.V4.App.Fragment[] fragments;
    ICharSequence[] titles;
    public ViewPagerAdapter(Android.Support.V4.App.FragmentManager fm, Android.Support.V4.App.Fragment[] fragments,
        ICharSequence[] titles) : base(fm)
    {
        this.fragments = fragments;
        this.titles = titles;
    }

    public override int Count => fragments.Length;

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

    public override ICharSequence GetPageTitleFormatted(int position)
    {
        return titles[position];
    }
}

我是Xamarin的新手。

1 个答案:

答案 0 :(得分:1)

替换行

return view;

return base.OnCreateView(( inflater,  container, savedInstanceState));