2012-10-10 54 views
6

我正在使用片段尋呼機適配器來實例化我的片段class.I能夠這樣做,但我的問題是我的getItem()方法被調用兩次正在進一步製造問題。你可以解釋爲什麼它會發生。當使用片段尋呼機適配器時,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); 

回答

2

在你的getItem方法創建一個片段的新實例。這很糟糕。它使你處於現在的狀況。你正在做的是創建一個新的annynmous項目,將活着它如你指定的日子。但是當邪惡的Android系統決定需要再次與你的Fragment對話並從你的適配器請求時,你就會告訴它「不,我不想!」而是給它一個新的片段,就像它的老一代一樣。

若要解決此問題,請提前填充所有片段,然後返回正確的片段。或者如果你願意,不要。但要記錄已創建的實例,以便可以返回已創建的片段。

我不知道爲什麼它會被調用兩次 - 我沒有搜到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

但告訴我一個位置返回我0&1都在getItem當PagerAdapterCall.So我們得到片段對象在1 position.So如何可以說你的代碼我們正確。 – yokees

11

注意,尋呼機至少保持一個頁面提前。這意味着當傳呼機被創建時,他創建至少兩頁 - 他顯示的那一頁和下一頁,以便允許「分頁」。

0

您可能遇到的問題不是方法getItem()的多次調用。一個小小的研究後,我發現,isViewFromObject()有巨大的作用,並在其文檔據說"This method is required for a PagerAdapter to function properly."

我還發現,實施該方法的正確方法是這樣的方法:

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

你可以看看here

相關問題