編輯:關於 Android 開發
幾乎每個人都用過QQ空間客戶端。在每一條說說底下,我們都能留下評論。仔細觀察一條「回復型」評論,你會發現它其實藏著不少互動細節:
白雪公主 回復 小矮人 : 你們好啊~
把它拆開來看,可以歸納出三個關鍵行為:
1、點擊「白雪公主」或「小矮人」這類使用者名稱,會跳轉到對應的個人主頁;也就是點擊文字即可進入該使用者的頁面。 2、點擊回復的正文內容「你們好啊~」,會彈出編輯框,讓你針對回復者「白雪公主」繼續回覆。 3、回復者與被回復者的名字,會以不同顏色高亮顯示。
效果示意圖:

站在Android開發者的角度,歸根結底我們要搞定的是一個TextView,並實現兩件事:
1、點擊其中不同的文字片段(片段數量並不固定)時,觸發各自對應的操作,例如跳轉個人主頁; 2、同一個TextView裡的不同文字,可以呈現不同顏色。
下面就一步步來實現它。
先交代一下整體思路。說說列表這個界面(以Fragment實現)的骨架是這樣的:
1、用RecyclerView承載整個說說列表。為什麼選它?
1、RecyclerView自帶item復用機制,對於有一兩年工作經驗的人來說,不建議再自己手寫ListView復用邏輯; 2、RecyclerView在對單個item做增刪改時更省心(這是它的突出優勢),比如實現「刪除這條說說」的功能,用RecyclerView會順手很多。
2、每個item內部,評論文字區域用一個「不可滑動的ListView」來展示(理論上換成RecyclerView更好,總之達到不可滑動的效果即可)。當初也考慮過用LinearLayout動態添加多個TextView,但實測下來既繁瑣又容易出錯,於是放棄。
不可滑動ListView的實現,可參考「自定義不可滑動的ListView和GridView」。
下面用一個Demo把評論展示的效果跑起來。先看布局文件,Demo裡只呈現評論列表,所以只需要一個不可滑動的ListView:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- 注意listview要去除分割線 -->
<com.xqx.com.qqhome.NoScrollListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null" >
</com.xqx.com.qqhome.NoScrollListView>
</RelativeLayout>
接著是item的布局(一條評論):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/txt_comment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="2dp" /> </LinearLayout>
再來看Java部分。MainActivity很直觀,我們手動造了5條評論塞進自定義的適配器裡。這裡有個要點:有些評論是沒有被回復人的。
public class MainActivity extends Activity { private NoScrollListView noScrollListView; /* --------- 數據源----------- */ //記錄回復說說用戶的集合 private ArrayList<String> name; //記錄被回復說說用戶的集合 private ArrayList<String> toName; //記錄評論內容的集合 private ArrayList<String> content; /* --------- 適配器------------*/ private CommentAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); noScrollListView = (NoScrollListView) findViewById(R.id.listview); name = new ArrayList<>(); toName = new ArrayList<>(); content = new ArrayList<>(); //添加數據 ,Demo只添加5條評論 name.add("白雪公主"); toName.add("小矮人"); content.add("你們好啊~"); name.add("小矮人"); toName.add("白雪公主"); content.add("白雪公主,早上好啊~"); name.add("王子"); toName.add(""); content.add("這條說說很有道理的樣子啊~"); name.add("國王"); toName.add(""); content.add("我很喜歡這條說說~"); name.add("白雪公主"); toName.add("王子"); content.add("你也是XX的朋友啊?"); adapter = new CommentAdapter(name,toName,content,this); noScrollListView.setAdapter(adapter); } }
布局有了、MainActivity也有了,接下來是重頭戲——自定義適配器。先聲明需要的屬性並寫好基礎方法:
public class CommentAdapter extends BaseAdapter { /* --------- 數據源----------- */ //記錄回復說說用戶的集合 private ArrayList<String> name; //記錄被回復說說用戶的集合 private ArrayList<String> toName; //記錄評論內容的集合 private ArrayList<String> content; private Context context; public CommentAdapter(ArrayList<String> name, ArrayList<String> toName, ArrayList<String> content, Context context) { this.name = name; this.toName = toName; this.content = content; this.context = context; } @Override public int getCount() { int ret = 0; if (name != null&&name.size()!=0) ret = name.size(); return ret; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; }
class ViewHolder{
TextView txt_comment;
}
核心來了——getView()。動手前,建議先補一補這幾篇基礎:SpannableString與SpannableStringBuilder、TextView顯示HTML樣式文字、以及ClickableSpan實現TextView局部文字點擊響應。
@Override public View getView(int position, View convertView, ViewGroup parent) { //其實評論一般都是文字,高級點的帶有圖片評論,光文字的話復用不復用就沒什麼大區別了 View view = null; if(convertView!=null) { view = convertView; } else { view = LayoutInflater.from(context).inflate(R.layout.item_comment, parent,false); } ViewHolder holder = (ViewHolder) view.getTag(); if(holder==null) { holder = new ViewHolder(); holder.txt_comment = (TextView) view.findViewById(R.id.txt_comment); view.setTag(holder); } //給相應位置的文字賦內容 if (name != null && name.size()!=0) { StringBuilder actionText = new StringBuilder(); //誰回復 actionText.append("<a style=\"text-decoration:none;\" href='name' ><font color='#1468a3'>" + name.get(position) + "</font> </a>"); // 回復誰,被回復的人可能不存在。 if(toName.get(position)!=null&&toName.get(position).length()>0) { actionText.append("回復"); actionText.append("<font color='#1468a3'><a style=\"text-decoration:none;\" href='toName'>" + toName.get(position) + " " + " </a></font>"); } // 內容 actionText.append("<font color='#484848'><a style=\"text-decoration:none;\" href='content'>" + ":" + content.get(position) + " " + " </a></font>"); holder.txt_comment.setText(Html.fromHtml(actionText.toString())); holder.txt_comment.setMovementMethod(LinkMovementMethod .getInstance()); CharSequence text = holder.txt_comment.getText(); int ends = text.length(); Spannable spannable = (Spannable) holder.txt_comment.getText(); URLSpan[] urlspan = spannable.getSpans(0, ends, URLSpan.class); SpannableStringBuilder stylesBuilder = new SpannableStringBuilder(text); stylesBuilder.clearSpans(); for (URLSpan url : urlspan) { FeedTextViewURLSpan myURLSpan = new FeedTextViewURLSpan(url.getURL(), context,name.get(position),toName.get(position),content.get(position)); stylesBuilder.setSpan(myURLSpan, spannable.getSpanStart(url), spannable.getSpanEnd(url), spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } holder.txt_comment.setText(stylesBuilder); holder.txt_comment.setFocusable(false); holder.txt_comment.setClickable(false); holder.txt_comment.setLongClickable(false); } return view; } static class FeedTextViewURLSpan extends ClickableSpan { private String clickString; private Context context; // 回復人的名字 private String name; // 被回復人的名字 private String toName; // 評論內容 private String content; public FeedTextViewURLSpan(String clickString, Context context, String name, String toName, String content) { this.clickString = clickString; this.context = context; this.name = name; this.toName = toName; this.content = content; } @Override public void updateDrawState(TextPaint ds) { ds.setUnderlineText(false); //給標記的部分 的文字 添加顏色 if(clickString.equals("toName")){ ds.setColor(context.getResources().getColor(R.color.blue)); }else if(clickString.equals("name")){ ds.setColor(context.getResources().getColor(R.color.blue)); } } @Override public void onClick(View widget) { // 根據文字的標記 來進行相應的 響應事件 if (clickString.equals("toName")) { //可以再次進行跳轉activity的操作 Toast.makeText(context,"點擊了"+toName,Toast.LENGTH_SHORT).show(); } else if (clickString.equals("name")) { //可以再次進行跳轉activity的操作 Toast.makeText(context,"點擊了"+name,Toast.LENGTH_SHORT).show(); } else if(clickString.equals("content")){ //可以再次進去回復評論的操作 Toast.makeText(context,"點擊了"+content,Toast.LENGTH_SHORT).show(); } } }
適配器完整代碼:

CommentAdapter.java
到這裡,說說評論列表的展示就講完了。那麼接下來兩個問題浮現出來:如何撰寫並送出新的回復?新評論又該怎樣即時刷新到當前列表?這些留到下一篇繼續探討。
延伸閱讀:QQ空間實現(二)——分享功能 / 彈出PopupWindow。
博主目前專注社交、社區類APP開發,歡迎同領域的朋友關注交流。
仿 QQ 圖片選擇器:Android 本地圖片選擇功能實作
做一款 APP 時,若需要選擇本地圖片,首先想到的往往是系統相冊。但 Android 手機型號五花八門,加上手機像素不斷提升、大圖無法返回等異常因素,導致適配各機型相當困
Android 學習筆記:SQLite 資料庫入門與 CRUD 完整示範
Android 學習筆記:SQLite 資料庫入門與 CRUD 完整示範 前陣子學了 Android 的幾種資料儲存方式:檔案、SharedPreferences,以及最
日誌數據分析:主帳號登錄、登出時間過於集中的排查思路
這是一個日誌分析的典型場景:需要判斷某公司主帳號的登錄行為是否存在時間過於集中的情況。數據源為一個月的登錄原始日誌。整個過程看似只是幾條 SQL,實際卻踩到了一些容易被忽
Activity 詳解(四):四種啟動模式 launchMode 完全解析
先看效果圖: 一、概述 Activity 的啟動方式共有四種:standard、singleTop、singleTask、singleInstance。按實際業務場景為