Java 何时对片段使用onCreateView?

Java 何时对片段使用onCreateView?,java,android,android-fragments,Java,Android,Android Fragments,我正在按步骤进行操作 我似乎不明白为什么他们会说,“创建片段时的一个区别是必须使用onCreateView()回调来定义布局。”但没有对HeadlinesFragment使用onCreateView,只有ArticleFragment。它们似乎都设置了一个布局,所以我不明白为什么不使用onCreateView。另外,fragment_容器只是xml文件吗?我猜它只在主要活动中使用。代码发布在 除了我在下面发布的articleFragment代码。到目前为止,我想我了解到您有一个扩展fragm

我正在按步骤进行操作

我似乎不明白为什么他们会说,“创建片段时的一个区别是必须使用onCreateView()回调来定义布局。”但没有对HeadlinesFragment使用onCreateView,只有ArticleFragment。它们似乎都设置了一个布局,所以我不明白为什么不使用onCreateView。另外,fragment_容器只是xml文件吗?我猜它只在主要活动中使用。代码发布在

除了我在下面发布的articleFragment代码。到目前为止,我想我了解到您有一个扩展fragmentactivity的类,该类包含片段容器和方法以切换出其他片段。那么,您有扩展fragment或listFragment的类的片段吗?但是这个网站,

显示了一些活动的示例,这些活动不扩展片段活动,而只是活动,并且应该包含其他片段活动

本网站:

显示了关于片段的更多信息,但生命周期没有说明何时使用OnCreate vs onCreateView,我认为这与布局有关,但它说明OnCreate也启动片段,因此我不确定使用哪种

谢谢

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);
} 
} 

如果仔细查看代码,您将看到
HeadlinesFragment
ArticleFragment
之间的差异:

public class HeadlinesFragment extends ListFragment

public class ArticleFragment extends Fragment

当然,区别在于
HeadlinesFragment
是一个
列表片段
,而不仅仅是一个普通的ol'
片段
ListFragment
已经实现了一个默认的
onCreateView()
,它使用
ListView
加载布局。如果要覆盖默认行为,可以编写自己的
onCreateView()
方法来制作更复杂的布局。

如果仔细查看代码,您将看到
HeadlinesFragment
ArticleFragment
之间的区别:

public class HeadlinesFragment extends ListFragment

public class ArticleFragment extends Fragment

当然,区别在于
HeadlinesFragment
是一个
列表片段
,而不仅仅是一个普通的ol'
片段
ListFragment
已经实现了一个默认的
onCreateView()
,它使用
ListView
加载布局。如果要覆盖默认行为,可以编写自己的
onCreateView()
方法来制作更复杂的布局。

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


headlinesFragment没有onCreateView的原因是因为它是一个列表,具有预定义的布局,因此不需要重写

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

headlinesFragment没有onCreateView的原因是因为它是一个列表,具有预定义的布局,因此不需要重写