15. android:layout_width ="0dp" 16. android:layout_height ="match_parent" /> 17. 18.</ LinearLayout> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation= "horizontal" android:layout_width= "match_parent" android:layout_height= "match_parent" >
<fragment android:name= "com.example.android.fragments.HeadlinesFragment" android:id ="@+id/headlines_fragment" android:layout_weight ="1" android:layout_width ="0dp" android:layout_height ="match_parent" />
<fragment android:name= "com.example.android.fragments.ArticleFragment" android:id ="@+id/article_fragment" android:layout_weight ="2" android:layout_width ="0dp" android:layout_height ="match_parent" />
</ LinearLayout>
Google官方的这个例子中还对平板和普通屏幕手机进行了适配,普通手机只显示一屏,首先使用到的是文章标题列表的Fragment,当点击文章时,会用文章详情Fragment来代替文章标题列表的Fragment,这里有一点需要注意的是,需要把加入到文章详情Fragment对应的事务加入到后台的回退栈中,以便事务能够回退,重新回到文章标题列表的Fragment [java] view plaincopyprint? 01. // Replace whatever is in the fragment_container view with this fragment, 02.// and add the transaction to the back stack so the user can navigate back 03.transaction.replace(R.id. fragment_container , newFragment); 04.transaction.addToBackStack( null ); 05. 06.// Commit the transaction 07.transaction.commit(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id. fragment_container , newFragment); transaction.addToBackStack( null );
// Commit the transaction transaction.commit();
再说一下怎么在FragmentActivity中加入一个Fragment时,需要指定一个View容器,Fragment事务需要被提交 [java] view plaincopyprint? 01. // Create an instance of ExampleFragment 02.HeadlinesFragment firstFragment = new HeadlinesFragment(); 03. 04.// In case this activity was started with special instructions from an Intent, 05.// pass the Intent's extras to the fragment as arguments 06.firstFragment.setArguments(getIntent().getExtras()); 07. 08.// Add the fragment to the 'fragment_container' FrameLayout 09.getSupportFragmentManager().beginTransaction() 10. .add(R.id. fragment_container , firstFragment).commit(); // Create an instance of ExampleFragment HeadlinesFragment firstFragment = new HeadlinesFragment();
// In case this activity was started with special instructions from an Intent, // pass the Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction() .add(R.id. fragment_container , firstFragment).commit();
还有一点是这个例子中由于要对平板和普通手机进行匹配,所以自定义了一个回调接口OnHeadlineSelectedListener, 在回调方法中通过判断文章详情Fragment是否存在来区分当文章被点击时是替换当前的显示的Fragment(一屏)还是更新Fragment(两屏) [html] view plaincopyprint? 01.public void onArticleSelected( int position) { 02. // The user selected the headline of an article from the HeadlinesFragment 03. 04. // Capture the article fragment from the activity layout 05. // 查找文章详情Fragment 06. ArticleFragment articleFrag = (ArticleFragment) 07. getSupportFragmentManager().findFragmentById(R.id. article_fragment); 08. 09. if (articleFrag != null) { // 存在则更新详情Fragment 10. // If article frag is available, we're in two-pane layout... 11. 12. // Call a method in the ArticleFragment to update its content 13. articleFrag.updateArticleView(position); 14. 15. } else {小学教师实习报告 // 不存在则替换为文章详情Fragment 16. // If the frag is not available, we're in the one-pane layout and must swap frags... 17. 18. // Create fragment and give it an argument for the selected article 19. ArticleFragment newFragment = new ArticleFragment(); 20. Bundle args = new Bundle(); 21. args.putInt(ArticleFragment. ARG_POSITION , position); 22. newFragment.setArguments(args); 23. FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 24. 25. // Replace whatever is in the fragment_container view with this fragment, 26. // and add the transaction to the back stack so the user can navigate back 27. transaction.replace(R.id. fragment_container , newFragment); 28. transaction.addToBackStack( null ); 29. 30. // Commit the transaction 31. transaction.commit(); 32. }
上一页 [1] [2]
|