【发布时间】:2016-01-25 11:51:29
【问题描述】:
方法一
public static MyFragment newInstance(int index) {
MyFragment f = new MyFragment();
Bundle args = new Bundle(1);
args.putInt("index", index);
f.setArguments(args);
return f;
}
使用
Myfragment.newInstance(1);
方法二
public MyFragment newInstance(int index) {
Bundle args = new Bundle(1);
args.putInt("index", index);
setArguments(args);
return this;
}
使用
new Myfragment().newInstance(1);
在上面的 sn-ps 中,哪一个是更合适和更可取的方式,请指出原因?
现在正在这样做..
List<Fragment> fragments = new ArrayList<>();
fragments.add(new MyFragment().newInstance(defaultId));
int i = 1;
for (Categories categories : mCategories) {
String id = categories.getCategory_id();
String name = categories.getCategory_name();
// String slno = categories.getSlno();
fragments.add(new MyFragment().newInstance(defaultId));
Titles[i] = name;
i++;
}
这有什么问题吗?
【问题讨论】:
-
第一个是静态工厂方法