如何在 FragmentPagerAdapter 中添加片段标签以便稍后引用片段?
Posted
技术标签:
【中文标题】如何在 FragmentPagerAdapter 中添加片段标签以便稍后引用片段?【英文标题】:How to add fragment Tag in a FragmentPagerAdapter to reference the fragment later? 【发布时间】:2017-10-23 07:08:44 【问题描述】:我尝试了以下方法,但没有帮助。如何从 FragmentPagerAdapter 设置片段的标签或访问标签?
@Override
public Fragment getItem(int position)
MainActivity activity = (MainActivity)mcontext;
if (position == 0)
Fragment discoverFragment = new DiscoverFragment();
activity.getSupportFragmentManager().beginTransaction().add(discoverFragment, "discover").commit();
return discoverFragment;
else if (position == 1)
Fragment myFeedFragment = new MyFeedFragment();
activity.getSupportFragmentManager().beginTransaction().add(myFeedFragment, "myfeed").commit();
return myFeedFragment;
else
Fragment filterFragment = new FilterFragment();
activity.getSupportFragmentManager().beginTransaction().add(filterFragment, "filter").commit();
return filterFragment;
【问题讨论】:
findFragmentByTag 函数可以使用This will be helpful 【参考方案1】:您似乎正确设置了标签。无论如何,这种方式应该可行。
设置标签:
MyFeedFragment myFeedFragment= new MyFeedFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, myFeedFragment, "myfeed");
fragmentTransaction.commit();
然后你可以通过搜索标签得到它:
List fragments = getSupportFragmentManager().getFragments();
Fragment discoverFrag = null;
Fragment myFeedFragment = null;
Fragment filterFragment = null;
for (int i = 0; i < fragments.size(); i++)
if (((Fragment) fragments.get(i)).getTag() == "discover")
discoverFrag = (Fragment) fragments.get(i);
if (((Fragment) fragments.get(i)).getTag() == "myfeed")
myFeedFragment = (Fragment) fragments.get(i);
if (((Fragment) fragments.get(i)).getTag() == "filter")
filterFragment = (Fragment) fragments.get(i);
【讨论】:
当我使用我的代码 java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 然后在上面试试这个。 试过你的方法。得到错误 java.lang.IllegalStateException: Can't change tag of fragment DiscoverFragment1488c4bd id=0x7f0d00a8 myfeed: is myfeed now android:switcher:2131558536:0 您需要更具体。发布您的完整堆栈跟踪和发生崩溃的代码行。此外,您想要参考的适配器和片段/活动的发布代码【参考方案2】:使用这个类作为你的适配器
class SwipeAdapter extends FragmentPagerAdapter
private Map<Integer, String> mFragmentTags;
private FragmentManager mFragmentManager;
private String mPagetile[];
public SwipeAdapter(FragmentManager fm, Context context)
super(fm);
mPagetile = context.getResources().getStringArray(R.array.pageTitle);
mFragmentManager = fm;
mFragmentTags = new HashMap<>();
@Override
public Fragment getItem(int position)
switch (position)
case 0:
return new "Your_fragment";
default:
break;
return null;
@Override
public Object instantiateItem(ViewGroup container, int position)
Object obj = super.instantiateItem(container, position);
if (obj instanceof Fragment)
Fragment f = (Fragment) obj;
String tag = f.getTag();
mFragmentTags.put(position, tag);
return obj;
public Fragment getFragment(int position)
String tag = mFragmentTags.get(position);
if (tag == null)
return null;
return mFragmentManager.findFragmentByTag(tag);
@Override
public CharSequence getPageTitle(int position)
return mPagetile[position];
@Override
public int getCount()
return "Number of fragments";
现在使用 SwipeAdapter 的实例调用 getFrament(int "position of fragment"); 希望你能明白。
【讨论】:
gettag() 方法给出 null。我们应该在 fragmentpagerAdapter 中设置 Tag 值?【参考方案3】:要检索 FragmentPagerAdapter 中使用的片段,您可以在任何地方执行此操作 (Kotlin):
for (fragment in supportFragmentManager.fragments)
if (fragment is YourFragment)
val yourFragment = fragment as YourFragment
【讨论】:
以上是关于如何在 FragmentPagerAdapter 中添加片段标签以便稍后引用片段?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 PagerAdapter 中返回多个片段(不是 FragmentPagerAdapter)
如何在 fragmentPagerAdapter 中调用 getItem(int position)?
使用 FragmentPagerAdapter 时如何获取现有片段
如何从 FragmentPagerAdapter 返回多个片段