何时使用onCreateView进行分片? [英] When to use onCreateView for fragments?

查看:256
本文介绍了何时使用onCreateView进行分片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照以下步骤进行操作

I'm following steps on

http://developer.android.com/training/basics/fragments/creating.html#AddInLayout

和我似乎无法弄清楚为什么他们说,创建片段时的一个区别是你必须使用onCreateView()回调来定义布局。但是没有使用onCreateView作为HeadlinesFragment,只使用了ArticleFragment。他们似乎都设置了布局,所以我不明白为什么不使用onCreateView。另外,fragment_container只是xml文件吗?我猜它只用在MainActivity中。代码发布在

and I can't seem to figure out why they stated, "One difference when creating a Fragment is that you must use the onCreateView() callback to define the layout." but didn't use onCreateView for the HeadlinesFragment, only the ArticleFragment. They both seem to set a layout so I don't understand why onCreateView is not used. Also, is the fragment_container only the xml file? I'm guessing it's only used in the MainActivity. Code is posted on

Android Fragment Basics Tutorial

除了我在下面发布的articleFragment代码。到目前为止,我想我明白你有一个扩展fragment活动的类,它包含片段容器和方法来切换其他片段。那么你有扩展fragment或listFragment的类的片段?但是,该网站,

except code for articleFragment which I posted below. So far, I think I understand that you have a class that extends fragmentactivity that holds fragment container and methods to switch out other fragments. Then you have the fragments that are classes that extends fragment or listFragment? However, the site,

http://developer.samsung.com/android/technical-docs/Using-Fragments-to-Build-User-Interfaces-in-Android

显示DONT扩展片段活动的活动示例,而不仅仅是活动,它应该保留其他片段活动。

shows examples of activities that DONT extend fragment activity but rather just activity and it's supposed to hold the other fragment activities.

这个网站:

http://developer.android.com/reference/android/app/Fragment.html

显示了有关片段的更多信息,但生命周期并没有澄清何时使用OnCreate vs onCreateView,我认为它与布局有关,但它表明onCreate也开始片段,所以我不肯定使用它。

showed more information on fragments but the lifecycle doesn't clarify when to use OnCreate vs onCreateView, I'm thinking it's something in regards to the layouts but it states that onCreate starts fragment too so I'm not positive on which to use.

感谢!!!!!

package com.example.android.fragments;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ArticleFragment extends Fragment {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // If activity recreated (such as from screen rotate), restore
    // the previous article selection set by onSaveInstanceState().
    // This is primarily necessary when in the two-pane layout.
    if (savedInstanceState != null) {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }

    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.article_view, container, false);
}

@Override
public void onStart() {
    super.onStart();

    // During startup, check if there are arguments passed to the fragment.
    // onStart is a good place to do this because the layout has already been
    // applied to the fragment at this point so we can safely call the method
    // below that sets the article text.
    Bundle args = getArguments();
    if (args != null) {
        // Set article based on argument passed in
        updateArticleView(args.getInt(ARG_POSITION));
    } else if (mCurrentPosition != -1) {
        // Set article based on saved instance state defined during onCreateView
        updateArticleView(mCurrentPosition);
    }
}

public void updateArticleView(int position) {
    TextView article = (TextView) getActivity().findViewById(R.id.article_fragment);
    article.setText(Ipsum.Articles[position]);
    mCurrentPosition = position;
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    // Save the current article selection in case we need to recreate the fragment
    outState.putInt(ARG_POSITION, mCurrentPosition);
} 
} 


推荐答案

我据悉,由于片段生命周期与活动相关联,因此创建活动和片段。 onCreate用于设置活动布局,其中onCreateView设置片段布局。

I learned that since the fragment lifecycle is tied to the activity onCreate creates both the activity and the fragment. onCreate is used to set activity layout where onCreateView sets the fragment layout.

headlinesFragment没有onCreateView的原因是因为它是一个列表,它有一个预定义的布局,因此不需要被覆盖。

the reason headlinesFragment didn't have onCreateView is because since it's a list it has a predefined layout and thus doesn't need to be overridden.

这篇关于何时使用onCreateView进行分片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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