第 4 章 探究碎片最佳实践之简易版的新闻应用
来源:互联网 发布:js 取出json 字段的值 编辑:程序博客网 时间:2024/05/16 00:59
实践过程中犯了一个错误布局标签<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布局时,为单页模式 } }
0 0
- 第 4 章 探究碎片最佳实践之简易版的新闻应用
- 碎片的最佳实践——简易版新闻应用
- 碎片的最佳实践——一个简易版的新闻应用
- 碎片的最佳实践——一个简易版的新闻应用
- Fragment最佳实践---------一个简易版的新闻应用
- 第 4 章 探究碎片
- 碎片实例:简易版本的新闻应用(碎片+列表显示的问题)
- 学习了《第一行代码》4.5章—一个简易版的新闻应用
- 《碎片的最佳实践》读书笔记
- 第4章 手机平板要兼顾,探究碎片
- 【安卓学习】4.碎片(Fragment)实践---一个简单的新闻应用。
- 阅读郭林《第一行代码》的笔记——第4章 手机平板要兼顾,探究碎片
- Fragment碎片应用--新闻APP
- 第一行代码笔记,第四章-------探究碎片
- J2ME编程最佳实践之灵活的RMS应用
- Android最佳实践之高效的应用导航
- 第3.1.1章 WEB系统最佳实践 js控件之slimScroll的使用
- 第3.1.2章 WEB系统最佳实践 js控件之requirejs的使用
- 九度 oj 题目1552:座位问题
- jquery选择器,toggle示例,js获取id与jquery获取id比较
- CodeForces
- Java中ServerSocket与Socket的区别
- Hibernate(二)——POJO对象的操作
- 第 4 章 探究碎片最佳实践之简易版的新闻应用
- hdu 2036_改革春风吹满地_计算几何
- 开发笔记005
- Android 左划返回控件SildingFinishLayout
- Hibernate(三)——框架中的关系映射
- [RFID]射频识别技术基础
- 常用SQL语句汇总——基础
- STM32中PWM的参数配置
- L1-020. 帅到没朋友