功能效果展示
界面设计分析
上述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