现在的位置: 首页 > 综合 > 正文

Android中利用Fragment显示为两屏

2013年08月02日 ⁄ 综合 ⁄ 共 3911字 ⁄ 字号 评论关闭
 
    主要是学习了下Google官方的一个小例子(http://developer.android.com/training/basics/fragments/index.html),如何在平板上显示为两屏,这个对类似于新闻类的应用比较适合,先看下效果图~



     上两篇文章中是通过ViewPager的适配器FragmentPagerAdapterFragmentStatePagerAdapter 来使用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();
        }
    }

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









抱歉!评论已关闭.