java – 片段中的super.onCreateView

前端之家收集整理的这篇文章主要介绍了java – 片段中的super.onCreateView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我完成了针对 Android手持系统的移动应用编程的Coursera课程.
课程中的一个示例代码告诉我们如何对我们进行分片.基本上它的作用是将屏幕分成两个片段,一个用于书名,一个用于书中的引用.如果用户单击左侧片段中的标题,则从该书中关联的引用将显示在右侧的片段中.

这是mainActivity的代码

  1. package course.examples.Fragments.StaticLayout;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import course.examples.Fragments.StaticLayout.TitlesFragment.ListSelectionListener;
  7.  
  8. public class QuoteViewerActivity extends Activity implements
  9. ListSelectionListener {
  10.  
  11. public static String[] mTitleArray;
  12. public static String[] mQuoteArray;
  13. private QuotesFragment mDetailsFragment;
  14.  
  15. private static final String TAG = "QuoteViewerActivity";
  16.  
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. mTitleArray = getResources().getStringArray(R.array.Titles);
  21. mQuoteArray = getResources().getStringArray(R.array.Quotes);
  22.  
  23. setContentView(R.layout.main);
  24.  
  25. mDetailsFragment = (QuotesFragment) getFragmentManager()
  26. .findFragmentById(R.id.details);
  27. }
  28.  
  29. @Override
  30. public void onListSelection(int index) {
  31. if (mDetailsFragment.getShownIndex() != index) {
  32. mDetailsFragment.showQuoteAtIndex(index);
  33. }
  34. }

这是Quote片段的代码

  1. package course.examples.Fragments.StaticLayout;
  2.  
  3. import android.app.Activity;
  4. import android.app.Fragment;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.TextView;
  11.  
  12. public class QuotesFragment extends Fragment {
  13.  
  14. private TextView mQuoteView = null;
  15. private int mCurrIdx = -1;
  16. private int mQuoteArrayLen;
  17.  
  18. private static final String TAG = "QuotesFragment";
  19.  
  20. public int getShownIndex() {
  21. return mCurrIdx;
  22. }
  23.  
  24. public void showQuoteAtIndex(int newIndex) {
  25. if (newIndex < 0 || newIndex >= mQuoteArrayLen)
  26. return;
  27. mCurrIdx = newIndex;
  28. mQuoteView.setText(QuoteViewerActivity.mQuoteArray[mCurrIdx]);
  29. }
  30. @Override
  31. public void onAttach(Activity activity) {
  32. Log.i(TAG,getClass().getSimpleName() + ":entered onAttach()");
  33. super.onAttach(activity);
  34. }
  35.  
  36. @Override
  37. public void onCreate(Bundle savedInstanceState) {
  38. Log.i(TAG,getClass().getSimpleName() + ":entered onCreate()");
  39. super.onCreate(savedInstanceState);
  40. }
  41.  
  42. @Override
  43. public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
  44. **return inflater.inflate(R.layout.quote_fragment,container,false);**
  45. }
  46.  
  47. @Override
  48. public void onActivityCreated(Bundle savedInstanceState) {
  49. super.onActivityCreated(savedInstanceState);
  50. mQuoteView = (TextView) getActivity().findViewById(R.id.quoteView);
  51. mQuoteArrayLen = QuoteViewerActivity.mQuoteArray.length;
  52. }

这是标题片段的代码

  1. package course.examples.Fragments.StaticLayout;
  2.  
  3. import android.app.Activity;
  4. import android.app.ListFragment;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.ListView;
  12.  
  13. public class TitlesFragment extends ListFragment {
  14. private ListSelectionListener mListener = null;
  15. private static final String TAG = "TitlesFragment";
  16.  
  17. public interface ListSelectionListener {
  18. public void onListSelection(int index);
  19. }
  20.  
  21. @Override
  22. public void onListItemClick(ListView l,View v,int pos,long id) {
  23. getListView().setItemChecked(pos,true);
  24. mListener.onListSelection(pos);
  25. }
  26.  
  27. @Override
  28. public void onAttach(Activity activity) {
  29. super.onAttach(activity);
  30. try {
  31. mListener = (ListSelectionListener) activity;
  32. } catch (ClassCastException e) {
  33. throw new ClassCastException(activity.toString()
  34. + " must implement OnArticleSelectedListener");
  35. }
  36. }
  37.  
  38. @Override
  39. public void onCreate(Bundle savedInstanceState) {
  40. Log.i(TAG,getClass().getSimpleName() + ":entered onCreate()");
  41. super.onCreate(savedInstanceState);
  42. }
  43.  
  44. @Override
  45. public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
  46. Log.i(TAG,getClass().getSimpleName() + ":entered onCreate()");
  47.  
  48. **return super.onCreateView(inflater,savedInstanceState);**
  49.  
  50. }
  51.  
  52. @Override
  53. public void onActivityCreated(Bundle savedState) {
  54. super.onActivityCreated(savedState);
  55.  
  56. getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  57. setListAdapter(new ArrayAdapter<String>(getActivity(),R.layout.title_item,QuoteViewerActivity.mTitleArray));
  58. }

MainActivity xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:baselineAligned="false"
  6. android:orientation="horizontal" >
  7.  
  8. <fragment
  9. android:id="@+id/titles"
  10. android:layout_width="0px"
  11. android:layout_height="match_parent"
  12. android:layout_weight="1"
  13. class="course.examples.Fragments.StaticLayout.TitlesFragment" />
  14.  
  15. <fragment
  16. android:id="@+id/details"
  17. android:layout_width="0px"
  18. android:layout_height="match_parent"
  19. android:layout_weight="2"
  20. class="course.examples.Fragments.StaticLayout.QuotesFragment" />
  21.  
  22. </LinearLayout>

引用片段xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <TextView
  8. android:id="@+id/quoteView"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:padding="5dip"
  12. android:textSize="32sp" >
  13. </TextView>
  14. </LinearLayout>

标题片段xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="?android:attr/activatedBackgroundIndicator"
  6. android:orientation="vertical"
  7. android:padding="5dip"
  8. android:textSize="32sp" >
  9.  
  10. </TextView>

我的问题是为什么onCreateView下的方法在Quote片段和Title片段下有所不同?
QuoteFragment返回inflater.inflate(R.layout.quote_fragment,false);
TitleFragment返回super.onCreateView(inflater,savedInstanceState);

解决方法

因为QuotesFragment扩展了默认情况下没有布局的Fragment,并且用户必须膨胀并返回自己的布局.这就是Fragment的onCreateView方法的样子:
  1. public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
  2. return null;
  3. }

TitlesFragment扩展了ListFragment,它默认包含一个布局,包含项目的ListView,列表为空时的标签的TextView和ProgressBar.在这种情况下,用户不必返回他自己的视图并且可以返回超级调用获得的对象. ListFragment的onCreateView:

  1. public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) {
  2. return inflater.inflate(com.android.internal.R.layout.list_content,false);
  3. }

猜你在找的Java相关文章