android - 使用 Fragment Pager Adapter 时,getItem() 方法被调用两次

标签 android

我正在使用 fragment 寻呼机适配器来实例化我的 fragment 类。我能够这样做,但我的问题是我的 getItem() 方法被调用了两次,这进一步造成了问题。你能解释一下为什么会这样吗.

    package com.creatiosoft.rssfeed.adaptor;

    import android.content.Context;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.util.Log;

    import com.creatiosoft.rssfeed.utils.RssItem;
    import com.viewpagerindicator.IconPagerAdapter;

    public class NewsFeedsAdapter extends FragmentPagerAdapter implements
            IconPagerAdapter {

        int[] icon = null;
        String[] content = null;
        String[] URLs = null;
        Context cont;

        public NewsFeedsAdapter(FragmentManager fm, Context context) {
            super(fm);
            Log.i("jk", "constructor");
            this.cont = context;
            RssItem newsFeedAppliaction = (RssItem) cont;
            /*
             * Retrieving the values of the Icons and contents from the application
             * class in utils package
             */
            icon = newsFeedAppliaction.getICONS();
            content = newsFeedAppliaction.getCONTENT();
            URLs = newsFeedAppliaction.getURL();

        }

        /** instantiate a new fragment class */
        @Override
        public Fragment getItem(int position) {
            Log.i("yt", "hello" + position);
            return TestFragment.newInstance(position % content.length, cont);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return content[position % content.length].toUpperCase();
        }

        public int getIconResId(int index) {
            return icon[index];
        }

        /** return the no of views on the basis of array items */
        @Override
        public int getCount() {

            Log.i("hi", "length" + content.length);
            return content.length;
        }
    }      

我用这段代码调用适配器:

NewsFeedsAdapter adapter = new NewsFeedsAdapter(
                getSupportFragmentManager(), getApplicationContext());
        /**
         * get the id of the view pager declared in the xml resource and set the
         * adaptor on the view pager
         */
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
        //pager.setCurrentItem(0);

        /**
         * Tab page indicator class is used to indicate the tabs and is accessed
         * from the library class
         */
        TabPageIndicator indicator = (TabPageIndicator) findViewById(R.id.indicator);
        indicator.setViewPager(pager);  

最佳答案

请注意,寻呼机至少保持一页领先。这意味着当创建分页器时,他至少创建两个页面 - 他显示的页面和下一个页面,以便允许“分页”。

关于android - 使用 Fragment Pager Adapter 时,getItem() 方法被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12823752/

相关文章:

java - 将 java 字符串处理为 Android Studio+NDK 的原生 C/C++

java - Android 对所有 Activity 使用独特的 onDestroy

android - 将 ListView 行最小化为一行

java - 使用按钮更改类(class) View

java - 生成 Google App Engine 后端(JDK 1.8,安装了新的 ADT)时,注释仅适用于 java 1.5 及更高版本

android - 是什么导致了 android webView 中的这个问题?

java - 找不到符号 findViewById

android - 重定向 URI 是否为 native 移动应用程序的 OAuth 实现提供安全性?

android - 回到之前的 fragment (如 ListView)后,我可以保持 ScrollView 的位置吗?

android - flutter : Several variant outputs are configured to use the same file name