Fragment简易新闻

这篇博客介绍了一个使用Fragment构建的简易新闻应用。在布局方面,针对不同屏幕尺寸,应用分别使用了activity_main.xml和activity_main.xml(sw600dp)。新闻标题展示在NewsTitleFragment中,其布局文件为news_title_frag.xml,而新闻内容部分在NewsContentFragment中,布局文件为news_content_frag.xml。NewsTitleFragment中的新闻标题通过recyclerview的item新闻_title_item.xml显示。当用户点击标题时,小于600dp的设备会跳转到NewsContentActivity,而大于600dp的设备则直接在NewsContentFragment中刷新内容。
摘要由CSDN通过智能技术生成

这里写图片描述

下面是xml布局文件
activity_main.xml: 屏幕宽度小于600dp加载这个布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <fragment
      android:id="@+id/news_title"
      android:name="com.ukulele.fragmentbestpractice.NewsTitleFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:layout_editor_absoluteY="8dp"
      tools:layout_editor_absoluteX="8dp" />

</LinearLayout>

activity_main.xml(sw600dp): 屏幕宽度大于600dp加载这个布局文件

<?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.ukulele.fragmentbestpractice.NewsTitleFragment"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="1"/>

    <fragment
        android:id="@+id/news_content_fragment"
        android:name="com.ukulele.fragmentbestpractice.NewsContentFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"/>

</LinearLayout>

news_title_frag.xml:新闻标题碎片需要加载的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值