您现在的位置: 爱51代码网 >> 范文 >> 文章正文
Android中利用Fragment显示为两屏

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] 

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    sharepoint 2010 获取用户信息Us
    设计包含max函数的队列
    随机从数组中取出指定的不重复的
    mysql主从同步延迟方案解决的学习
    青岛科学六年级下册教材分析
    生日旅行总结
    中小板生日快乐随感
    送生日快乐桑葚乳酪小蛋糕
    写给女儿的生日快乐
    总分公司财务核算
    mysql主从同步延迟方案解决的学习
    生日旅行总结
    中小板生日快乐随感
    送生日快乐桑葚乳酪小蛋糕
    写给女儿的生日快乐
    总分公司财务核算
    恢复使用繁体字可行性研究报告
    保险受益人制度相关问题的探讨
    初中生地理读图能力培养的研究
    搞笑生日祝福
    HTTP 错误 500.19 - Interna
    如何获取别人访问我图片的ip
    java线程优先的问题
    Birt 如何动态添加超链接
    TOMCAT里面的WEB-APP里的项目
    android 4.2.1 一种高效log打
    Android批量插入数据
    Android ContentProvider总结
    mac下git svn 设置代理
    盲签名 blind signature 简介
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |