2012-10-10 5 views
6

フラグメント・ページャ・アダプタを使用してフラグメント・クラスをインスタンス化しています。それが起こっている理由を私に説明することができます。あなたはフラグメントの新しいインスタンスを作成し、あなたのgetItem方法でフラグメント・ポケット・アダプタを使用すると、getItem()メソッドが2回コールされる

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

答えて

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

私はこのコードを使用してアダプタを呼び出しています。これはひどい。あなたが今いる状況にあなたを置きます。あなたがしていることは、あなたが指定した日数を超えて生き残る新しい年鑑アイテムを作成することです。しかし、悪いAndroidシステムがあなたのフラグメントに再度話して、あなたのアダプターからそれを要求する必要があると判断すると、あなたはそれを「いいえ、私はしたくない!その代わりに古いフラグメントと全く同じ新しいフラグメントを与えます。

これを修正するには、フラグメントをすべて前もって膨張させてから、適切なフラグメントを返します。または、もしあなたがむしろ、しないでください。しかし、作成したインスタンスを記録しておくと、既に作成されたフラグメントを返すことができます。

私はv4のソースを精査していませんが、実際にアイテムが実際に取得されたか、システムが以前のアイテムが必要だった状態になったかを確認するために行われますrefereshed。

結論として、作成されたインスタンス - 、とりわけをフラグメント、ビュー、アクティビティとして重いオブジェクトに保持します。

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]; 
    } 
} 
+0

可能であれば、サンプルコードを教えてください。 – yokees

+0

@ Rishabh.CreatioSoftあなたは行き​​ます。 :) – AedonEtLIRA

+0

しかし私は1つのことを教えてくれます。私は0と1の両方をgetItemに返します.PagerAdapterCall.Soでは、1つの位置にフラグメントオブジェクトがあります。 – yokees

11

ページャが先に少なくとも1ページを保持していることに注意してください。つまり、ページャが作成されたときに、「ページング」を許可するために、ページャを作成し、次のページを少なくとも2つ作成します。

0

おそらく問題は、方法getItem()の複数の呼び出しではありません。少し研究した後、私が見つかりました。その大きな役割を持っており、そのマニュアルに"This method is required for a PagerAdapter to function properly."

私もこの方法を実施するための正しい方法は本であることがわかったと言われてisViewFromObject()方法:

@Override 
public boolean isViewFromObject(View view, Object object) { 
    if(object != null){ 
     return ((Fragment)object).getView() == view; 
    }else{ 
     return false; 
    } 
} 

あなたを見ることができるhere

関連する問題