Android中利用Fragment显示为两屏

 主要是学习了下Google官方的一个小例子( http://developer.android.com/training/basics/fragments/index.html ),如何在平板上显示为两屏,这个对类似于新闻类的应用比较适合,先看下效果图~



     上两篇文章中是通过ViewPager的适配器 FragmentPagerAdapter FragmentStatePagerAdapter  来使用Fragment的,我们也可以直接在Activity中使用Fragment,Android SDK  v4+ Support 中为我们提供了 FragmentActivity  来对Fragment进行管理,使用Fragment时需要明白的一点是,Fragment的布局文件(不管是静态布局文件还是动态创建)会被加入到容纳它的View容器中 ,还记得上一篇中动态创建Fragment时怎么创建一个返回的View吗,其中的LayoutInflater的inflate()方法就是实现了这点~
[java]  view plain copy
  1.  @Override  
  2.  public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  3.              Bundle savedInstanceState) {  
  4.   
  5.       Log. i( "INFO""onCreateView : " + (currentPageNum + 1));  
  6.         
  7.       ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.per_pager1 ,  
  8.                     container, false );  
  9.   
  10.        switch (currentPageNum ) {  
  11.        case 0:  
  12.              rootView.setBackgroundResource(R.drawable. page1_bg );  
  13.               break ;  
  14.        case 1:  
  15.              rootView.setBackgroundResource(R.drawable. page2_bg );  
  16.               break ;  
  17.        case 2:  
  18.              rootView.setBackgroundResource(R.drawable. page3_bg );  
  19.               break ;  
  20.        default :  
  21.               break ;  
  22.       }  
  23.   
  24.        return rootView;  
  25.   
  26. }  

在这一篇中通过配置文件来创建Fragment,这样可能会更方便和直观

     Google官方提供的这个例子中用到了 ListFragment ,你可以把它看成是一个列表Fragment,它在内部内置了一个ListView,并对它进行了有效的管理,非常的方便和实用,它是继承于 Fragment  

     在配置文件中配置Fragment时,注意要指定Fragment的类全名,Android系统在运行时是根据这个来构建Fragment实例  
[html]  view plain copy
  1. < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
  2.     android:orientation"horizontal"  
  3.     android:layout_width"match_parent"  
  4.     android:layout_height"match_parent" >  
  5.   
  6.     <fragment android:name"com.example.android.fragments.HeadlinesFragment"  
  7.               android:id ="@+id/headlines_fragment"  
  8.               android:layout_weight ="1"  
  9.               android:layout_width ="0dp"  
  10.               android:layout_height ="match_parent" />  
  11.   
  12.     <fragment android:name"com.example.android.fragments.ArticleFragment"  
  13.               android:id ="@+id/article_fragment"  
  14.               android:layout_weight ="2"  
  15.               android:layout_width ="0dp"  
  16.               android:layout_height ="match_parent" />  
  17.   
  18. </ LinearLayout>  

     Google官方的这个例子中还对平板和普通屏幕手机进行了适配,普通手机只显示一屏,首先使用到的是文章标题列表的Fragment,当点击文章时,会用文章详情Fragment来代替文章标题列表的Fragment,这里有一点需要注意的是,需要把加入到文章详情Fragment对应的事务加入到后台的回退栈中,以便事务能够回退,重新回到文章标题列表的Fragment
[java]  view plain copy
  1.  // Replace whatever is in the fragment_container view with this fragment,  
  2. // and add the transaction to the back stack so the user can navigate back  
  3. transaction.replace(R.id. fragment_container , newFragment);  
  4. transaction.addToBackStack( null );  
  5.   
  6. // Commit the transaction  
  7. transaction.commit();  

再说一下怎么在FragmentActivity中加入一个Fragment时,需要指定一个View容器,Fragment事务需要被提交
[java]  view plain copy
  1.  // Create an instance of ExampleFragment  
  2. HeadlinesFragment firstFragment = new HeadlinesFragment();  
  3.   
  4. // In case this activity was started with special instructions from an Intent,  
  5. // pass the Intent's extras to the fragment as arguments  
  6. firstFragment.setArguments(getIntent().getExtras());  
  7.   
  8. // Add the fragment to the 'fragment_container' FrameLayout  
  9. getSupportFragmentManager().beginTransaction()  
  10.         .add(R.id. fragment_container , firstFragment).commit();  

     还有一点是这个例子中由于要对平板和普通手机进行匹配,所以自定义了一个回调接口 OnHeadlineSelectedListener,    在回调方法中通过判断文章详情Fragment是否存在来区分当文章被点击时是替换当前的显示的Fragment(一屏)还是更新Fragment(两屏)
[html]  view plain copy
  1. public void onArticleSelected( int position) {  
  2.    // The user selected the headline of an article from the HeadlinesFragment  
  3.   
  4.    // Capture the article fragment from the activity layout  
  5.    // 查找文章详情Fragment  
  6.    ArticleFragment articleFrag = (ArticleFragment)  
  7.            getSupportFragmentManager().findFragmentById(R.id. article_fragment);  
  8.   
  9.    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.    }  

我认为这个例子中基本上就这些地方需要注意下了,如果还有更多需要注意的地方,望朋友们告知~
文章出自:http://blog.csdn.net/tu_bingbing/article/details/8760977
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值