2014-12-18 95 views
2

在觀看和研究此主題的每個帖子後,我沒有爲我的bug找到解決方法。ViewPager中的片段不會刷新,也不會顯示任何內容

我有一個PagerSlidingTabStrip這是選擇我的ViewPager,和我的適配器爲ViewPager是:

public class ViewPagerAdapter extends FragmentStatePagerAdapter{ 


    public ViewPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public int getItemPosition(Object object) { 
     return POSITION_NONE; 
    } 

    @Override 
    public Fragment getItem(int i) { 
     return fragments.get(i); 
    } 

    @Override 
    public int getCount() { 
     return fragments.size(); 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     if (position == 0) 
      return "ALL"; 
     else 
      return tipos.get(position-1).getNombre(); 
    } 
} 

我主持上Activity尋呼機。

目的fragments是:

HashMap<Integer,PostFragment> fragments = new HashMap<Integer,PostFragment>(); 

和我用來初始化HashMap中爲:

public void initHashMap(HashMap<Integer,PostFragment> fragments) { 
    if (fragments.size() > 0) 
     return; 

    PostFragment allFragment = new PostFragment(); 
    Bundle argsAll = new Bundle(); 
    argsAll.putInt("tipo", 0); 
    argsAll.putSerializable("tipos", (Serializable)tipos); 
    allFragment.setArguments(argsAll); 
    fragments.put(0, allFragment); 

    for (int i = 0; i < tipos.size(); i++) { 
     PostFragment fragment = new PostFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("tipo", i+1); 
     args.putSerializable("tipos", (Serializable)tipos); 
     fragment.setArguments(args); 
     fragments.put(i+1, fragment); 
    } 
} 

問題: 當我滑動,在第一時間片段得到裝載。如果我快速地刷一下,一些片段在回到它們時開始不再工作。 有時,當我點擊標籤條時,它們也不加載。

我試過幾乎所有我以前試過的解決方法。

這是我的XML,我主持它:

<RelativeLayout 
    android:id="@+id/parentViews" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/rl1" > 

    <RelativeLayout 
     android:id="@+id/searchView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/pagerBackground" 
     android:visibility="gone" > 

     <RelativeLayout 
      android:id="@+id/searchRL" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_margin="10dp" 
      android:background="@drawable/fondoblancoborder" > 

      <ImageView 
       android:id="@+id/borrarEditTextIV" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" 
       android:padding="10dp" 
       android:src="@drawable/borrar_edittext" /> 

      <EditText 
       android:id="@+id/busquedaTagET" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_centerVertical="true" 
       android:layout_marginBottom="5dp" 
       android:layout_marginLeft="10dp" 
       android:layout_marginTop="5dp" 
       android:layout_toLeftOf="@+id/borrarEditTextIV" 
       android:ems="10" 
       android:imeOptions="actionSearch" 
       android:inputType="text" 
       android:paddingRight="5dp" > 
      </EditText> 
     </RelativeLayout> 

     <com.example.help.resources.FlowLayout 
      android:id="@+id/tagLL" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/searchRL" 
      android:layout_alignRight="@+id/searchRL" 
      android:layout_below="@+id/searchRL" 
      android:layout_marginBottom="2dp" > 
     </com.example.help.resources.FlowLayout> 
    </RelativeLayout> 

    <com.astuetz.PagerSlidingTabStrip 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="15dp" 
     android:paddingBottom="15dp" 
     android:background="@color/pagerBackground" /> 

</RelativeLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/viewPager" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/parentViews" > 
</android.support.v4.view.ViewPager> 

我試過還有:

  • WeakReference
  • .notifyDataChanged()
  • 創建片段播放,每次處理調用getItem()
  • setRetainInstance(true)
  • FragmentStatePagerAdapter更改爲我不記得了,因爲這是第7天用這個bug FragmentPagerAdapter
  • 的事情。

事情知道

  • getItem(int position)被稱爲
  • onCreateView被調用上的每個片段,即使它不會加載

任何線索? 在此先感謝!

回答

0

你爲什麼重寫getItemPosition()返回POSITION_NONE

@Override 
public int getItemPosition(Object object) { 
    return POSITION_NONE; 
} 

這意味着每個片段不再適用於其當前位置。

當主機視圖試圖確定項目的 位置是否已更改時調用。如果 給定項目的位置未更改,則返回POSITION_UNCHANGED;如果該項目在適配器中不存在更長的 ,則返回POSITION_NONE。

嘗試刪除此方法。

+0

發生同樣的情況:\ – 2014-12-20 21:08:11