标签:
当你设计你的应用来支持多个屏幕尺寸。你能够基于可用的屏幕空间通过在不同的布局上重用fragment来优化用户体验。
比如,在一个手机上。使用单面板(一次仅仅显示一个fragment)的用户体验更加合适。For example, on a handset device it might be appropriate to display just one fragment at a time for a single-pane user interface. 相反,你可能希望在一个可以展示很多其它信息的平板上设置并排摆放的fragments。
图 1. 同一个activity在不同的屏幕尺寸上展示的不同的fragment配置。在大尺寸屏幕上。两个fragment并排展示,而在小屏幕的设备上,一次仅仅能显示一个fragment。须要用户操作来切换它们。
FragmentManager 类提供了可以在执行时add,
remove, 以及 replace fragment的方法,这样就行创建一个动态的用户体验。
不同于直接在activity的布局文件里定义fragments(像在上一课中展示的那样,使用,<fragment>元素)。你能够在activity执行时加入一个fragment。假设你打算在activity的生命周期期间更改fragment,那么这样做非常有必要。
要运行一个加入或者移除fragment操作,你必须使用FragmentManager 创建一个FragmentTransaction,它提供了add,
remove, replace, 以及运行其它fragment事务的APIs。
假设你的activity同意fragments被移除或者替换。那么你须要在activity的onCreate()方法中加入初始化这些fragments的方法。
在使用fragment,特别是动态加入的fragment时,有一个重要的规则是fragment必须有一个容器视图,用作fragment的容器。
上一课中展示了一个一次显示一个fragment的布局,以下的布局是上一课中那个布局的可选项。
为了使用fragment之间的切换,这个activity布局中包括了一个空的FrameLayout 作为fragment的容器。
能够注意到这个文件名称和上一课中的文件名称同样,可是他的布局文件夹没有包括lager限定符,因此这个布局是给小的屏幕设备使用的,由于这种屏幕不适用同一时候展示两个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包的API,调用getSupportFragmentManager() 来获取一个 FragmentManager 。然后调用beginTransaction() 来创建一个FragmentTransaction 。然后调用add() 方法来加入一个fragment。
你能够使用FragmentTransaction为activity运行多个fragment事务。
最后(当你准备好改变时)。你必须调用commit()方法。
比如。以下是怎样为之前的布局加入一个fragment:
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已经在执行时被动态增加了这个FrameLayout 容器中——而不是在activity的布局文件的<fragment>元素中被定义。这个activity可以移除这个fragment或者替换这个fragment。
替换fragment的流程和加入fragment的流程十分相似,不同的是须要调用replace() 方法,而不是add()方法。
要记住当你运行一个fragment事务时,比如替换或者移除一个,这一般会同意用户返回或者撤销这些改变。要同意用户退回一个fragment事务,你必须在提交fragment事务之前调用addToBackStack() 方法。
提示: 当你移除或者替换一个fragment而且加入这个事务到返回栈时,被移除的fragment将会被停止(而不是被销毁)。假设用户再返回到这个fragment,它会重新启动(而不是又一次创建)。假设你没有将它加入到返回栈,那么这个fragmen在被移除或者替换时就直接被销毁了。
替换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() 方法须要传入一个字符串类型的參数来指定这次事务的唯一名称。假设你打算不使用高级的fragment操作APIsFragmentManager.BackStackEntry 。这个名字是不是角色。
Android学习路径(22)应用Fragment建立动态UI——构建一个灵活UI
标签:
原文地址:http://www.cnblogs.com/gcczhongduan/p/4722115.html