主要是学习了下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()方法就是实现了这点~
        @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container,                    Bundle savedInstanceState) {             Log. i( "INFO", "onCreateView : " + (currentPageNum + 1));                          ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.per_pager1 ,                           container, false );              switch (currentPageNum ) {              case 0:                    rootView.setBackgroundResource(R.drawable. page1_bg );                     break ;              case 1:                    rootView.setBackgroundResource(R.drawable. page2_bg );                     break ;              case 2:                    rootView.setBackgroundResource(R.drawable. page3_bg );                     break ;              default :                     break ;             }              return rootView;       }

在这一篇中通过配置文件来创建Fragment,这样可能会更方便和直观
Google官方提供的这个例子中用到了 ListFragment,你可以把它看成是一个列表Fragment,它在内部内置了一个ListView,并对它进行了有效的管理,非常的方便和实用,它是继承于 Fragment
在配置文件中配置Fragment时,注意要指定Fragment的类全名,Android系统在运行时是根据这个来构建Fragment实例
< 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
             // 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事务需要被提交
             // 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(两屏)
     public void onArticleSelected( int position) {        // The user selected the headline of an article from the HeadlinesFragment        // Capture the article fragment from the activity layout        // 查找文章详情Fragment        ArticleFragment articleFrag = (ArticleFragment)                getSupportFragmentManager().findFragmentById(R.id. article_fragment);        if (articleFrag != null) { // 存在则更新详情Fragment            // If article frag is available, we're in two-pane layout...            // Call a method in the ArticleFragment to update its content            articleFrag.updateArticleView(position);        } else { // 不存在则替换为文章详情Fragment            // If the frag is not available, we're in the one-pane layout and must swap frags...            // Create fragment and give it an argument for the selected article            ArticleFragment newFragment = new ArticleFragment();            Bundle args = new Bundle();            args.putInt(ArticleFragment. ARG_POSITION , position);            newFragment.setArguments(args);            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();            // 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();        }    }

我认为这个例子中基本上就这些地方需要注意下了,如果还有更多需要注意的地方,望朋友们告知~










更多相关文章

  1. 开发技术前线 第十一期
  2. Android(安卓)Tint使用
  3. Android:线性布局(LinearFrame)例子
  4. Android(安卓)ContentResolver 使用例子,读取联系人
  5. Android(安卓)连续点击例子一个
  6. android 视频录制 例子源码
  7. android 播放音效 小例子
  8. OpenGL ES for Android(安卓)总览
  9. Android(安卓)上层界面到内核代码的完整的流程分析,以alarm为例子

随机推荐

  1. 详解服务器性能测试基准体系
  2. 详谈NVMe和NVMe-oF架构和知识点
  3. 如何使用 Docker Compose 来构建一套开发
  4. 详解服务器处理器基础知识
  5. 关于InfiniBand架构和知识点漫谈
  6. Flash闪存颗粒和SSD知识深度解析
  7. 细数主流数据中心资产管理软件
  8. NVMe Over Fabrics架构概述
  9. 创建数据库
  10. php之数据库链式操作