简单明了易懂的回复功能(仿 QQ 空间,朋友圈)
先上图 (其实但看这一页面中也包含了不少功能,比如图片压缩、分享、RecyclerView、点赞、仿朋友圈评论键盘样式
如何做到跟朋友圈、qq空间般回复一般样式,这需要 用到
SpannableStringBuilder (该类功能强大,值得一看)
这是一个文本和标记都可以更改的文本类
设置文本样式:
start :设置样式的起始位置
end :设置样式的结束位置
flags:为以下四种
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)
Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)
Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)
Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)
Replay replay = replayData.get(position); ViewHolder viewHolder; if (view == null) { viewHolder = new ViewHolder();//创建ViewHolder view = layoutInflater.inflate(R.layout.layout_list_item_replay, null); viewHolder.text = view.findViewById(R.id.text); view.setTag(viewHolder); } else { viewHolder = (ViewHolder) view.getTag(); } // Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括) // Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括) // Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括) // Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)。看个截图就更明白了:
//核心代码: 是下面这些设置样式的,其他可根据自己项目需要改变
SpannableStringBuilder builder = new SpannableStringBuilder(); int end = 0; if (answer.userId.equals(replay.userId)) { // 判断是否是发出这个问题的用户 builder.append(replay.nickname); //评论人 builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.green)), 0, builder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); //设置字体颜色 int start = builder.length(); builder.append(": "); end = builder.length(); builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorDarkGray)), start, builder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); } else { builder.append(replay.nickname); //评论人 builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.green)), 0, builder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); int start1 = builder.length(); builder.append("回复"); builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorDarkGray)), start1, builder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); int start2 = builder.length(); builder.append(replay.replies); end = builder.length(); builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.green)), start2, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE); builder.append(": "); } builder.append(replay.replyText); builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorDarkGray)), end, builder.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); viewHolder.text.setText(builder);
//核心代码: 是上面这些设置样式的,其他可根据自己项目需要改变
return view;欢迎不懂的初学者评论