編輯:關於 Android 程式設計
這一節要為部落格、新聞、評論三個頁面的 ListView 加上長按事件,讓使用者快速跳到對應頁面:
1. 部落格:查看內容、查看評論、查看作者。
2. 新聞:查看內容、查看評論。
3. 評論:引用、回覆(此功能尚未實作)。
核心做法是自己寫一個 ItemDialog,繼承 Dialog 並實作 OnItemClickListener,把選項字串陣列塞進 ListView;再依 TAG 區分呼叫來源,決定要跳哪一個 Activity。
類別裡對外暴露了 blogEntry、newsEntry、commentEntry 三個資料物件與一個 TAG 字串,呼叫端在 new 出物件後設定即可:
public class ItemDialog extends Dialog implements OnItemClickListener {
private Context context;
private String[] items;
private ListView listView;
public int selectIndex = 0;
public Blog blogEntry;
public News newsEntry;
public Comment commentEntry;
public String commentString;
public String TAG;
public ItemDialog(Context context, int theme) {
super(context, theme);
this.context = context;
}
public ItemDialog(Context context) {
super(context);
this.context = context;
}
public ItemDialog(Context context, String[] items) {
super(context);
this.context = context;
this.items = items;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.item_dialog);
listView = (ListView) findViewById(R.id.listViewBlogDialog);
listView.setAdapter(new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, items));
listView.setOnItemClickListener(this);
}
}
onItemClick 依被點選的文字決定後續動作,並用 TAG 判斷是部落格還是新聞:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectIndex = position;
Log.i("ItemDialog", "selectIndex --- " + selectIndex);
String selectItem = items[position];
if (selectItem.equals("查看內容")) {
this.hide();
if (TAG.equals("blog")) {
viewBlogContent();
} else if (TAG.equals("news")) {
viewNewsContent();
}
} else if (selectItem.equals("查看評論")) {
this.hide();
if (TAG.equals("blog")) {
viewComment(blogEntry.getBlogId(), "blog");
} else if (TAG.equals("news")) {
viewComment(newsEntry.getNewsId(), "news");
}
} else if (selectItem.equals("查看作者")) {
this.hide();
viewAuthor();
} else if (selectItem.equals("回覆")) {
this.hide();
addComment(commentString, "回覆");
} else if (selectItem.equals("引用")) {
this.hide();
addComment(commentString, "引用");
} else if (selectItem.equals("取消")) {
this.hide();
}
return;
}
把標題、作者、時間、id 等欄位用 Intent 的 extra 帶過去,頭像為空時給一張預設圖:
private void viewBlogContent() {
Intent intent = new Intent(context, BlogActivity.class);
if (blogEntry.getAuthorAvatar() != null) {
intent.putExtra("avatar", blogEntry.getAuthorAvatar().toString());
} else {
intent.putExtra("avatar", DEFAULT_AVATAR);
}
intent.putExtra("title", blogEntry.getBlogTitle());
intent.putExtra("author", blogEntry.getAuthorName());
intent.putExtra("published", AppUtils.parseDateToString(blogEntry.getPublishedDateDate()));
intent.putExtra("id", blogEntry.getBlogId());
intent.putExtra("link", blogEntry.getBlogTitle());
context.startActivity(intent);
}
private void viewNewsContent() {
Intent intent = new Intent(context, NewsActivity.class);
if (newsEntry.getTopicIcon() != null) {
intent.putExtra("topicIcon", newsEntry.getTopicIcon().toString());
} else {
intent.putExtra("topicIcon", DEFAULT_AVATAR);
}
intent.putExtra("title", newsEntry.getNewsTitle());
intent.putExtra("sourceName", newsEntry.getSourceName());
intent.putExtra("published", AppUtils.parseDateToString(newsEntry.getPublishedDate()));
intent.putExtra("id", newsEntry.getNewsId());
context.startActivity(intent);
}
這一段涉及網路請求,因此必須放在子執行緒中:先把作者名稱做 URL 編碼,替換到查詢字串的參數位置,抓回使用者清單後再跳轉:
private void viewAuthor() {
new Thread(new Runnable() {
@Override
public void run() {
String authorName = blogEntry.getAuthorName();
String path = Config.SEARCH_AUTHOR_BY_NAME;
Log.i("SearchActivity", "textAuthor:" + authorName);
try {
authorName = URLEncoder.encode(authorName, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
path = path.replace("{TERM}", authorName);
Log.i("SearchActivity", "path:" + path);
List<User> userList = AppUtils.getUserList(path);
User userEntry = userList.get(0);
Intent intent = new Intent(context, UserActivity.class);
intent.putExtra("blogapp", userEntry.getBlogapp());
intent.putExtra("link", userEntry.getUserLink().toString());
if (userEntry.getUserAvatar() != null) {
intent.putExtra("avatar", userEntry.getUserAvatar().toString());
} else {
intent.putExtra("avatar", DEFAULT_AVATAR);
}
intent.putExtra("postcount", userEntry.getPostCount());
intent.putExtra("updated", AppUtils.parseDateToString(userEntry.getUpdatedDate()));
intent.putExtra("title", userEntry.getTitle());
context.startActivity(intent);
}
}).start();
}
查看評論只需要把 id 與型別帶給 CommentActivity:
private void viewComment(int id, String type) {
Intent intent = new Intent(context, CommentActivity.class);
intent.putExtra("id", id);
intent.putExtra("type", type);
context.startActivity(intent);
}
private void addComment(String string, String type) {
//引用與回覆功能暫未實作
}
在 ListView 的 OnItemLongClickListener 中建立 ItemDialog,設定 TAG 與對應的資料物件,再傳入選項陣列並 show() 出來即可。這樣的設計把「長按後要做什麼」集中在同一個類別裡,各頁面的差異只用一個 TAG 就能區分,後續要加選項也只要改字串陣列。
Android 異步任務入門:Handler、AsyncTask 與 runOnUiThread 三種實現
面試時聊到同步與異步,讓我重新梳理了一遍這個概念。日常開發裡我們常把用 Handler 刷新主線程這類操作稱為異步任務,而其中最常用的莫過於 AsyncTask。趁這個機
一篇理清 Android 的轉場動畫(Transitions)
背景 隨著 Material Design 概念的普及,動畫與 UI 的優化在開發中變得越來越重要,而「Material Motion(-material 動態效果)」正
Android UI 開發(四十三):用 Property Animation 重現墨跡天氣 3.0 引導頁動畫
先前寫過《墨跡天氣 3.0 引導界面及動畫實現》,那篇使用的是 View Animation;本篇改用 Property Animation 重做一遍。Property
小米「請勿遮擋橙色區域」怎麼解?防誤觸與距離感測器校正
小米手機出現請勿遮擋橙色區域,通常是防誤觸或距離感測器作用。可先到設定、鎖屏和密碼,關閉防誤觸模式。 若仍異常,可在撥號盤輸入工程模式,進入距離感應器測試,將手指靠近感測