FragmentPagerAdapter只存在于Android.Support.V4.App(而不是Android.App) [英] FragmentPagerAdapter Exists Only In Android.Support.V4.App (and not Android.App)

查看:785
本文介绍了FragmentPagerAdapter只存在于Android.Support.V4.App(而不是Android.App)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到找到FragmentPagerAdapter内Android.App。

我不希望使用片段的从Android.Support.V4.App,因为我的目标API是 14及更高版本(的Andr​​oid 4.0及以上)。所以,我想只用Android.App.Fragments平原, 并有相关的课程。

我只发现它内Android.Support.V4.App,但这并不足以 对我来说B / C我试图使用Android.App.Fragment的(不Android.Support.V4.App.Fragment的) 而且在Android.App(不Android.Support.V4.App),和我的code习惯相关的类 编译,如果我得到我的传呼机从FragmentPagerAdapter如果从支持库, 因为导致类型不匹配的Andr​​oid.App和Android.Support.V4.App之间。

正如这里的情况不能转换为android.app.Fragment ,有一个正常的寻呼机(PagerAdapter)我应该使用类 地方FragmentPagerAdapter什么的(就像你的正常活动获得,以及 不FragmentActivity,当针对API 11或更高)。

下面是示例code我正在使用(它在FragmentPagerSupport.cs从在<一个发现MonoDroid例子Support4.sln解决方案中的文件href="https://github.com/xamarin/monodroid-samples/tree/master/Support4">https://github.com/xamarin/monodroid-samples/tree/master/Support4).

我注释掉的引用Android.Support.V4.App并替换它们与线 code引用Android.App。有Android.Support.V4.App之外没有FramePagerAdapter,我能找到的,我真的需要它)。

感谢。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;

使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
//使用Android.Support.V4.App;
//使用Android.Support.V4.View;

命名空间Support4
{
    [活动(标签=@字符串/ fragment_pager_support)]
    [IntentFilter的(新[] {} Intent.ActionMain,类别=新的[] {mono.support4demo.sample})]
    //公共类FragmentPagerSupport:FragmentActivity
        公共类FragmentPagerSupport:活动
    {
        const int的NUM_ITEMS = 10;
        MyAdapter适配器;
        ViewPager寻呼机;

        保护覆盖无效的OnCreate(束捆)
        {
            base.OnCreate(包);

            的setContentView(Resource.Layout.fragment_pager);

            //适配器=新MyAdapter(SupportFragmentManager);
                        适配器=新MyAdapter(FragmentManager);

            寻呼机= FindViewById&LT; ViewPager&GT;(Resource.Id.pager);
            pager.Adapter =适配器;

            VAR按钮= FindViewById&LT;按钮&GT;(Resource.Id.goto_first);
            button.Click + =(发件人,E)=&GT; {
                pager.CurrentItem = 0;
            };
            按钮= FindViewById&LT;按钮&GT;(Resource.Id.goto_last);
            button.Click + =(发件人,E)=&GT; {
                pager.CurrentItem = NUM​​_ITEMS  -  1;
            };
        }

                // ???????????????????????????????????????????????? ?
                //  - 哪里是FragmentPagerAdapter
                // ???????????????????????????????????????????????? ?

        保护类MyAdapter:FragmentPagerAdapter
        {
            公共MyAdapter(FragmentManager FM):基地(FM)
            {
            }

            公众覆盖INT计数{
                得到 {
                    返回NUM_ITEMS;
                }
            }

            公众覆盖片段的GetItem(INT位置)
            {
                返回新ArrayListFragment(位置);
            }


        }

        保护类ArrayListFragment:ListFragment
        {
            INT NUM;

            公共ArrayListFragment()
            {
            }

            公共ArrayListFragment(INT NUM)
            {
                变参=新包();
                args.PutInt(民,NUM);
                参数= ARGS;
            }

            公众覆盖无效的OnCreate(束P0)
            {
                base.OnCreate(P0);

                NUM =参数!= NULL? Arguments.GetInt(民):1;
            }

            公众覆盖查看OnCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
            {
                变种V = inflater.Inflate(Resource.Layout.fragment_pager_list,集装箱,假);
                VAR电视= v.FindViewById&LT; TextView的&GT;(Resource.Id.text);
                tv.Text =片段#+ NUM;
                返回伏;
            }

            公众覆盖无效OnActivityCreated(包P0)
            {
                base.OnActivityCreated(P0);

                ListAdapter =新的ArrayAdapter&LT;字符串&GT;(活动,Android.Resource.Layout.SimpleListItem1,Cheeses.cheeseStrings);
            }

            公众覆盖无效OnListItemClick(ListView的L,视图V,INT位置,长的id){
                Console.WriteLine(项点击+ ID);
            }
        }
    }
}
 

