在一些场景里, TabLayout
会对应一组Fragment,然后通过 onTabSelected(TabLayout.Tab tab)
来触发Tab所对应Fragment的 add()
(实例化) show()
与 hide()
,那么如何尽量少写代码来实现这个场景呢?
我的解决方案是用 Tag
、 反射原理 以及 FragmentManager
的 findFragmentByTag(String tag)
方法
将Fragment的类的限定名作为
Tag
附加到Tab
上onTabSelected(TabLayout.Tab tab)
时获取Tab
对应的Tag
这时获得的就是Fragment的限定名利用反射通过限定名实例化Fragment并
add()
show()
对应的Fragmenthide()
也是同样的道理
代码片段:
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
final TabLayout.Tab tab1 = tabLayout
.newTab()
.setText(R.string.text_1)
.setTag(Tab1Fragment.class.getName()); //用Fragment的限定名作为Tag
TabLayout.Tab tab2 = tabLayout
.newTab()
.setText(R.string.text_2)
.setTag(Tab2Fragment.class.getName());
TabLayout.Tab tab3 = tabLayout
.newTab()
.setText(R.string.text_3)
.setTag(Tab3Fragment.class.getName());
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
FragmentUtils.showBeforeAddByTag(SomeActivity.this, R.id.frame_layout_fragment_container, (String) tab.getTag()); //用Tab的Tag来进一步完成Fragment的add show hide
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
FragmentUtils.hideByTag(SomeActivity.this, (String) tab.getTag());
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
FragmentUtils代码片段:
//通过反射实例化Fragment
@Nullable
public static Fragment reflectionNewInstance(@NonNull String fragmentClassName, Object... args) {
Class<?>[] parameterTypes;
if (args == null) {
parameterTypes = new Class[0];
} else {
parameterTypes = new Class[args.length];
for (int i = 0; i < args.length; i++) {
parameterTypes[i] = args[i].getClass();
}
}
try {
Method method = Class.forName(fragmentClassName).getMethod("newInstance", parameterTypes);
return (Fragment) method.invoke(null, args);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
//添加Fragment
public static Fragment addByTag(FragmentActivity fragmentActivity, int containerResourceId, String tag, Bundle arguments) {
FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
Fragment fragment = reflectionNewInstance(tag);
fragment.setArguments(arguments);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(containerResourceId, fragment, tag);
fragmentTransaction.commit();
return fragment;
}
//显示Fragment
public static void showByTag(FragmentActivity fragmentActivity, String tag) {
FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag); //通过Tab上的tag查找Fragment实例
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.show(fragment);
fragmentTransaction.commit();
}
//隐藏Fragment
public static void hideByTag(FragmentActivity fragmentActivity, String tag) {
FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);
if (fragment != null) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.hide(fragment);
fragmentTransaction.commit();
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。