2013-07-30 43 views
6

即使當我設置oViewPager.setOffscreenPageLimit(0)時,我的視圖分頁器仍然在可見頁面的每一側加載1個關閉屏幕頁面。ViewPager.setOffscreenPageLimit(0)完全沒有效果。我如何只在ViewPager中一次加載一個頁面?

如何使它只有用戶幻燈片時加載的頁面呢?

我的適配器是否有幫助:

public class MainPagerAdapter extends FragmentPagerAdapter { 
    protected static final String[] CONTENT = new String[] { "page 1", "page 2", "page 3", "page 4", }; 

    private int mCount = CONTENT.length; 

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

    @Override 
    public Fragment getItem(int position) { 
     //return TestFragment.newInstance(CONTENT[position % CONTENT.length]); 
     return MainFragment.newInstance(position); 
    } 

    @Override 
    public int getCount() { 
     return mCount; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return MainPagerAdapter.CONTENT[position % CONTENT.length]; 
    } 

    public void setCount(int count) { 
     if (count > 0 && count <= 10) { 
      mCount = count; 
      notifyDataSetChanged(); 
     } 
    } 


} 

回答

3

如何使它只在用戶滑到它時加載頁面?

寫下你自己的ViewPager。正如Jani的回答所指出的,ViewPager對每邊的最小屏幕外頁面限制爲1。

這對於刷卡動畫工作的順利開展需要。否則,如果我們不得不等待您爲下一頁設置用戶界面,則用戶的刷卡將被凍結,或者刷到空白內容,因爲您需要很長時間才能建立該用戶界面。

5

另一種方法是使用一個OnPageChangeListener,每當出現一個頁面變化調用一個刷新方法在你的片段。

這將需要您設置一個更高的OffScreenPageLimit,但是這些片段將每一個被帶入視圖時更新。

pager.setOnPageChangeListener(new OnPageChangeListener() { 

    @Override 
    public void onPageSelected(int position){ 
     ((MyRefreshableFragment)pager.getAdapter().getItem(position)).refresh(); 
    } 

    @Override 
    public void onPageScrolled(int arg0, float arg1, int arg2) {} 
    @Override 
    public void onPageScrollStateChanged(int arg0) {} 
    }); 
+0

-1沒有方法getItem返回位置的片段/視圖。 AFAIK只有一個黑客如何獲取當前顯示的片段或使用可見提示 – for3st

+0

哦,對。你必須調用'pager.getAdapter()。getItem()' – edthethird

0

我使用Loader和INIT在onPageSelected的每個片段的裝載機。

+0

loader是什麼? – code511788465541441

+0

我在我的答案中添加了一個鏈接。 – Dante

+1

@Dante您能否提供您在onPageSelected中使用Loader類的代碼? –

0

有源代碼

/** 
* Set the number of pages that should be retained to either side of the 
* current page in the view hierarchy in an idle state. Pages beyond this 
* limit will be recreated from the adapter when needed. 
* 
* <p>This is offered as an optimization. If you know in advance the number 
* of pages you will need to support or have lazy-loading mechanisms in place 
* on your pages, tweaking this setting can have benefits in perceived smoothness 
* of paging animations and interaction. If you have a small number of pages (3-4) 
* that you can keep active all at once, less time will be spent in layout for 
* newly created view subtrees as the user pages back and forth.</p> 
* 
* <p>You should keep this limit low, especially if your pages have complex layouts. 
* This setting defaults to 1.</p> 
* 
* @param limit How many pages will be kept offscreen in an idle state. 
*/ 
public void setOffscreenPageLimit(int limit) { 
    if (limit < DEFAULT_OFFSCREEN_PAGES) {//DEFAULT_OFFSCREEN_PAGES = 1 
     Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to " + 
       DEFAULT_OFFSCREEN_PAGES); 
     limit = DEFAULT_OFFSCREEN_PAGES; 
    } 
    if (limit != mOffscreenPageLimit) { 
     mOffscreenPageLimit = limit; 
     populate(); 
    } 
} 

,你可以定製你的viewpager通過複製viewpager的來源,那麼改變這種方法

這樣

public class LazyViewPager extends ViewGroup { 
private static final String TAG = "LazyViewPager"; 
private static final boolean DEBUG = false; 

private static final boolean USE_CACHE = false; 

private static final int DEFAULT_OFFSCREEN_PAGES = 0;//change default to load one page,no offset,ViewPager is 1,so cache 2 Fragment 
private static final int MAX_SETTLE_DURATION = 600; // ms 
+0

' public class LazyViewPager extends ViewGroup { private static final String TAG =「LazyViewPager」; private static final boolean DEBUG = false; private static final boolean USE_CACHE = false; private static final int DEFAULT_OFFSCREEN_PAGES = 0; //假定加載頁面,ViewPager是1個,所以會加載兩個分片 private static final int MAX_SETTLE_DURATION = 600; // ms ... ' –

-1

我知道這是非常晚的答案,但是爲了上帝的緣故,您不必像有人建議的那樣編寫自己的viewpager。

讓我們的事情從開始:

如何viewpager工作:

默認情況下,它使一個項目在內存中任何一方,因此不必每次創建它。

1解決方法:

mViewPager.setOffscreenPageLimit(0); 

其結果:

請求屏幕外的頁面限制0太小;默認爲1

第二個解決方案:

您想更新的片段時,它實際上是對用戶可見,所以使用方法:

@Override 
    public void setUserVisibleHint(boolean isVisibleToUser) { 
     super.setUserVisibleHint(isVisibleToUser); 
     if (isVisibleToUser) { 
      //Do something like 
      //adapter.notifyDataSetChanged(); 
     } 
    } 

希望它可以幫助別人的未來!!!

相關問題