首页 > 其他 > 详细

fragment的使用

时间:2014-02-19 02:01:13      阅读:408      评论:0      收藏:0      [点我收藏+]

在运行的Activity中添加一个Fragment

一个处理Fragment的重要原则,Fragment必须有一个在布局中有一个让其寄存的View容器,特别是对那些运行时的Fragment。

以下是一个布局文件,每次显示一个Fragment。为了用一个Fragment替换另外一个,Activity的布局文件包含一个空的FrameLayout,作为Fragment容器使用。

  1. res/layout/news_articles.xml:
  2. <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:id="@+id/fragment_container"
  4.    android:layout_width="match_parent"
  5.    android:layout_height="match_parent" />          

在Activity当中,利用getSupportFragmentManager()获取一个FragmentManager,以下是代码示例:

  1. import android.os.Bundle;
  2. import android.support.v4.app.FragmentActivity;
  3. public class MainActivity extends FragmentActivity {
  4.     @Override
  5.     public voidonCreate(Bundle savedInstanceState) {
  6.        super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.news_articles);
  8.         // Checkthat the activity is using the layout version with
  9.         // thefragment_container FrameLayout
  10.         if(findViewById(R.id.fragment_container) != null) {
  11.             //However, if we‘re being restored from a previous state,
  12.             // thenwe don‘t need to do anything and should return or else
  13.             // wecould end up with overlapping fragments.
  14.             if(savedInstanceState != null) {
  15.                return;
  16.             }
  17.             // Create an instance of ExampleFragment
  18.            HeadlinesFragment firstFragment = new HeadlinesFragment();
  19.             // Incase this activity was started with special instructions from an Intent,
  20.             // passthe Intent‘s extras to the fragment as arguments
  21.            firstFragment.setArguments(getIntent().getExtras());
  22.             // Addthe fragment to the ‘fragment_container‘ FrameLayout
  23.            getSupportFragmentManager().beginTransaction()
  24.                     .add(R.id.fragment_container,firstFragment).commit();
  25.         }
  26.     }
  27. }

Fragment替换


替换Fragment的流程和添加的流程类似,只不过需要利用replace方法替换add方法。

需要记住的是,当你是Fragment切换的时候,例如替换或者一处,最好让用户拥有返回或者撤销的机会

当你允许用户返回或者撤销Fragment操作的时候,你必须在调用FragmentTransaction之前调用addToBackStack。

以下是一个示例:

    1. // Create fragment and give it an argument specifying thearticle it should show
    2. ArticleFragment newFragment = new ArticleFragment();
    3. Bundle args = new Bundle();
    4. args.putInt(ArticleFragment.ARG_POSITION, position);
    5. newFragment.setArguments(args);
    6. FragmentTransaction transaction =getSupportFragmentManager().beginTransaction();
    7. // Replace whatever is in the fragment_container viewwith this fragment,
    8. // and add the transaction to the back stack so the usercan navigate back
    9. transaction.replace(R.id.fragment_container,newFragment);
    10. transaction.addToBackStack(null);
    11. // Commit the transaction
    12. transaction.commit();

fragment的使用

原文:http://www.cnblogs.com/ff0o0/p/3554324.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!