我使用一个ViewPager和一个FragmentStatePagerAdapter来托管三个不同的片段:
(Fragment1) (Fragment2) (Fragment3)
当我想从FragmentActivity中的ViewPager中获取Fragment1时。
问题是什么,我该如何解决它?
我使用一个ViewPager和一个FragmentStatePagerAdapter来托管三个不同的片段:
(Fragment1) (Fragment2) (Fragment3)
当我想从FragmentActivity中的ViewPager中获取Fragment1时。
问题是什么,我该如何解决它?
当前回答
我知道这有几个答案,但也许这能帮助到一些人。当我需要从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;
}
其他回答
在TabLayout中有多个标签片段。你可以通过标签找到片段,使用片段的索引。
例如,Fragment1的索引是0,所以在findFragmentByTag()方法中,传递Viewpager的标记。使用fragmentTransaction后可以添加、替换fragment。
字符串标签= "android:switcher:" + R.id.viewPager + ":" + 0; Fragment1 f = (Fragment1) getSupportFragmentManager().findFragmentByTag(tag);
在片段
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();
}
}
主要答案依赖于框架生成的名称。如果这种情况发生变化,那么它将不再起作用。
这个解决方案怎么样,重写你的Fragment(State)PagerAdapter的instantiateItem()和destroyItem():
public class MyPagerAdapter extends FragmentStatePagerAdapter {
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return ...;
}
@Override
public Fragment getItem(int position) {
return MyFragment.newInstance(...);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
在处理可用的Fragments时,这似乎对我有用。尚未实例化的片段在调用getRegisteredFragment时将返回null。但我一直在使用这个主要是为了获得当前片段的ViewPager: adapter . getregisteredfragment (viewpage . getcurrentitem()),这不会返回null。
我不知道这个解决方案还有什么其他缺点。如果有的话,我想知道。
我知道这有几个答案,但也许这能帮助到一些人。当我需要从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;
}
我找不到一个简单干净的方法来做这件事。然而,ViewPager小部件只是另一个ViewGroup,它承载着你的片段。ViewPager将这些片段作为直接子片段。所以你可以遍历它们(使用. getchildcount()和. getchildat()),看看你正在寻找的片段实例当前是否加载到ViewPager中,并获得对它的引用。例如,你可以使用一些静态的唯一ID字段来区分片段。
注意,ViewPager可能没有加载你正在寻找的片段,因为它是一个像ListView一样的虚拟化容器。