編輯:問題已解決。請參閱下面的評論...使用fragmentpageradapter獲取當前片段
我想實現一個OnPageChangeListener到我的ViewPager。我有問題確定活動片段(命名類myFragment)。某處在我TabsAdapter這個片段設置:
@Override
public Fragment getItem(int position)
{
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
我認爲,info.clss.getName()就像是myFragment.class.getName()。 如果我把這種:
FragmentManager fm = getSupportFragmentManager();
然後myfragment.class.getName()出現在屬性。但是,如果我打電話給:
Fragment test = fm.findFragmentByTag(myfragment.class.getName());
測試爲空。
見FM是什麼在運行時:
fm FragmentManagerImpl (id=830007952696)
mActive ArrayList (id=830007771376)
array Object[12] (id=830008021856)
[0] fragment1 (id=830007965416)
[1] myFragment (id=830008076232)
[2] null
...
size 2
mActivity tabActivity (id=830007948592)
mAdded ArrayList (id=830007781264)
mAvailBackStackIndices null
mAvailIndices null
mBackStack null
mBackStackChangeListeners null
mBackStackIndices null
mCreatedMenus null
mCurState 5
mDestroyed false
mExecCommit FragmentManagerImpl$1 (id=830007953504)
mExecutingActions false
mHavePendingDeferredStart false
mNeedMenuInvalidate false
mNoTransactionsBecause null
mPendingActions ArrayList (id=830007908880)
mStateArray null
mStateBundle null
mStateSaved false
mTmpActions Runnable[1] (id=830007783384)
的另一個問題是,該標籤指示未設置爲活動選項卡。
如果我使用Fragment.instatiate碎片永遠不會有一個ID或標籤! – wog 2012-07-20 08:06:09
獲取片段ID或標記的一種方法是在調用onCreateView()時調用myFragment.getID。我試圖設置一個標籤,並編寫更新方法...這工作。但是,因爲如果調用OnWheelChangedListener時更新方法曾經被調用過,所以很慢。我有六個輪子...比我發現這個:http://stackoverflow.com/questions/9779397/detect-viewpager-tab-change-inside-fragment – wog 2012-07-21 21:07:22