学习笔记之ViewPager
2016-06-08 10:28
411 查看
转载请注明出处
[我的博客]http://www.lostbug.com
今天看ViewPager的官方文档,官方做了一个ActionBar tabs和Fragment组合的例子
在 class TabsAdapter extends FragmentPagerAdapter中
有一句代码,没怎么见过
就是这个通过ClassName获取Fragment的方法,这是源代码:
不多说啦,我只是菜
[我的博客]http://www.lostbug.com
今天看ViewPager的官方文档,官方做了一个ActionBar tabs和Fragment组合的例子
在 class TabsAdapter extends FragmentPagerAdapter中
有一句代码,没怎么见过
@Override public Fragment getItem(int position) { TabInfo info = mTabs.get(position); return Fragment.instantiate(mContext, info.clss.getName(), info.args); }
就是这个通过ClassName获取Fragment的方法,这是源代码:
/** * Create a new instance of a Fragment with the given class name. This is * the same as calling its empty constructor. * * @param context The calling context being used to instantiate the fragment. * This is currently just used to get its ClassLoader. * @param fname The class name of the fragment to instantiate. * @param args Bundle of arguments to supply to the fragment, which it * can retrieve with {@link #getArguments()}. May be null. * @return Returns a new fragment instance. * @throws InstantiationException If there is a failure in instantiating * the given fragment class. This is a runtime exception; it is not * normally expected to happen. */ public static Fragment instantiate(Context context, String fname, @Nullable Bundle args) { try { Class<?> clazz = sClassMap.get(fname); if (clazz == null) { // Class not found in the cache, see if it's real, and try to add it clazz = context.getClassLoader().loadClass(fname); if (!Fragment.class.isAssignableFrom(clazz)) { throw new InstantiationException("Trying to instantiate a class " + fname + " that is not a Fragment", new ClassCastException()); } sClassMap.put(fname, clazz); } Fragment f = (Fragment)clazz.newInstance(); if (args != null) { args.setClassLoader(f.getClass().getClassLoader()); f.mArguments = args; } return f; } catch (ClassNotFoundException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (java.lang.InstantiationException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (IllegalAccessException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } }
不多说啦,我只是菜
相关文章推荐
- 用modprobe 加载模块时,提示模块找不到的问题
- ubuntu-caffe
- 21. Merge Two Sorted Lists
- 自动make工具--autotools
- JS与Ajax Get和Post在使用上的区别实例详解
- AndroidStudio UnsupportedMethodException Unsupported method: InstantRun.getRestartDexFile().
- Powershell日常AD管理-1
- Linux信号列表详解
- 浅谈JS原生Ajax,GET和POST
- SDUT 1138 单链表操作A
- 互联网广告综述之点击率特征工程
- Dialog自定义样式及使用笔记
- Source Insight加载源码
- Unity中协程的运行
- centos6 安装 php7
- NSJSONReadingOptions参数说明
- JavaScript:正则表达式
- Loadrunner11以Java Vuser进行socket压力测试
- final关键字的应用
- 剑指offer(10)-调整数组顺序使奇数位于偶数前面