CommentListTextView
一个TextView实现朋友圈多条评论效果 朋友圈 微博 评论 comment
Install / Use
/learn @itgowo/CommentListTextViewREADME
CommentListTextView#
说明
我是将朋友圈分成了几个独立模块单独自定义的View,通过回调完成交互,耦合性算是非常低了,主要有以下及部分:
1.评论布局(自定义TextView)
2.点赞布局(原理和评论的自定义TextView一样,都是用的SpannableString)
3.图片列表(出门右转,理论上没有数量限制,和listView配合使用也很好,缓存也自己处理了)
我也是找第三方例子不好找,于是自己写了一个,我和同事还打算做一套IM系统,app和后台都要做,我们自己定义sdk,我们还要封装H5,类似hbuilder如果有什么问题,可以联系我,
QQ:1264957104
CSDN:http://blog.csdn.net/hnsugar
GitHub:https://github.com/hnsugar
个人做测试项目的服务器:http://lujianchao.com http://itgowo.com
链接是跳转到GitHub的,部分文章我会直接贴出关键View的代码。
示例

##主要方法##
####设置评论最大显示行数####
mCommentListTextView.setMaxlines (6);
####设置超过最大行数下面显示的提示文本####
mCommentListTextView.setMoreStr ("查看更多");
####设置名字文本显示颜色####
mCommentListTextView.setNameColor (Color.parseColor ("#fe671e"));
####设置评论内容文本颜色####
mCommentListTextView.setCommentColor (Color.parseColor ("#242424"));
####设置名字之间的文本####
mCommentListTextView.setTalkStr ("回复");
####设置名字之间的文本颜色####
mCommentListTextView.setTalkColor (Color.parseColor ("#242424"));
####设置显示数据####
mCommentListTextView.setData (mCommentInfos);
####设置监听
mCommentListTextView.setonCommentListener (new CommentListTextView.onCommentListener ())
onNickNameClick (int position, CommentListTextView.CommentInfo mInfo)
“A回复B”中A名称被点击 position是第几条评论,mInfo是这条评论的信息
onToNickNameClick (int position, CommentListTextView.CommentInfo mInfo)
“A回复B”中B名称被点击 position是第几条评论,mInfo是这条评论的信息
onCommentItemClick (int position, CommentListTextView.CommentInfo mInfo)
position是第几条评论,mInfo是这条评论的信息
onOtherClick
内部处理了点击文字会触发两个回调的问题,这个是点击非文字或者没有单独定义点击事件的回调
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lujianchao.commentlisttextview.commentlisttextview.MainActivity">
<com.lujianchao.commentlisttextview.commentlisttextview.CommentListTextView
android:id="@+id/commentlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"/>
<TextView
android:id="@+id/log"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="match_parent"/>
</LinearLayout>
代码
public class MainActivity extends AppCompatActivity {
private CommentListTextView mCommentListTextView;
private TextView mTextView;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
mCommentListTextView = (CommentListTextView) findViewById (R.id.commentlist);
mTextView = (TextView) findViewById (R.id.log);
test ();
}
private void test () {
mTextView.setMovementMethod (ScrollingMovementMethod.getInstance ());
mCommentListTextView.setMaxlines (6);
mCommentListTextView.setMoreStr ("查看更多");
mCommentListTextView.setNameColor (Color.parseColor ("#fe671e"));
mCommentListTextView.setCommentColor (Color.parseColor ("#242424"));
mCommentListTextView.setTalkStr ("回复");
mCommentListTextView.setTalkColor (Color.parseColor ("#242424"));
List<CommentListTextView.CommentInfo> mCommentInfos = new ArrayList<> ();
mCommentInfos.add (new CommentListTextView.CommentInfo ().setID (1111).setComment ("今天天气真好啊!11").setNickname ("张三").setTonickname ("赵四"));
mCommentInfos.add (new CommentListTextView.CommentInfo ().setID (2222).setComment ("今天天气真好啊!22").setNickname ("赵四"));
mCommentInfos.add (new CommentListTextView.CommentInfo ().setID (3333).setComment ("今天天气真好啊!33").setNickname ("王五").setTonickname ("小三"));
mCommentInfos.add (new CommentListTextView.CommentInfo ().setID (4444).setComment ("今天天气真好啊!44").setNickname ("小三").setTonickname ("王五"));
mCommentInfos.add (new CommentListTextView.CommentInfo ().setID (5555).setComment ("今天天气真好啊!55").setNickname ("李大"));
mCommentInfos.add (new CommentListTextView.CommentInfo ().setID (6666).setComment ("今天天气真好啊!66").setNickname ("小三").setTonickname ("王五"));
mCommentInfos.add (new CommentListTextView.CommentInfo ().setID (7777).setComment ("今天天气真好啊!77").setNickname ("李大").setTonickname ("张三"));
mCommentInfos.add (new CommentListTextView.CommentInfo ().setID (8888).setComment ("今天天气真好啊!88").setNickname ("小三").setTonickname ("王五"));
mCommentInfos.add (new CommentListTextView.CommentInfo ().setID (9999).setComment ("今天天气真好啊!99").setNickname ("李大").setTonickname ("张三"));
mCommentListTextView.setData (mCommentInfos);
mCommentListTextView.setonCommentListener (new CommentListTextView.onCommentListener () {
@Override
public void onNickNameClick (final int position, final CommentListTextView.CommentInfo mInfo) {
mTextView.append ("onNickNameClick position = [" + position + "], mInfo = [" + mInfo + "]" + "\r\n");
}
@Override
public void onToNickNameClick (final int position, final CommentListTextView.CommentInfo mInfo) {
mTextView.append ("onToNickNameClick position = [" + position + "], mInfo = [" + mInfo + "]" + "\r\n");
}
@Override
public void onCommentItemClick (final int position, final CommentListTextView.CommentInfo mInfo) {
mTextView.append ("onCommentItemClick position = [" + position + "], mInfo = [" + mInfo + "]" + "\r\n");
}
@Override
public void onOtherClick () {
mTextView.append ("onOtherClick" + "\r\n");
}
});
}
}
##源码##
package com.lujianchao.commentlisttextview.commentlisttextview;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import java.util.List;
/**
* Created by lujianchao on 2017/1/19.
*
* @author lujianchao
*/
public class CommentListTextView extends TextView {
/**
* 所有评论数据
*/
private List<CommentInfo> mInfos;
private onCommentListener mListener;
/**
* 点击文字会触发两个回调,用这个变量控制屏蔽掉一个
*/
private boolean isNickNameClick = false;
/**
* 最大显示行数,超过指定行数多一行显示为查看更多文本,可设置文本
*/
private int mMaxlines = 6;
/**
* 当超过n行后,n+1行显示为这个文本;
*/
private String mMoreStr = "查看全部评论";
/**
* 谁回复谁中回复字符串,可以变成别的
*/
private String mTalkStr = "回复";
/**
* 人名称颜色
*/
private int mNameColor = Color.parseColor ("#fe671e");
private int mCommentColor = Color.parseColor ("#242424");
private int mTalkColor = Color.parseColor ("#242424");
public int getTalkColor () {
return mTalkColor;
}
public CommentListTextView setTalkColor (final int mTalkColor) {
this.mTalkColor = mTalkColor;
return this;
}
public int getMaxlines () {
return mMaxlines;
}
public CommentListTextView setMaxlines (final int mMaxlines) {
this.mMaxlines = mMaxlines;
return this;
}
public String getMoreStr () {
return mMoreStr;
}
public CommentListTextView setMoreStr (final String mMoreStr) {
this.mMoreStr = mMoreStr;
return this;
}
public String getTalkStr () {
return mTalkStr;
}
public CommentListTextView setTalkStr (final String mTalkStr) {
this.mTalkStr = mTalkStr;
return this;
}
public int getNameColor () {
return mNameColor;
}
public CommentListTextView setNameColor (final int mNameColor) {
this.mNameColor = mNameColor;
return this;
}
public int getCommentColor () {
return mCommentColor;
}
public CommentListTextView setCommentColor (final int mCommentColor) {
this.mCommentColor = mCommentColor;
return this;
}
public CommentListTextView (final Context context) {
super (context);
}
public CommentListTextView (final Context context, final AttributeSet attrs) {
super (context, attrs);
}
public CommentListTextView setonCommentListener (final onCommentListener mListener) {
this.mListener = mListener;
return this;
}
public void setData (List<CommentInfo> mInfos) {
this.mInfos = mInfos;
/**
* textview必须设置,否则自定义点击事件没响应
*/
setMovementMethod (LinkMovementMethod.getInstance ());
setHighlightColor (Color.TRANSPARENT);
setText (getCommentString ());
setOnClickListener (new OnClickListener () {
@Override
public void onClick (final View v) {
if (isNickNameClick) {
isNickNameClick = false;
return;
Related Skills
node-connect
346.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
107.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
346.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
346.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
