原文地址:[url]http://www.cnblogs.com/ihou/archive/2012/09/12/2681759.html[/url]
关于ViewPager和Fragment
1.如何获取Fragement的引用?
Fragment fragement = getSupportFragmentManager(). findFragmentByTag("android:switcher:"+R.id.ViewPage+":" + selectTab);
这个Tag可以从FragmentPagerAdapter的源码中找到,如下:
@Override
public Object instantiateItem(ViewGroup container, int position) {
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
// Do we already have this fragment?
String name = makeFragmentName(container.getId(), position);
Fragment fragment = mFragmentManager.findFragmentByTag(name);
if (fragment != null) {
if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment);
mCurTransaction.attach(fragment);
} else {
fragment = getItem(position);
if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
mCurTransaction.add(container.getId(), fragment,
makeFragmentName(container.getId(), position));
}
if (fragment != mCurrentPrimaryItem) {
fragment.setMenuVisibility(false);
fragment.setUserVisibleHint(false);
}
return fragment;
}
其中的makeFragmentName方法产生的就是Tag
makeFragmentName的源码:
1
2
3
private static String makeFragmentName(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}
2.如何更新Fragement?
如果你需要在某一时刻动态的更改Fragement的布局或重新加载布局,可以使用此方法:
/***
* 整体更新ViewPager的一个Fragement的内容
*
* @param pager
* 需要更新的ViewPager
* @param position
* 需要更新的项目索引
* @param newView
* 更新后的View
* */
private void upDataFragement(ViewPager pager, int position, View newView) {
((FrameLayout) pager.getChildAt(position)).removeAllViews();
((FrameLayout) pager.getChildAt(position)).addView(newView);
}
1
1
关于ViewPager和Fragment
1.如何获取Fragement的引用?
Fragment fragement = getSupportFragmentManager(). findFragmentByTag("android:switcher:"+R.id.ViewPage+":" + selectTab);
这个Tag可以从FragmentPagerAdapter的源码中找到,如下:
@Override
public Object instantiateItem(ViewGroup container, int position) {
if (mCurTransaction == null) {
mCurTransaction = mFragmentManager.beginTransaction();
}
// Do we already have this fragment?
String name = makeFragmentName(container.getId(), position);
Fragment fragment = mFragmentManager.findFragmentByTag(name);
if (fragment != null) {
if (DEBUG) Log.v(TAG, "Attaching item #" + position + ": f=" + fragment);
mCurTransaction.attach(fragment);
} else {
fragment = getItem(position);
if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
mCurTransaction.add(container.getId(), fragment,
makeFragmentName(container.getId(), position));
}
if (fragment != mCurrentPrimaryItem) {
fragment.setMenuVisibility(false);
fragment.setUserVisibleHint(false);
}
return fragment;
}
其中的makeFragmentName方法产生的就是Tag
makeFragmentName的源码:
1
2
3
private static String makeFragmentName(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}
2.如何更新Fragement?
如果你需要在某一时刻动态的更改Fragement的布局或重新加载布局,可以使用此方法:
/***
* 整体更新ViewPager的一个Fragement的内容
*
* @param pager
* 需要更新的ViewPager
* @param position
* 需要更新的项目索引
* @param newView
* 更新后的View
* */
private void upDataFragement(ViewPager pager, int position, View newView) {
((FrameLayout) pager.getChildAt(position)).removeAllViews();
((FrameLayout) pager.getChildAt(position)).addView(newView);
}
1
1