使用Fragment Pager Adapter时getItem()方法被调用两次 [英] getItem() method is getting called twice when using Fragment Pager Adaptor

查看:79
本文介绍了使用Fragment Pager Adapter时getItem()方法被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a fragment pager adaptor to instantiate my fragment class.I am able to do so but my problem is that my getItem() method is getting called twice which is creating problem further.can u explain me why it is happening .

    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;
        }
    }      

我正在使用以下代码调用Adapter:

I am calling Adaptor with this code:

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);  

推荐答案

请注意,寻呼机至少保留个页面.这意味着在创建寻呼机时,他将创建至少两页-他显示的页面和下一页,以允许分页".

Be aware that the pager keeps at least one page ahead. Meaning that when the pager is created, he creates at least two pages - the one he shows and the next one, in order to allow "paging".

这篇关于使用Fragment Pager Adapter时getItem()方法被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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