2012-10-29 82 views
3

ViewPager中的片段有一個等效的方法,如OnPageSelected()OnPageShow()?特定頁面顯示時,我應該做些什麼?片段在ViewPager OnPageSelected()或OnPageShow()?


我設置onPageScrolled()每個片段頁適配器的隱藏狀態,我不知道爲什麼onHiddenChanged()不會被調用?

//在PageAdapter(延伸FragmentPagerAdapter)

public void onHiddenChanged(int arg0) 
    { 
     for (int i = 0; i < getCount(); i++) { 
      if (i == arg0) { 
       mAdapter.getItem(arg0).onHiddenChanged(true); 
      } else { 
       mAdapter.getItem(arg0).onHiddenChanged(false); 
      } 
     } 
    } 

//在PageActivity(延伸FragmentActivity)

public void onPageSelected(int arg0) 
{ 
    mAdapter.onHiddenChanged(arg0); 
} 
+0

解決@see這樣的:HTTP://stackoverflow.com/questions/10024739/how-to-determine-when-fragment-becomes-visible- in-viewpager/12523627#12523627 – thecr0w

回答

2

AndroidViewPager有一個listener稱爲ViewPager.OnPageChangeListener具有像

方法
onPageScrollStateChanged(int state), 

onPageScrolled(int position, float positionOffset, int positionOffsetPixels) , 

onPageSelected(int position) 

如果您想爲頁面執行一些特定任務,您需要編寫ViewPager.OnPageChangeListener並覆蓋onPageSelected(int position)方法。

更多細節see here

+0

解決! @see:http://stackoverflow.com/questions/10024739/how-to-determine-when-fragment-becomes-visible-in-viewpager/12523627#12523627 – thecr0w

1

您可以使用OnPageChangeListener。當頁面被更改時得到一個回調,例如:

mViewPager.setOnPageChangeListener(new OnPageChangeListener() { 

    @Override 
    public void onPageSelected(int position) { 
     // this will be called when the page is changed 
    } 

    @Override 
    public void onPageScrolled(int arg0, float arg1, int arg2) { 
    } 

    @Override 
    public void onPageScrollStateChanged(int arg0) { 
    } 
}); 
+0

我需要調用Fragment而不是FragmentTabsActivity,所以? – thecr0w

+0

onHiddenChanged在片段中也不起作用 – thecr0w

+0

解決! @see:HTTP://stackoverflow.com/questions/10024739/how-to-determine-when-fragment-becomes-visible-in-viewpager/12523627#12523627 – thecr0w