Fragment的概念与使用(二)--构建灵活的Android应用UI


当你的应用需要支持很多种屏幕尺寸,你可以在不同的布局配置中重用你的Fragment组件在可用的屏幕尺寸上优化用户体验。

例如,在手持设备中一次只显示一个Fragment可能是一个合适的选择,当然在Pad设备中将两个Fragment拼在一起来填满更大尺寸的屏幕,同时为用户显示更多的信息。


在上图中,两个Fragment,配合不同的配置显示在不同尺寸的屏幕上的效果。

FragmentManager类提供了运行时在Activity上添加,移除或者替换Fragment的方法,从而可以制造出动态的体验。

在运行的Activity中添加一个Fragment


你可以在运行中的Activity中添加Fragment,如果你想在Activity的生命周期中改变Fragment。

FragmentManager类提供了运行时在Activity上添加,移除或者替换Fragment的方法,从而可以制造出动态的体验。

如果你的Activity允许Fragment被移除和替换,你需要在Activity的onCreate方法中添加初始化的Fragment(s)。

一个处理Fragment的重要原则,Fragment必须有一个在布局中有一个让其寄存的View容器,特别是对那些运行时的Fragment。

以下是一个布局文件,每次显示一个Fragment。为了用一个Fragment替换另外一个,Activity的布局文件包含一个空的FrameLayout,作为Fragment容器使用。

  1. res/layout/news_articles.xml:
  2. <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:id="@+id/fragment_container"
  4.    android:layout_width="match_parent"
  5.    android:layout_height="match_parent" />

.

在Activity当中,利用getSupportFragmentManager()获取一个FragmentManager,以下是代码示例:

  1. import android.os.Bundle;
  2. import android.support.v4.app.FragmentActivity;
  3.  
  4. public class MainActivity extends FragmentActivity {
  5.     @Override
  6.     public voidonCreate(Bundle savedInstanceState) {
  7.        super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.news_articles);
  9.  
  10.         // Checkthat the activity is using the layout version with
  11.         // thefragment_container FrameLayout
  12.         if(findViewById(R.id.fragment_container) != null) {
  13.  
  14.             //However, if we're being restored from a previous state,
  15.             // thenwe don't need to do anything and should return or else
  16.             // wecould end up with overlapping fragments.
  17.             if(savedInstanceState != null) {
  18.                return;
  19.             }
  20.  
  21.             // Create an instance of ExampleFragment
  22.            HeadlinesFragment firstFragment = new HeadlinesFragment();
  23.            
  24.             // Incase this activity was started with special instructions from an Intent,
  25.             // passthe Intent's extras to the fragment as arguments
  26.            firstFragment.setArguments(getIntent().getExtras());
  27.            
  28.             // Addthe fragment to the 'fragment_container' FrameLayout
  29.            getSupportFragmentManager().beginTransaction()
  30.                     .add(R.id.fragment_container,firstFragment).commit();
  31.         }
  32.     }
  33. }

Fragment替换


替换Fragment的流程和添加的流程类似,只不过需要利用replace方法替换add方法。

需要记住的是,当你是Fragment切换的时候,例如替换或者一处,最好让用户拥有返回或者撤销的机会

当你允许用户返回或者撤销Fragment操作的时候,你必须在调用FragmentTransaction之前调用addToBackStack。

以下是一个示例:

  1. // Create fragment and give it an argument specifying thearticle it should show
  2. ArticleFragment newFragment = new ArticleFragment();
  3. Bundle args = new Bundle();
  4. args.putInt(ArticleFragment.ARG_POSITION, position);
  5. newFragment.setArguments(args);
  6.  
  7. FragmentTransaction transaction =getSupportFragmentManager().beginTransaction();
  8.  
  9. // Replace whatever is in the fragment_container viewwith this fragment,
  10. // and add the transaction to the back stack so the usercan navigate back
  11. transaction.replace(R.id.fragment_container,newFragment);
  12. transaction.addToBackStack(null);
  13.  
  14. // Commit the transaction
  15. transaction.commit();
  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值