編輯:關於 Android 程式設計
時間悄悄流逝,轉眼實習已滿三個月。從最初的電商項目到現在的車段子項目,小編走過一個又一個項目,也一次次戰勝了自己。每遇到一個新技術點,就忍不住搗鼓一番——搗鼓這回事,還真需要點勇氣,搗鼓搗鼓,功能出來了,特別有成就感。能力往往是做了才會擁有,而不是有了能力才去做。
很多 App 都有「發現」功能:點擊發現按鈕,自動加載最熱門的信息。最近項目裡正好有這個需求,折騰幾天終於新鮮出爐。今天這篇就來簡單介紹如何點擊發現按鈕、自動加載相關信息,希望能幫到需要的小伙伴。
首先編寫實體層,這裡用到兩個實體。第一個 Content:
package com.jczb.car.bean; import java.util.Date; import java.util.List; import android.R.integer; import android.R.integer; /** * 說明:段子內容實體 * 作者:丁國華 * 時間:2015年9月10日 10:41:43 */ @SuppressWarnings(serial) public class Content extends Entity { /** 內容表標識 */ private int uid; /** 顯示標題 */ private String title; /** 視頻或者圖片路徑 */ private String path; /**詳情*/ private String details; /**頻道類型*/ private String channelType; /**頻道名稱*/ private String channelName; /**作者*/ private String author; /**是否推薦*/ private String isRecommend; /**閱讀或者播放數量*/ private int browseNumber; /**評論數*/ private int commentbrowseNumber; /**贊數*/ private int praiseNumber; /**踩數*/ private int treadNumber; /**發布時間*/ private String issueTime; /**是否收藏*/ private int isCollect; /**關聯視頻*/ private List Content; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getDetails() { return details; } public void setDetails(String details) { this.details = details; } public String getChannelType() { return channelType; } public void setChannelType(String channelType) { this.channelType = channelType; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getIsRecommend() { return isRecommend; } public void setIsRecommend(String isRecommend) { this.isRecommend = isRecommend; } public int getBrowseNumber() { return browseNumber; } public void setBrowseNumber(int browseNumber) { this.browseNumber = browseNumber; } public int getCommentbrowseNumber() { return commentbrowseNumber; } public void setCommentbrowseNumber(int commentbrowseNumber) { this.commentbrowseNumber = commentbrowseNumber; } public int getPraiseNumber() { return praiseNumber; } public void setPraiseNumber(int praiseNumber) { this.praiseNumber = praiseNumber; } public int getTreadNumber() { return treadNumber; } public void setTreadNumber(int treadNumber) { this.treadNumber = treadNumber; } public String getIssueTime() { return issueTime; } public void setIssueTime(String issueTime) { this.issueTime = issueTime; } public String getChannelName() { return channelName; } public void setChannelName(String channelName) { this.channelName = channelName; } public int getIsCollect() { return isCollect; } public void setIsCollect(int isCollect) { this.isCollect = isCollect; } public List getContent() { return Content; } public void setContent(List content) { Content = content; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } }
第二個實體 ContentVo,用於承接 JSON 解析結果:
package com.jczb.car.bean; import java.util.List; /** *說明: 內容集合,用於Json串的解析 *作者:丁國華 *時間:2015年9月10日 10:38:38 */ @SuppressWarnings(serial) public class ContentVo extends Entity { /** 服務器返回結果:true或false */ private String result; /** 服務器返回的Json串解析為List結合 */ private List Content; public String getResult() { return result; } public void setResult(String result) { this.result = result; } public List getContent() { return Content; } public void setContent(List content) { Content = content; } }
接著編寫需要調用的接口方法,同樣處理了有網時取伺服器、無網時讀快取:
/** * 說明:發現 * @user 丁國華 * @date 2015-8-31 下午2:10:59 */ public ContentVo discover(boolean isFirst, int contentVoId) throws AppException { ContentVo contentVo = null; // 構建緩存文件的key String key = contentVo_ + contentVoId; // 1.如果聯網則首先從服務器獲取數據 if (isNetworkConnected() &&(isReadDataCache(key) || isFirst)) { try { // 從服務器獲取Content的集合 contentVo = ApiClient.discover(this); // 如果能夠獲取到服務器中的數據則保存到本地緩存,只有保證數據不為空,且獲取到的結果為true的時候才緩存到本地 if (contentVo != null && contentVo.getResult().equals(false)) { contentVo.setCacheKey(key); saveObject(contentVo, key); } } catch (AppException e) { // 如果出現訪問中途斷網,則獲取本地緩存中的數據 contentVo = (ContentVo) readObject(key); // 如果本地緩存中數據為空,則拋出異常 if (contentVo == null) { throw e; } } } // 2.如果未聯網則從緩存中獲取數據 else { contentVo = (ContentVo) readObject(key); if (contentVo == null) { contentVo = new ContentVo(); } return contentVo; } return contentVo; }
第三步編寫 XML 佈局,包含頂部標題、搜索框與熱門頻道列表:
第四步編寫 Activity 邏輯,包含初始化控件、子線程拉取數據、Handler 更新列表與點擊跳轉:
package com.jczb.car.ui; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; import com.jczb.car.AppContext; import com.jczb.car.AppException; import com.jczb.car.R; import com.jczb.car.bean.Content; import com.jczb.car.bean.ContentVo; /** * 說明:發現功能頁面 * 作者:丁國華 * 時間: 2015-9-2下午2:02:58 */ public class DiscoveryActivity extends Activity implements OnClickListener, OnItemClickListener { //RelativeLayout rlFoundSearch; //ImageView ivTuBiaoSearch; // 聲明用到的幾個控件 private TextView etSearch; private ImageView ivSearch; private ListView lvHotChannel; private RelativeLayout rlFoundSearchEmpty; //private ImageView ivTuBiao; /**解析發現接口用的實體類*/ private ContentVo contentVo = null; /**用來填充ListView的List*/ private List
至此,「發現」功能的整條鏈路就走通了。最後來看看運行效果:

小結:發現功能的實作就簡單介紹到這裡。從實體建模、接口請求、佈局搭建到 Activity 中子線程拉數據、Handler 刷新列表、SimpleAdapter 綁定與點擊跳轉,串起來就是一個完整的發現頁流程。下一篇文章我們將分享如何透過輸入關鍵字搜索出相應內容,敬請期待。
Android 中 android:exported 屬性詳解
某天用 360 掃描應用漏洞時,掃描結果跳出了一個 android:exported 屬性。老實說,在此之前我根本不知道有這個屬性,更不清楚它的用途,詳見下圖: 於是
Android 對話框全攻略:普通、單選、多選、帶圖標、自定義 Adapter 與自定義 View
對話框的本質就是一個 AlertDialog,但看似簡單的 AlertDialog,其實能玩出不少花樣。下面我們就一起把 AlertDialog 的各種用法系統性地梳理一
CircleImageView 實作:把任意圖片裁成圓形頭像
QQ、微信、微博等應用都有設置頭像的功能,頭像有方形也有圓形。本文給出一個以圖片中心為基準裁剪成圓的工具控件,可直接在佈局中使用。 處理前的原始圖: 處理後作為頭
Android 自定義 Toast:不用 XML,用程式碼組出自己的提示樣式
系統預設的 Toast 只有一行文字,樣式實在朴素。如果想要加上圖示、改字級、換背景色,最靈活的方式是自己組一個 View 塞進去。 核心觀念 Toast.make