何时使用onCreateView进行分片?

我正在按照步骤进行操作

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

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

Android Fragment Basics Tutorial

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

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

显示了DONT扩展片段活动但仅仅是活动的活动示例,它应该包含其他片段活动。

这个网站:

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

显示了有关片段的更多信息,但生命周期没有说明何时使用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); } } 

如果仔细查看代码,您会看到HeadlinesFragmentArticleFragment之间的区别:

 public class HeadlinesFragment extends ListFragment public class ArticleFragment extends Fragment 

当然,不同之处在于HeadlinesFragment是一个ListFragment而不仅仅是一个简单的’ Fragment ‘。 ListFragment已经实现了一个默认的onCreateView() ,它使用ListView加载布局。 如果要覆盖默认行为,可以编写自己的onCreateView()方法以进行更复杂的布局。

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

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