解决方案

还有一个是在<一个href="http://developer.android.com/reference/android/support/v13/app/FragmentPagerAdapter.html"><$c$c>android.support.v13.app.FragmentPagerAdapter,这应该做你想要它做的事情。这是一个FragmentPagerAdapter对非援助的片段。

Android的Studio安装

请加跟随摇篮依赖

 相关性{
    编译com.android.support:support-v13:+
}
 

I cannot find find FragmentPagerAdapter within Android.App.

I do not want to use the Fragment's from Android.Support.V4.App, as my target API is 14 and higher (Android 4.0 and up). So, I want to just use plain of Android.App.Fragments, and there associated classes.

I only have found it within Android.Support.V4.App, but this doesn't suffice for me b/c I'm trying to use Android.App.Fragment's (not Android.Support.V4.App.Fragment's) and there related classes within Android.App (not Android.Support.V4.App), and my code wont compile if I derive my pager from FragmentPagerAdapter if its from the Support library, because of the resulting type mismatch's between Android.App and Android.Support.V4.App.

Just as with the case here Cannot be cast to android.app.Fragment, is there a "normal" pager (PagerAdapter) class I should be using in place of FragmentPagerAdapter or something (just like you derive from normal Activity, and not FragmentActivity, when targeting API 11 or higher).

Here is the sample code I'm working with (its the FragmentPagerSupport.cs file within the Support4.sln solution from the MonoDroid examples found at https://github.com/xamarin/monodroid-samples/tree/master/Support4).

I've commented out the lines that referenced Android.Support.V4.App and replaced them with code that references Android.App. There is no FramePagerAdapter outside of Android.Support.V4.App that I could find, and I really need it).

Thanks.

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;
//using Android.Support.V4.View;

namespace Support4
{
    [Activity (Label = "@string/fragment_pager_support")]
    [IntentFilter (new[]{Intent.ActionMain}, Categories = new[]{ "mono.support4demo.sample" })]
    //public class FragmentPagerSupport : FragmentActivity
        public class FragmentPagerSupport : Activity
    {
        const int NUM_ITEMS = 10;
        MyAdapter adapter;
        ViewPager pager;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView(Resource.Layout.fragment_pager);

            //adapter = new MyAdapter(SupportFragmentManager);
                        adapter = new MyAdapter(FragmentManager);

            pager = FindViewById<ViewPager>(Resource.Id.pager);
            pager.Adapter = adapter;

            var button = FindViewById<Button>(Resource.Id.goto_first);
            button.Click += (sender, e) => {
                pager.CurrentItem = 0;  
            };
            button = FindViewById<Button>(Resource.Id.goto_last);
            button.Click += (sender, e) => {
                pager.CurrentItem = NUM_ITEMS - 1;
            };
        }

                // ?????????????????????????????????????????????????
                // - where is FragmentPagerAdapter 
                // ?????????????????????????????????????????????????

        protected class MyAdapter : FragmentPagerAdapter 
        {
            public MyAdapter(FragmentManager fm) : base(fm)
            {
            }

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

            public override Fragment GetItem (int position)
            {
                return new ArrayListFragment(position);
            }


        }

        protected class ArrayListFragment : ListFragment
        {
            int num;

            public ArrayListFragment()
            {
            }

            public ArrayListFragment(int num)
            {
                var args = new Bundle();
                args.PutInt("num", num);
                Arguments = args;
            }

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

                num = Arguments != null ? Arguments.GetInt("num") : 1;
            }

            public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                var v = inflater.Inflate(Resource.Layout.fragment_pager_list, container, false);
                var tv = v.FindViewById<TextView>(Resource.Id.text);
                tv.Text = "Fragment #" + num;
                return v;
            }

            public override void OnActivityCreated (Bundle p0)
            {
                base.OnActivityCreated (p0);

                ListAdapter = new ArrayAdapter<string>(Activity, Android.Resource.Layout.SimpleListItem1, Cheeses.cheeseStrings);
            }

            public override void OnListItemClick(ListView l, View v, int position, long id) {
                Console.WriteLine ( "Item clicked: " + id);
            }
        }
    }
}

解决方案

There is one that is in android.support.v13.app.FragmentPagerAdapter, which should do what you want it to do. It's a FragmentPagerAdapter for non-support fragments.

Android Studio Installation

Please add follow Gradle dependencies

dependencies {
    compile 'com.android.support:support-v13:+'
}

这篇关于FragmentPagerAdapter只存在于Android.Support.V4.App(而不是Android.App)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