天天看點

自制Android RSS閱讀器

這個問題困擾我挺久了,android端找不到一款好的RSS閱讀器。我就想簡簡單單打開自己訂閱的RSS每天看一看,沒有注冊,沒有廣告,就簡簡單單的。終于抽了兩天時間出來寫了個BETA。

所有代碼都放在我的Github上了。

記錄一下開發過程中遇到的一些問題。

1. RecyclerView處在ConstraintLayout中出現的視圖問題

視圖的問題如圖。

自制Android RSS閱讀器

adapter

onCreateViewHolder()

方法中,就是按照

這樣來寫的。子視圖的高度也都是确定的。如果這麼簡單就好了。問題照舊。

嘗試了很久,結果症結出在

ConstraintLayout

RecyclerView

一起使用上。

上圖中是一個fragment,有問題的fragment的根布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/fragment_bg"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rssRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>
           

因為其他很多地方我都排查過了,是以對這個布局心生懷疑。

我就把

ConstraintLayout

改成了

FrameLayout

,修改後的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/fragment_bg"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rssRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>
           

問題解決。

2. 對RSS的XML文檔的解析

讀取RSS中的内容其實就是解析一下XML文檔。

RSS的格式有個大概的标準,大緻就是

<channel>

或者

<rss>

開頭,然後是最外層的

<title>, <link>, <description>

的内容;緊接着就是

<item>

,代表每個子條目;每個

<item>

标簽中也有相應的

<title>, <link>, <description>

節點。

比較了

feed.yeeyan.org/select

zhihu.com/rss

http://www.read.org.cn/feed

以及

feed.androidauthority.com

四個RSS,發現有三個問題:

  • <link>

    标簽的内容,可能包含在

    <link>

    的屬性中,如:

    <link href="http://..." target="_blank" rel="external nofollow" >

  • <description>

    标簽有時候又叫

    <subtitle>

  • <item>

    标簽有時候又叫

    <entry>

是以在解析的時候,将這三個問題考慮進去,大部分的RSS應該都能解析了。

簡單的應用,使用sqlite作為本地存儲,如果重複添加已有的RSS,就相當于重新整理RSS的内容。

目前還隻有簡單的添加RSS,浏覽其内容的功能。

陸續還會抽空把重新整理,進度條和 RSS補全庫等功能加上。

代碼希望對大家有用。有需要改進的地方,可以郵件我或者ISSUE。

上個截圖:

自制Android RSS閱讀器