在 Android 中使用 Fragments 而不是 Views 有什么好处? [英] What is the benefit of using Fragments in Android, rather than Views?

查看:38
本文介绍了在 Android 中使用 Fragments 而不是 Views 有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为 Android 开发时,您可以将目标(或最低)sdk 设置为 4(API 1.6)并添加 android 兼容包 (v4) 以添加对 Fragments.昨天我这样做并成功实现了 Fragments 来可视化来自自定义类的数据.

When developing for Android, you can set your target (or minimum) sdk to 4 (API 1.6) and add the android compatibility package (v4) to add support for Fragments. Yesterday I did this and successfully implemented Fragments to visualize data from a custom class.

我的问题是:使用 Fragments 与简单地从自定义对象获取视图并仍然支持 API 1.5 相比有什么好处?

My question is this: what is the benefit for using Fragments as opposed to simply getting a View from a custom object, and still supporting API 1.5?

例如,假设我有类 Foo.java:

For example, say I have the class 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

这两种方法在 Activity 中创建和使用都非常简单,例如,有一个 List 要显示(例如,以编程方式将每个添加到 ScrollView),那么 Fragments 真的那么有用,还是只是过度简化了获取视图的方式,例如通过上面的代码?

Both methods are very simple to create and to work with in an Activity that, say, has a List<Foo> to display (for example, programmatically adding each to a ScrollView), so are Fragments really all that useful, or are they just an over-glorified simplification of getting a View, such as through the code above?

推荐答案

使用 Fragments 的主要原因是为了 backstack 和生命周期功能.否则,自定义视图的重量更轻且更易于实现.

起初,我实际上尝试使用自定义视图构建手机/平板电脑应用程序.一切似乎都可以在手机和平​​板电脑上运行,甚至可以从单面板切换到拆分面板.我遇到麻烦的地方是后退按钮和生命周期.因为我只是手动更新视图......没有任何东西可以跟踪视图的历史及其状态.因此,后退按钮没有按预期工作,甚至在生命周期事件期间(例如旋转应用程序时)也很难重新创建最新状态.为了解决这个问题,我必须将自定义视图包装在片段中并使用 FragmentManager 以便保存和重新创建以前的状态.

At first, I actually tried to build a phone/tablet app using custom views. Everything appeared to work across phones AND tablets, even switching from single panel to split panel. Where I ran into trouble was with the back button and life cycle. Since I was simply updating views manually...there was nothing keeping track of the history of views and their states. Therefore, the back button did not work as expected and it was difficult to recreate even the latest state during life cycle events, such as when rotating the app. To fix that, I had to wrap my custom views in fragments and use the FragmentManager so that the previous states would be saved and recreated.

我在回答后意识到我在一年前发布了一个类似的问题: https://stackoverflow.com/a/11126397/618881

这篇关于在 Android 中使用 Fragments 而不是 Views 有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