第 4 章 探究碎片最佳实践之简易版的新闻应用
2017-03-08 20:30
232 查看
实践过程中犯了一个错误布局标签
1准备实体类News 略过
2准备新闻内容布局news_content_frag.xml,此布局效果在大小不同的屏幕显示略有不同,主要是有个一
3.新建NewsContentFragment类,用于显示新闻主题内容的Fragment这里用静态加载的方法
4.新建一个用于存放Fragment的Activity,NewsContentActivity同时为其创建布局news_content.xml在里面添加NewsContentFragment的布局
5.创建用于显示新闻列表的布局news_title_frag.xml
6.创建新闻列表子项的布局
7.创建显示新闻列表的Fragment,NewsTitleFragment,直接这个类里创建继承自RecyclerView.Adapter的适配器
8.把NewsTitleFragment的布局添加到activity_main.xml
9.新建一个文件夹layout-sw600dp,并在文件夹下新建一个activity_main.xml,代码如下
此布局与同名布局的差别在于,可以同时显示两个Fragment,新闻标题列表,新闻内容。此布局中的ID:news_content_layout的存在与否,可作为是加载一个Fragment还是两个的判断依据。
在NewsTitleFragment的onActivityCreated方法中判断
<View>写成了小写的
<view>
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.fragmentpractice/com.example.android.fragmentpractice.NewsContentActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class null
1准备实体类News 略过
2准备新闻内容布局news_content_frag.xml,此布局效果在大小不同的屏幕显示略有不同,主要是有个一
<View>和连个
<View>作分割线的区别(之前没看懂,运行之后才知道)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:layout_width="1dp" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:background="#000" android:id="@+id/view" /> <LinearLayout android:id="@+id/visibility_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:visibility="invisible" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/view" android:layout_toEndOf="@+id/view"> <TextView android:id="@+id/news_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:textSize="20sp" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000" /> <TextView android:id="@+id/news_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="15dp" android:textSize="18sp" /> </LinearLayout> </RelativeLayout>
3.新建NewsContentFragment类,用于显示新闻主题内容的Fragment这里用静态加载的方法
4.新建一个用于存放Fragment的Activity,NewsContentActivity同时为其创建布局news_content.xml在里面添加NewsContentFragment的布局
5.创建用于显示新闻列表的布局news_title_frag.xml
6.创建新闻列表子项的布局
7.创建显示新闻列表的Fragment,NewsTitleFragment,直接这个类里创建继承自RecyclerView.Adapter的适配器
8.把NewsTitleFragment的布局添加到activity_main.xml
9.新建一个文件夹layout-sw600dp,并在文件夹下新建一个activity_main.xml,代码如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <fragment android:id="@+id/news_title_fragment" android:name="com.example.android.fragmentpractice.NewsTitleFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <FrameLayout android:id="@+id/news_content_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" > <fragment android:id="@+id/news_content_fragment" android:name="com.example.android.fragmentpractice.NewsContentFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> </LinearLayout>
此布局与同名布局的差别在于,可以同时显示两个Fragment,新闻标题列表,新闻内容。此布局中的ID:news_content_layout的存在与否,可作为是加载一个Fragment还是两个的判断依据。
在NewsTitleFragment的onActivityCreated方法中判断
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (getActivity().findViewById(R.id.news_content_layout) != null) { isTwoPane = true; // 可以找到news_content_layout布局时,为双页模式 } else { isTwoPane = false; // 找不到news_content_layout布局时,为单页模式 } }
相关文章推荐
- 碎片最佳实践-简易版的新闻应用
- 碎片的最佳实践——简易版新闻应用
- 碎片的最佳实践——一个简易版的新闻应用
- 碎片的最佳实践——一个简易版的新闻应用
- Fragment最佳实践---------一个简易版的新闻应用
- 【安卓学习】4.碎片(Fragment)实践---一个简单的新闻应用。
- 博客园添新书《Windows Powershell 2.0应用编程最佳实践》出版
- MapGuide应用最佳实践----实现MapGuide Server的负载均衡
- WebSphere Application Server 环境配置与应用部署最佳实践
- J2ME编程最佳实践之灵活的RMS应用
- 品牌推广应用设计的最佳实践
- [新闻资讯] Flex开发指南---创建用户体验的工作流程和最佳实践
- MapGuide应用最佳实践—MapGuide Server和MapGuide WebExtension分开部署
- [导入]ExtJs 2.0 公司工程化应用的最佳实践(二)
- MapGuide应用最佳实践--采用托管(Managed)资源还是非托管(Unmanaged)资源
- WF 的实际应用:Windows Workflow Foundation 应用程序最佳实践
- MapGuide应用最佳实践----资源库Repository的维护
- 若干条J2EE应用中运用“配置”的最佳实践 - 企业应用 - Java - JavaEye论坛
- Spring构建应用系统的最佳架构与模式实践(1)
- 第四讲 构建安全的Microsoft ASP.NET 应用的最佳实践和技术