大家都玩QQ空间客户端,对于每一个说说,我们都可以评论,那么,对于某一条评论:

白雪公主 回复 小矮人 : 你们好啊~

我们来分析一下:

、QQ空间允许我们 点击 回复人和被回复人的名字就可以进入对于用户的个人主页(即点击文字“白雪公主”/“小矮人”,就可以进入到这俩用户相应个人主页)
、点击 回复的文字,就可以对回复人进行回复(即点击评论中回复的内容“你们好啊~”,便对弹出一个编辑框对回复人“白雪公主”进行回复)
、回复人 和 被回复人 的名字是有颜色的

效果图:

作为一个android开发者,我们要实现对一个TextView :

、点击不同的文字部分(文字个数还不确定)有相应的响应操作(进入个人主页等等)

、一个TextView中某些文字有不同的颜色

下面学习如何实现-->

----------------------------------------------------------------------------------

首先介绍下QQ空间说说列表这一个界面(fragment来实现)的整体框架:

1、使用RecyclerView来展示说说列表   why?

、RecyclerView 自带实现复用机制,对于工作1--2年左右的,不建议使用自己写的复用ListView
、RecyclerView 方便对于某一个item 项的增删改操作 (大优势),比如控件删除该说说的功能的实现 RecyclerView实现更好

2、每一个item 内部 ,评论文字部分 用不可以滑动的ListView(RecyclerView理论上更棒,反正不可以滑动就行了)来展示 (博主一开始想的是用LinearLayout 内部 动态添加TextView来展示,经测试,太麻烦且易出错)

不可滑动的ListView 代码 --> 自定义不可滑动的ListView和GridView

-----------------------------------------------------------------------------------

下面用一个Demo来学习如何实现说说评论的效果:

首先布局文件,就一个不可滑动的ListView,我们Demo只展示评论列表

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://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="http://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.java

很简单,自己创建了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 = ;
if (name != null&&name.size()!=)
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()!=) {
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()>) {
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(, 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();
}
}
}

适配器完整代码:

import android.content.Context;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList; 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 = ;
if (name != null&&name.size()!=)
ret = name.size();
return ret;
} @Override
public Object getItem(int position) {
return position;
} @Override
public long getItemId(int position) {
return position;
} @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()!=) {
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()>) {
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(, 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();
} }
} class ViewHolder{
TextView txt_comment;
} }

CommentAdapter.java

-----------------------------------------------------------------------------------

如何实现QQ空间说说列表评论的展示介绍完了~~

那么如何 回复评论呢?

如何将新评论的评论及时的显示在当前列表呢?

之后的博客继续讨论~~~

相关知识:

QQ空间实现(二)—— 分享功能 / 弹出PopupWindow

博主现在从事社交类社区类APP开发,有同领域的朋友欢迎关注交流~~~

