本文实例为大家分享了Android实现朋友圈评论回复列表的具体代码,供大家参考,具体内容如下
Android实现朋友圈评论回复列表
Android实现朋友圈点赞列表
Android实现朋友圈多图显示功能
正文
还是老流程,先来看一下效果图:
然后是主要实现代码:
CommentsView
public class CommentsView extends LinearLayout { private Context mContext; private ListmDatas; private onItemClickListener listener; public CommentsView(Context context) { this(context, null); } public CommentsView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public CommentsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setOrientation(VERTICAL); this.mCOntext= context; } /** * 设置评论列表信息 * * @param list */ public void setList(List list) { mDatas = list; } public void setOnItemClickListener(onItemClickListener listener) { this.listener = listener; } public void notifyDataSetChanged() { removeAllViews(); if (mDatas == null || mDatas.size() <= 0) { return; } LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); layoutParams.setMargins(0, 10, 0, 10); for (int i = 0; i
CircleMovementMethod
public class CircleMovementMethod extends BaseMovementMethod { private final static int DEFAULT_COLOR_ID = android.R.color.transparent; /** * 整个textView的背景色 */ private int textViewBgColor; /** * 点击部分文字时部分文字的背景色 */ private int clickableSpanBgClor; private BackgroundColorSpan mBgSpan; private ClickableSpan[] mClickLinks; /** * @param clickableSpanBgClor 点击选中部分时的背景色 */ public CircleMovementMethod(int clickableSpanBgClor) { this.clickableSpanBgClor = clickableSpanBgClor; } /** * @param clickableSpanBgClor 点击选中部分时的背景色 * @param textViewBgColor 整个textView点击时的背景色 */ public CircleMovementMethod(int clickableSpanBgClor, int textViewBgColor) { this.textViewBgColor = textViewBgColor; this.clickableSpanBgClor = clickableSpanBgClor; } public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= widget.getTotalPaddingLeft(); y -= widget.getTotalPaddingTop(); x += widget.getScrollX(); y += widget.getScrollY(); Layout layout = widget.getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); mClickLinks = buffer.getSpans(off, off, ClickableSpan.class); if (mClickLinks.length > 0) { // 点击的是Span区域,不要把点击事件传递 Selection.setSelection(buffer, buffer.getSpanStart(mClickLinks[0]), buffer.getSpanEnd(mClickLinks[0])); //设置点击区域的背景色 mBgSpan = new BackgroundColorSpan(clickableSpanBgClor); buffer.setSpan(mBgSpan, buffer.getSpanStart(mClickLinks[0]), buffer.getSpanEnd(mClickLinks[0]), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } else { // textview选中效果 // widget.setBackgroundColor(textViewBgColor); widget.setBackgroundResource(DEFAULT_COLOR_ID); } } else if (action == MotionEvent.ACTION_UP) { if (mClickLinks.length > 0) { mClickLinks[0].onClick(widget); if (mBgSpan != null) {//移除点击时设置的背景span buffer.removeSpan(mBgSpan); } } else { } Selection.removeSelection(buffer); widget.setBackgroundResource(DEFAULT_COLOR_ID); } else if (action == MotionEvent.ACTION_MOVE) { //这种情况不用做处理 } else { if (mBgSpan != null) {//移除点击时设置的背景span buffer.removeSpan(mBgSpan); } widget.setBackgroundResource(DEFAULT_COLOR_ID); } return Touch.onTouchEvent(widget, buffer, event); } }
相关数据结构(模拟)
CommentsBean
public class CommentsBean implements Serializable { private int commentsId; private String content; private UserBean replyUser; // 回复人信息 private UserBean commentsUser; // 评论人信息 public int getCommentsId() { return commentsId; } public void setCommentsId(int commentsId) { this.commentsId = commentsId; } public String getContent() { return content; } public void setContent(String content) { this.cOntent= content; } public UserBean getReplyUser() { return replyUser; } public void setReplyUser(UserBean replyUser) { this.replyUser = replyUser; } public UserBean getCommentsUser() { return commentsUser; } public void setCommentsUser(UserBean commentsUser) { this.commentsUser = commentsUser; } }
UserBean
public class UserBean implements Serializable { private int userId; private String userName; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } }
用法
commentView = LvV.find(this, R.id.commentView); commentView.setList(Data.getComments()); commentView.setOnItemClickListener(new CommentsView.onItemClickListener() { @Override public void onItemClick(int position, CommentsBean bean) { } }); commentView.notifyDataSetChanged();
代码已整理到Github
附:如果需要完整朋友圈项目的话,这里推荐一个 Github 项目仿微信实现的朋友圈
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。