6

フラグメント ページャー アダプターを使用してフラグメント クラスをインスタンス化しています。そうすることはできますが、問題は getItem() メソッドが 2 回呼び出され、さらに問題が発生していることです。

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

3 に答える 3

12

ポケットベルは少なくとも 1 ページ先に進むことに注意してください。つまり、ページャーが作成されると、「ページング」を可能にするために、表示されるページと次のページの少なくとも 2 つのページが作成されます。

于 2013-10-03T07:14:43.570 に答える
2

getItem メソッドで、フラグメントの新しいインスタンスを作成します。これはひどいです。それはあなたを今の状況に置きます。あなたがやっていることは、あなたが指定したように何日も生き続ける新しいAnnynmousアイテムを作成することです. しかし、邪悪な Android システムが Fragment と再度通信する必要があると判断し、アダプタから要求すると、「いいえ、したくありません!」と言っているのと同じです。代わりに、古い兄弟とまったく同じ新しいフラグメントを与えます。

これを修正するには、事前にすべてのフラグメントをインフレートしてから、適切なフラグメントを返します。または、そうしないでください。ただし、作成したインスタンスの記録を保持して、作成済みのフラグメントを返すことができるようにします。

なぜそれが 2 回呼び出されるのか正確にはわかりません。v4 のソースを精査していませんが、アイテムが実際に取得されるか、システムが前のアイテムを参照する必要がある状態になったことを確認するために行われる可能性があります。

結論として、特にフラグメント、ビュー、アクティビティのような重いオブジェクトの場合は、作成されたインスタンスを保持してください。

class Adapter extends MyWhateverAdapter {
    Fragment[] myLovelyFragments;

    public Adapter() {
        // Instantiate your fragments and place them into myLovelyFragments
    }

    public int getFragmentCount() {
        return myLovelyFragments.length;
    }

    public Fragment getItem(int position) {
        return myLovelyFragments[position];
    }
}
于 2012-10-10T16:14:50.247 に答える