为进行开发时Android
,您可以将目标(或最低)sdk设置为4(API 1.6),并添加android兼容性软件包(v4)以添加对的支持Fragments
。昨天,我这样做了,并成功实现Fragments
了可视化来自自定义类的数据。
我的问题是:与Fragments
仅从自定义对象获取View并仍支持API 1.5相比,使用它有什么好处?
例如,假设我有Foo.java类:
public class Foo extends Fragment {
/** Title of the Foo object*/
private String title;
/** A description of Foo */
private String message;
/** Create a new Foo
* @param title
* @param message */
public Foo(String title, String message) {
this.title = title;
this.message = message;
}//Foo
/** Retrieves the View to display (supports API 1.5. To use,
* remove 'extends Fragment' from the class statement, along with
* the method {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)})
* @param context Used for retrieving the inflater */
public View getView(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.foo, null);
TextView t = (TextView) v.findViewById(R.id.title);
t.setText(this.title);
TextView m = (TextView) v.findViewById(R.id.message);
m.setText(this.message);
return v;
}//getView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
View v = inflater.inflate(R.layout.foo, null);
TextView t = (TextView) v.findViewById(R.id.title);
t.setText(this.title);
TextView m = (TextView) v.findViewById(R.id.message);
m.setText(this.message);
return v;
}//onCreateView
}//Foo
这两种方法都非常容易创建和使用,例如在List<Foo>
要显示的Activity中使用(例如,以编程方式将每个方法添加到ScrollView
),因此它们Fragments
真的很有用,还是只是过度夸张地简化了获得一个视图,例如通过上面的代码?