使用RecyclerView实现新闻列表并实现Activity跳转

Android应用:新闻列表与详情展示与解析实践
本文详细介绍了如何在Android应用中使用RecyclerView实现新闻列表,以及如何解析聚合新闻API并用HtmlTextView展示详情,包括数据解析、Adapter设计和界面交互解决方案。

功能效果展示

界面设计分析

上述APP界面分为两个部分,第一部分是新闻列表页,第二部分是新闻详情页,新闻列表页中可以使用recyclerView实现,详情页这里我使用了HtmlTextView接受url传回的html数据,进行显示。具体如下:

新闻详情页

此次在微信tab页添加列表,因此可以直接在fragment_news.xml文件中设置

  • 添加RecyclerView控件
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".news">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
  • 创建recyclerView的布局

对每个item来说,都包含图片,新闻标题,新闻来源,以及发布时间,因此可以做以下布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/liner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/news_image"
        android:layout_width="140dp"
        android:layout_height="100dp"
        android:layout_weight="0"
        tools:srcCompat="@tools:sample/avatars" />
    <LinearLayout
        android:id="@+id/liner2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0"
        android:orientation="vertical">
        <TextView
            android:id="@+id/news_title"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:text="标题"
            android:textSize="20sp" />
        <LinearLayout
            android:id="@+id/liner3"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/news_author"
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:textSize="16sp"
                android:layout_weight="1"
                android:text="作者" />
            <TextView
                android:id="@+id/news_date"
                android:layout_width="wrap_content"
                android:layout_height="20dp"
                android:textSize="16sp"
                android:layout_weight="1"
                android:text="发布时间" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
  • 效果展示
    在这里插入图片描述

新闻详情页

新闻详情页比较简单,因为我的详情页面的数据是由okhttp根据新闻url请求后返回的HTML数据构成,因此选择htmlTextView作为控件解析页面。

  • 布局文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值