自制Android RSS閱讀器

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

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

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

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

視圖的問題如圖。

這裏寫圖片描述

adapteronCreateViewHolder()方法中,就是按照

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rss_list_item, parent, false);

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

嘗試了很久,結果癥結出在ConstraintLayoutRecyclerView一起使用上。

上圖中是一個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/selectzhihu.com/rsshttp://www.read.org.cn/feed以及feed.androidauthority.com四個RSS,發現有三個問題:

  • <link>標籤的內容,可能包含在<link>的屬性中,如:<link href="http://...">
  • <description>標籤有時候又叫<subtitle>
  • <item>標籤有時候又叫<entry>

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

簡單的應用,使用sqlite作爲本地存儲,如果重複添加已有的RSS,就相當於刷新RSS的內容。

目前還只有簡單的添加RSS,瀏覽其內容的功能。

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

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

上個截圖:

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章