Android项目实战(十六):QQ空间实现(一)—— 展示说说中的评论内容并有相应点击事件的更多相关文章

  1. Android项目实战(六):JazzyGridView和JazzyListView的使用

    GridView和ListView控件划动的动画效果 ------------------------------------------------------------------------- ...

  2. (转载)Android项目实战(十七):QQ空间实现(二)—— 分享功能 / 弹出PopupWindow

    Android项目实战(十七):QQ空间实现(二)—— 分享功能 / 弹出PopupWindow   这是一张QQ空间说说详情的截图. 分析: 1.点击右上角三个点的图标,在界面底部弹出一个区域,这个 ...

  3. Android项目实战(十七):QQ空间实现(二)—— 分享功能 / 弹出PopupWindow

    这是一张QQ空间说说详情的截图. 分析: .点击右上角三个点的图标,在界面底部弹出一个区域,这个区域有一些按钮提供给我们操作 .当该区域出现的时候,详情界面便灰了,也说成透明度变化了 .当任意选了一个 ...

  4. Android项目实战(四十九):Andoird 7.0+相机适配

    解决方案类似: Android项目实战(四十):Andoird 7.0+ 安装APK适配 解决方法: 一.在AndroidManifest.xml 文件中添加 四大组件之一的 <provider ...

  5. (转载)Android项目实战(三十二):圆角对话框Dialog

    Android项目实战(三十二):圆角对话框Dialog   前言: 项目中多处用到对话框,用系统对话框太难看,就自己写一个自定义对话框. 对话框包括:1.圆角 2.app图标 , 提示文本,关闭对话 ...

  6. (转载)Android项目实战(二十八):Zxing二维码实现及优化

    Android项目实战(二十八):Zxing二维码实现及优化   前言: 多年之前接触过zxing实现二维码,没想到今日项目中再此使用竟然使用的还是zxing,百度之,竟是如此牛的玩意. 当然,项目中 ...

  7. (转载)Android项目实战(二十八):使用Zxing实现二维码及优化实例

    Android项目实战(二十八):使用Zxing实现二维码及优化实例 作者:听着music睡 字体:[增加 减小] 类型:转载 时间:2016-11-21我要评论 这篇文章主要介绍了Android项目 ...

  8. Android项目实战(四十四):Zxing二维码切换横屏扫描

    原文:Android项目实战(四十四):Zxing二维码切换横屏扫描 Demo链接 默认是竖屏扫描,但是当我们在清单文件中配置横屏显示的时候: <activity android:name=&q ...

  9. Android项目实战(四十):在线生成按钮Shape的网站

    原文:Android项目实战(四十):在线生成按钮Shape的网站 AndroidButton Make  右侧设置按钮的属性,可以即时看到效果,并即时生成对应的.xml 代码,非常高效(当然熟练的话 ...

随机推荐

  1. Mesh Wifi

    best idea ever Submitted by ozzy (not verified) on Mon, 2009-09-21 09:39. I thought of doing this on ...

  2. MFC设置静态文本框,编辑框等控件背景和字体颜色

    在MFC类库提供了CWnd::OnCtlColor函数,在工作框架的子窗口被重画时将调用该成员函数.因此可以重载WM_CTLCOLOR消息的响应函数.此函数的原型:afx_msg HBRUSH OnC ...

  3. [转]查看手机已经记住的WIFI密码

    有时用过wifi后记住密码了,但再想知道wifi密码是多少,怎么办呢.下面的方法为你解决这样的问题. 1.手机必须取得root权限. 2.用RE管理器或es文件浏览器进入data/misc/wifi, ...

  4. [转]Visual Studio技巧之打造拥有自己标识的代码模板

    可能经过很多博客的介绍,大家都知道代码段的使用,使用代码段可以很方便地生成一些常用的代码格式,确实对我们开发很方便.在团队开发中或者在某些情况下我们经常可能还会希望使用Visual Studio生成的 ...

  5. C#调用Java类

    C#调用Java类 (2011-01-07 14:02:05) 转载▼   分类: Java学习  1. 在Eclipse中新建名称为hello的java project,此工程仅包含一个文件hell ...

  6. css3,background-clip/background-origin的使用场景,通俗讲解

    先不说background-clip/background-origin的用法,我们先来聊聊css背景方面的知识. <!DOCTYPE html> <html lang=" ...

  7. 自动化回归测试案例评价标准 MeRest

    自动化回归测试案例评价标准试图定义不同维度来评价自动化案例的优劣,作为后续我们评判讨论测试框架.测试技术和测试案例编写模式的基础.那什么是好的自动化回归测试案例呢?简而言之,就是投资回报率高的案例,因 ...

  8. Windows7下的Java运行环境搭建过程图解

    第一步:下载JDK 地址:http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html,(由于Sun于20 ...

  9. Teehan & Lax 发布 iOS 7 GUI PSD 模板,免费下载

    在 iOS 7 发布不久,Teehan & Lax 就发布了 iOS 7 GUI PSD 模板.该网站分享众多 PSD 模板素材,这些精美的 PSD 界面模板在制作界面原型非常有用,能够帮助设 ...

  10. CentOS6.4安装包初识

    LiveCD 一般用来修复系统使用,有容量很小,不用安装,可以自启动等特性.bin DVD也具有同 样的功能,但是体积较大,所以会有live DVD. LiveDVD 与LiveCD 相同是不需要安装 ...