Android中利用Fragment顯示為兩屏

來源:互聯網
上載者:User
     主要是學習了下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();        }    }

我認為這個例子中基本上就這些地方需要注意下了,如果還有更多需要注意的地方,望朋友們告知~








聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.