学习Fragment笔记-选择Master/Dtail Flow创建项目开始(1)
2014-04-17 17:29
561 查看
项目生成目录
创建了两个activity,一个显示分类,一个显示类目详情
ItemListActivity代码
继承FragmentActivity类,实现回调接口
以上模式里layout使用了
activity_item_list
里面并无item_detail_container这个容器,所以以上模式按传统activity跳转模式进行
项目另外提供了一个layout,activity_item_twopane.xml
创建了两个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双面板的模式
相关文章推荐
- Hibernate学习笔记一:项目创建与基本配置文件
- maven学习笔记--maven项目创建
- 【极客学院】-iOS学习笔记-2-Xcode(安装,创建项目,工作空间)
- Pyramid 学习笔记:创建Pyramid项目(中)
- Symfony1.4.11学习笔记(二):创建项目
- Django学习笔记1-创建Django项目
- Maven学习笔记005——导入和创建Maven项目
- SSM框架学习之高并发秒杀业务--笔记1-- 项目的创建和依赖
- typeorm学习笔记之创建项目
- Quartz.NET 2.0 学习笔记(5) :实例创建Windows服务实现任务调度 Quartz.NET 项目地址 http://quartznet.sourceforge.net/ Quar
- Quick cocos2dx-Lua(V3.3R1)学习笔记(1)----创建新项目
- 五毛的cocos2d-x学习笔记01-创建项目
- 不错的maven学习笔记,收藏——Maven学习 (三) 使用m2eclipse创建web项目
- Maven学习笔记——(1)Maven在Eclipse中创建JavaWeb项目
- 项目已经开始学习HL7一段时间了,以后还是对学习做一些笔记
- SpringBoot学习笔记-Eclipse创建SpringBoot项目
- react---学习笔记:使用create-react-app快速构建开发环境并创建项目
- maven 学习笔记(六)-创建简单的多模块maven项目)
- 开始cocos2dx学习小记:MacOS下使用cocos2dx创建项目遇到permission denied
- java学习笔记(一):开始第一个java项目