您的位置:首页 > 大数据 > 人工智能

学习Fragment笔记-选择Master/Dtail Flow创建项目开始(1)

2014-04-17 17:29 561 查看
项目生成目录

创建了两个activity,一个显示分类,一个显示类目详情

ItemListActivity代码

继承FragmentActivity类,实现回调接口

public class ItemListActivity extends FragmentActivity implements
ItemListFragment.Callbacks {

private boolean mTwoPane;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
//字面理解:当前layout中找到item_detail_container类目详情容器,则在当前activity中启用双面板
if (findViewById(R.id.item_detail_container) != null) {
mTwoPane = true;
((ItemListFragment) getSupportFragmentManager().findFragmentById(
R.id.item_list)).setActivateOnItemClick(true);
}

}
//在分类列表中选中某个分类的时候,如果mTwoPane=true则使用fragment,否则用activity
@Override
public void onItemSelected(String id) {
if (mTwoPane) {
//fragment跳转
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);//封装传递信息
ItemDetailFragment fragment = new ItemDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.item_detail_container, fragment).commit();//每次点击都新建一个fragment显示类目详情,替换容器里的旧fragment

} else {
//传统activity跳转
Intent detailIntent = new Intent(this, ItemDetailActivity.class);
detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);
}
}
}


以上模式里layout使用了
setContentView(R.layout.activity_item_list);


activity_item_list

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_list"
android:name="com.example.testfragment.ItemListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
tools:context=".ItemListActivity"
tools:layout="@android:layout/list_content" />


里面并无item_detail_container这个容器,所以以上模式按传统activity跳转模式进行

项目另外提供了一个layout,activity_item_twopane.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".ItemListActivity" >

<!--
This layout is a two-pane layout for the Items
master/detail flow. See res/values-large/refs.xml and
res/values-sw600dp/refs.xml for an example of layout aliases
that replace the single-pane version of the layout with
this two-pane version.

For more on layout aliases, see: http://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters -->

<fragment
android:id="@+id/item_list"
android:name="com.example.testfragment.ItemListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@android:layout/list_content" />

<FrameLayout
android:id="@+id/item_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />

</LinearLayout>
提供了item_detail_container这个容器,将setContentView(R.layout.activity_item_list);修改成setContentView(R.activity_item_twopane);可以切换成一个activity双面板的模式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: