本文介绍了getSupportFragmentManager().getFragments()已弃用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用viewpager和编译API23.我在以下语句的代码中显示了编译错误,但项目确实可以编译.
I am working with a viewpager and compiling API 23. I'm showing a compile error in my code for the following statement, but the project does compile.
List<Fragment> fragments = getSupportFragmentManager().getFragments();
此外,我在Android文档中找不到支持片段管理器和非支持片段管理器的此方法.有人知道这是怎么回事吗?
Furthermore, I cannot find this method in the android documentation for either the support fragment manager and the non-support one. Does anyone know what's going on here?
推荐答案
我实际上是这样做的,以获取对所有片段的引用:
I actually did this to get a reference to all the fragments:
private List<WeakReference<Fragment>> mFragList = new ArrayList<WeakReference<Fragment>>();
@Override
public void onAttachFragment (Fragment fragment) {
mFragList.add(new WeakReference(fragment));
}
public List<Fragment> getActiveFragments() {
ArrayList<Fragment> ret = new ArrayList<Fragment>();
for(WeakReference<Fragment> ref : mFragList) {
Fragment f = ref.get();
if(f != null) {
if(f.isVisible()) {
ret.add(f);
}
}
}
return ret;
}
public Fragment findFragement(String tClass) {
List<Fragment> fragments = getActiveFragments();
for (Fragment fragment : fragments) {
if (tClass.equalsIgnoreCase("Home")) {
if (fragment instanceof ToggleFragment) {
return fragment;
}
} else if (tClass.equalsIgnoreCase("Contacts")) {
if (fragment instanceof ContactFragment) {
return fragment;
}
}
}
return null;
}
这篇关于getSupportFragmentManager().getFragments()已弃用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!