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()方法就是實現了這點~@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(); } }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!