什么是使用Android的碎片,而不是意见的好处?碎片、而不是、好处、意见

2023-09-12 01:27:01 作者:慢慢学会承受一切

当制定安卓,你可以设置你的目标(或最少)的SDK 4(API 1.6),并添加了Android兼容包(V4)添加支持片段。昨天我做了这一点,并成功实施片段从自定义类的数据可视化。

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.

我的问题是:什么是利益使用片段,而不是仅仅让从一个自定义对象视图,并且仍然支持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

这两种方法都非常简单的创建,并与一个活动的,比方说,有一个名单,其中工作;富> 来显示(例如,通过编程将每个来一个滚动型),所以是片段真的那么有用的,或者他们只是一个过度美化的简化得到一个的查看,如通过code以上?

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?

推荐答案

使用片段的backstack和生命周期特征的主要原因。否则,自定义视图更重量轻,易于实现。

起初,我居然试图建立使用自定义视图手机/平板电脑应用程序。一切的出现的跨越手机和平板电脑的工作,从单屏切换,甚至分裂面板。在那里我遇到了麻烦是与后退按钮和生命周期。因为我只是手动更新的观点......没有任何跟踪的看法和他们的国家的历史。因此,后退按钮并没有按预期工作,这是很难甚至重新在生命周期事件,旋转应用程序时,如最新的状态。为了解决这个问题,我不得不缠上了我的自定义视图中的片段,并使用FragmentManager使得previous状态将被保存并重新创建。

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.

我意识到回答我发布到一个类似的问题一年前后:的的http://计算器.COM / A /六十一万八千八百八十一分之一千一百一十二万六千三百九十七