设为首页 加入收藏

TOP

Android UI基础教程:使用Fragment构建灵活的桌面
2014-11-23 20:28:54 来源: 作者: 【 】 浏览:25
Tags:Android 基础 教程 使用 Fragment 构建 灵活 桌面

当设计应用程序,你可以在不同的布局结构中重复使用Fragment,以支持众多的屏幕尺寸,,在可用的屏幕空间上优化用户体验。例如在手持设备(如Nexus 4)上,一个屏显示一个Fragment,在更大屏(如Nexus 7)上可以使用多个Fragment显示信息。如下图:



图一


图一中,在大屏中两个Fragment显示在一个屏中,但是手持设备中,需要两屏显示完,一屏只能显示一个,他们之间需要相互引导。


FragmentManager类提供了一些方法,使您可以在Activity运行时添加,删除和替换Fragment,以创造一个灵活、动态的体验。


添加Fragment到一个运行的Activity


1、activity的onCreate()方法中添加初始化的fragment


2、fragment放置位置的布局中必须有一个视图容器


程序例子中, res/layout/news_articles.xml文件提供了视图容器。


android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />


Activity中使用getSupportFragmentManager()获取FragmentManager,之后调用beginTransaction去创建一个FragmentTransaction对象, 再调用add()方法即可添加一个fragment。 在activity中可以使用同一个FragmentTransaction对象去执行多个fragment事务,当做这样操作时,必须调用commint()方法。 下面的代码演示怎样添加一个fragment到res/layout/news_articles.xml的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 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();
}
}
}


这里的fragment是在运行时添加到FrameLayout,而不是直接使用标签定义在activity的布局中,activity可以移除它或者使用另外一个不同的fragment替换它。


替换Fragment


替换一个fragment的过程和添加Fragment的过程差不多,但是需要的是replace()方法,而不是add()方法。 需要注意的是,当执行fragment事务时,比如替换或者删除一个fragment,如果想能回退到当前,你必须在你提交fragment事务之前调用 addToBackStack()方法。


当移除或替换fragment时将事务添加到堆栈中,被移除的Fragmeng没有消亡,如果用户返回,Fragment会重新启动。如果没有放入到堆栈中,当Fragment被替换或移除,Fragment会消亡。


下面是替换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()方法有一个可选的字符串参数,用来指定事务的唯一名称,这个是非必须的。


参考:http://developer.android.com/training/basics/fragments/fragment-ui.html



图二 Google IO APP


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android应用如何支持屏幕多尺寸多.. 下一篇Android UI基础教程:Android Fra..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: