我使用一个ViewPager和一个FragmentStatePagerAdapter来托管三个不同的片段:
(Fragment1) (Fragment2) (Fragment3)
当我想从FragmentActivity中的ViewPager中获取Fragment1时。
问题是什么,我该如何解决它?
我使用一个ViewPager和一个FragmentStatePagerAdapter来托管三个不同的片段:
(Fragment1) (Fragment2) (Fragment3)
当我想从FragmentActivity中的ViewPager中获取Fragment1时。
问题是什么,我该如何解决它?
当前回答
为FragmentPagerAdapter添加下一个方法:
public Fragment getActiveFragment(ViewPager container, int position) {
String name = makeFragmentName(container.getId(), position);
return mFragmentManager.findFragmentByTag(name);
}
private static String makeFragmentName(int viewId, int index) {
return "android:switcher:" + viewId + ":" + index;
}
getActiveFragment(0)必须工作。
下面是在ViewPager https://gist.github.com/jacek-marchwicki/d6320ba9a910c514424d中实现的解决方案。如果出现故障,你会看到良好的崩溃日志。
其他回答
我找不到一个简单干净的方法来做这件事。然而,ViewPager小部件只是另一个ViewGroup,它承载着你的片段。ViewPager将这些片段作为直接子片段。所以你可以遍历它们(使用. getchildcount()和. getchildat()),看看你正在寻找的片段实例当前是否加载到ViewPager中,并获得对它的引用。例如,你可以使用一些静态的唯一ID字段来区分片段。
注意,ViewPager可能没有加载你正在寻找的片段,因为它是一个像ListView一样的虚拟化容器。
我知道这有几个答案,但也许这能帮助到一些人。当我需要从ViewPager中获取片段时,我使用了一个相对简单的解决方案。在持有ViewPager的Activity或Fragment中,你可以使用这段代码循环遍历它持有的每个Fragment。
FragmentPagerAdapter fragmentPagerAdapter = (FragmentPagerAdapter) mViewPager.getAdapter();
for(int i = 0; i < fragmentPagerAdapter.getCount(); i++) {
Fragment viewPagerFragment = fragmentPagerAdapter.getItem(i);
if(viewPagerFragment != null) {
// Do something with your Fragment
// Check viewPagerFragment.isResumed() if you intend on interacting with any views.
}
}
如果你知道片段在ViewPager中的位置,你可以调用getItem(knownPosition)。 如果你不知道Fragment在ViewPager中的位置,你可以用getUniqueId()这样的方法让你的子Fragment实现一个接口,并使用它来区分它们。或者你可以循环遍历所有片段并检查类类型,例如if(viewPagerFragment instanceof FragmentClassYouWant)
! !编辑! !
I have discovered that getItem only gets called by a FragmentPagerAdapter when each Fragment needs to be created the first time, after that, it appears the the Fragments are recycled using the FragmentManager. This way, many implementations of FragmentPagerAdapter create new Fragments in getItem. Using my above method, this means we will create new Fragments each time getItem is called as we go through all the items in the FragmentPagerAdapter. Due to this, I have found a better approach, using the FragmentManager to get each Fragment instead (using the accepted answer). This is a more complete solution, and has been working well for me.
FragmentPagerAdapter fragmentPagerAdapter = (FragmentPagerAdapter) mViewPager.getAdapter();
for(int i = 0; i < fragmentPagerAdapter.getCount(); i++) {
String name = makeFragmentName(mViewPager.getId(), i);
Fragment viewPagerFragment = getChildFragmentManager().findFragmentByTag(name);
// OR Fragment viewPagerFragment = getFragmentManager().findFragmentByTag(name);
if(viewPagerFragment != null) {
// Do something with your Fragment
if (viewPagerFragment.isResumed()) {
// Interact with any views/data that must be alive
}
else {
// Flag something for update later, when this viewPagerFragment
// returns to onResume
}
}
}
你需要这个方法。
private static String makeFragmentName(int viewId, int position) {
return "android:switcher:" + viewId + ":" + position;
}
我用一种不同的方法简单地实现了这一点。
我的自定义FragmentAdapter。getItem方法返回的不是新的MyFragment(),而是在FragmentAdapter构造函数中创建的MyFragment实例。
在我的活动中,我然后从适配器获得片段,检查它是否是需要的片段的instanceOf,然后强制转换和使用所需的方法。
最简单、最简洁的方法。如果你在ViewPager中所有的片段都属于不同的类,你可以像下面这样检索并区分它们:
public class MyActivity extends Activity
{
@Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
if (fragment.getClass() == MyFragment.class) {
mMyFragment = (MyFragment) fragment;
}
}
}
在片段
public int getArgument(){
return mPage;
{
public void update(){
}
在FragmentActivity
List<Fragment> fragments = getSupportFragmentManager().getFragments();
for(Fragment f:fragments){
if((f instanceof PageFragment)&&(!f.isDetached())){
PageFragment pf = (PageFragment)f;
if(pf.getArgument()==pager.getCurrentItem())pf.update();
}
}