开源android豆瓣电影阅读器
學習andorid開發有一段時間了,做了個小應用--豆瓣電影閱讀器,作為這一階段學習的一個總結。當前實現了基本的閱讀器功能,更多豐富的特性,后續會進一步添加。
代碼托管在github上,https://github.com/zhaoyu87/DouBan,需要的同學可以下載
應用主要分為兩塊:
1. 數據內容,http訪問豆瓣api獲取JSON格式的電影信息
2. 顯示界面,使用了一個ListView來顯示電影列表,和一個WebView來顯示電影詳情,如下所示:
?
1. 數據內容,http訪問豆瓣api獲取JSON格式的電影信息
?app中使用的豆瓣api說明文檔見http://developers.douban.com/wiki/?title=api_v2。
應用中使用了top250電影的api來獲取電影列表信息api.douban.com/v2/movie/top250?count=20&start=0,后面可以跟參數count和start以及apikey
需要被解析的json數據分別為top250、simple subject、rating和image,代碼中分別體現為下面所示幾個文件
2. 顯示界面,使用了一個ListView來顯示電影列表,和一個WebView來顯示電影詳情
顯示界面只使用了一個Top250ListActivity,電影列表和電影詳情分別使用Top250ListFragment和MovieDetailFragment兩個fragment實現,這里選擇使用fragment而不是activity主要是出于對靈活性的考慮,便于后面更改顯示效果,比如改為view pager顯示,或者需要支持pad設備
?
應用對象圖解如上所示
整個應用的結構并不復雜,不再做詳細解釋。直說重要的幾點:
1.Top250ListAdapter中通過AsyncTask來創建httpClient通過httpGet訪問服務端獲取電影列表,需要注意的是這里設置了httpGet的user-agent為瀏覽器,否則無法正常獲取信息
HttpClient httpClient = new DefaultHttpClient();HttpGet httpGet = new HttpGet(Urls.BASE + Top250.APPEND_URL + "?count=" + count + "&start=" + start + "&apikey=0537d7e598d847b20fa787b0ca88d716");httpGet.setHeader("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");2.Top250ListFragment到MovieDetailFragment的跳轉過程實現:Top250ListFragment中定義了Callback接口,由Top250ListActivity實現該接口來進行fragment切換,不直接在Top250ListFragment中實現fragment切換,保持兩個fragment的獨立
public interface Callbacks {void onTop250ItemClicked(SimpleSubject subject);}3.MovieDetailFragment通過如下newInstance方法創建,有利于fragment的封裝性
public static MovieDetailFragment newInstance(String url) {Bundle bundle = new Bundle();bundle.putString(MOVIE_DETAIL_URL, url);MovieDetailFragment fragment = new MovieDetailFragment();fragment.setArguments(bundle);return fragment;}4.下載圖片使用了開源框架Android-Universal-Image-Loader(https://github.com/nostra13/Android-Universal-Image-Loader),支持多線程異步加載網絡、本地圖片,支持圖片內存緩存、文件系統緩存。推薦一個博客http://blog.csdn.net/xiaanming/article/details/26810303
注意,如果需要SD卡緩存的話,需要增加權限
<manifest><uses-permission android:name="android.permission.INTERNET" /><!-- Include next permission if you want to allow UIL to cache images on SD card --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />... </manifest>5.list view item體驗了一下Android 5.0?material design中的CardView,CardView繼承自FrameLayout類,可以在一個卡片布局中一致性的顯示內容,卡片可以包含圓角和陰影。
list_item_top250.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/card_view"app:cardCornerRadius="8dp"android:layout_width="match_parent"android:layout_height="120dp">......</android.support.v7.widget.CardView>導入支持庫android.support.v7.widget.CardView,需要在build.gradle dependencies中添加,如下
dependencies {compile fileTree(include: ['*.jar'], dir: 'libs')compile 'com.android.support:appcompat-v7:21.0.3'compile 'com.android.support:cardview-v7:21.+'compile files('libs/universal-image-loader-1.9.3.jar') }上面就是這個小應用的簡單介紹,和大家分享一下,共同學習。如果發現任何bug或者有建議歡迎留言
?
轉載于:https://www.cnblogs.com/letusrock/p/4311976.html
總結
以上是生活随笔為你收集整理的开源android豆瓣电影阅读器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: liunx检查与安装软件包
- 下一篇: WebService的事务处理 (转)