第 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
原创粉丝点击