Android开发之利用Fragment建立动态UI

Android开发之利用Fragment建立动态UI

前言

如果你的APP设计成要支持范围广泛的屏幕尺寸时,在可利用的屏幕空间内,你可以通过在不同的布局配置中重用你的fragment来优化你的用户体验。

比如,一个手机设备可能适合一次只有一个fragment的单面板用户交互。相反,在更大屏幕尺寸的平板电脑上,你可能更想要两个fragment并排在一起,用来向用户展示更多信息。

这里写图片描述

如图所示,两个fragments,在同一个activity不同屏幕尺寸中用不同的配置来展示。在大屏幕上,两个fragment被并排放置,但是在手机上,一次只放置一个fragment,所以在用户导航中,两个fragment必须进行替换。

FragmentManager类提供了方法,让你在activity运行时能够对fragment进行添加,移除,替换,来达到动态的用户体验。

在activity运行时添加fragment

比起用标签在activity的布局文件中定义fragments,你也可以在activity运行时动态添加fragment,如果你在打算在activity的生命周期内替换fragment,这是必须的。

为了执行fragment的增加或者移除操作,你必须用 FragmentManager 创建一个FragmentTransaction对象, FragmentTransaction提供了用来增加、移除、替换以及其它一些操作的APIs。

如果你的activity允许fragments移除或者替换,你应该在activity的onCreate()方法中添加初始化fragment(s).

运用fragment(特别是那些你在运行时添加的)的一个很重要的规则就是在布局中你必须有一个容器view,fragment的layout将会放在这个view里面。

下面这个布局,为了从一个布局替换为另外一个布局,activity的布局包含了一个空的 FrameLayout作为fragment的容器。

res/layout/news_articles.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

在你的activity里面,用Support Library APIs调用 getSupportFragmentManager()方法获取FragmentManager 对象,然后调用 beginTransaction() 方法创建一个FragmentTransaction对象,然后调用add()方法添加一个fragment.

你可以使用同一个 FragmentTransaction进行多次fragment事务。当你完成这些变化操作的时候,必须调用commit()方法。

例如,如何添加一个fragment到之前的layout中

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);

        // Check that the activity is using the layout version with
        // the fragment_container FrameLayout
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create a new Fragment to be placed in the activity layout
            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();
        }
    }
}

因为fragment是在activity运行时被添加进来时(不是在XML布局中用定义的),activity 可以移除这个fragment或者用另外一个来替换它

Fragment替换

替换fragment的过程与添加过程类似,只需要将add()方法替换为 replace()方法。

记住当你执行fragment事务的时候,例如移除或者替换,你经常要适当地让用户可以向后导航与”撤销”这次改变。为了让用户向后导航fragment事务,你必须在FragmentTransaction提交前调用addToBackStack()方法。

Note:当你移除或者替换一个fragment并把它放入返回栈中时,被移除的fragment的生命周期是stopped(不是destoryed).当用户返回重新恢复这个fragment,它的生命周期是restarts。如果你没把fragment放入返回栈中,那么当他被移除或者替换时,它的生命周期是destoryed。

下面是一个fragment替换的例子

// Create fragment and give it an argument specifying the article it should show
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();

addToBackStack()方法提供了一个可选的String参数为事务指定了一个唯一的名字。这个名字不是必须的,除非你打算用FragmentManager.BackStackEntry APIs来进行一些高级的fragments操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值