第 4 章 探究碎片最佳实践之简易版的新闻应用

本文介绍了一位开发者在构建Android新闻应用时的实践经验,详细讲述了从创建实体类、设计不同屏幕尺寸的布局,到实现新闻内容显示Fragment、新闻列表Fragment的全过程。在实践中,作者注意到了布局标签大小写的问题,并分享了如何根据屏幕尺寸动态加载Fragment的技巧。
摘要由CSDN通过智能技术生成

实践过程中犯了一个错误布局标签<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布局时,为单页模式
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值