先看这样一段代码
android:layout_height="match_parent"
android:background="#555555">
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent"
android:background="#00550033" />
android:layout_width="0px"
android:layout_height="match_parent" />
程序运行图片如下:
这里有一个名为
这里下一个标记是 FrameLayout, 而不是 再写一个
这是因为:
详情碎片是活动的, 详情碎片必须位于视图容器内, 因为碎片不是视图容器,这就是为活动的main.xml 布局文件选择FrameLayout的原因。 FrameLayout中将包含详情碎片, 如果在活动的布局文件中指定了另一个
首先应知道,将在详情文本上执行一些过渡,将一个碎片切换到另一个。使用FrameLayout 作为视图容器来持有当前的文本碎片, 对于标题碎片,只需一个碎片,没有切换和过渡, 对于显示详情的区域, 将有多个碎片!
在这个应用中,要确定使用碎片,只需判断设备的方向, 如果处于横向模式, 则拥有多个窗格,那么将使用碎片来显示文本,将碎片定义为DetailsFragment, 使用一种工厂方法来创建带有索引的Fragment,如果处于纵向模式,则没有多个窗格
主要代码
public boolean isMultiPane() { // 根据是否横屏,判断是否多个碎片
return getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE;
}
/**
* 用于显示选中条目的详情的方法, either by
* displaying a fragment in-place in the current UI, or starting a
* whole new activity in which it is displayed.
*/
public void showDetails(int index) {
Log.v(TAG, "in MainActivity showDetails(" + index + ")");
if (isMultiPane()) {
// Check what fragment is shown, replace if needed.
DetailsFragment details = (DetailsFragment) getSupportFragmentManager().findFragmentById(R.id.details);
if (details == null || details.getShownIndex() != index) {
// Make new fragment to show this selection.
details = DetailsFragment.newInstance(index);
// Execute a transaction, replacing any existing
// fragment with this one inside the frame.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
//ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.replace(R.id.details, details);
//ft.addToBackStack(TAG);
ft.commit();
//getSupportFragmentManager().executePendingTransactions();
}
} else {
// 否则加载一个新的Activity用于显示被选中文本的对话框碎片
Intent intent = new Intent();
intent.setClass(this, DetailsActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
具体代码请参见: ch29_ShakespeareSDK 工程