欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现朋友圈评论回复列表

程序员文章站 2022-07-11 17:54:21
本文实例为大家分享了android实现朋友圈评论回复列表的具体代码,供大家参考,具体内容如下 android实现朋友圈评论回复列表 android实现朋友圈点赞列表...

本文实例为大家分享了android实现朋友圈评论回复列表的具体代码,供大家参考,具体内容如下

android实现朋友圈评论回复列表

android实现朋友圈点赞列表

android实现朋友圈多图显示功能

正文

还是老流程,先来看一下效果图:

Android实现朋友圈评论回复列表

然后是主要实现代码:

commentsview

public class commentsview extends linearlayout {

 private context mcontext;
 private list<commentsbean> mdatas;
 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<commentsbean> 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 < mdatas.size(); i++) {
   view view = getview(i);
   if (view == null) {
    throw new nullpointerexception("listview item layout is null, please check getview()...");
   }
   addview(view, i, layoutparams);
  }
 }

 private view getview(final int position) {
  final commentsbean item = mdatas.get(position);
  userbean replyuser = item.getreplyuser();
  boolean hasreply = false; // 是否有回复
  if (replyuser != null) {
   hasreply = true;
  }
  textview textview = new textview(mcontext);
  textview.settextsize(15);
  textview.settextcolor(0xff686868);

  spannablestringbuilder builder = new spannablestringbuilder();
  userbean comuser = item.getcommentsuser();
  string name = comuser.getusername();
  if (hasreply) {
   builder.append(setclickablespan(name, item.getcommentsuser()));
   builder.append(" 回复 ");
   builder.append(setclickablespan(replyuser.getusername(), item.getreplyuser()));

  } else {
   builder.append(setclickablespan(name, item.getcommentsuser()));
  }
  builder.append(" : ");
  builder.append(setclickablespancontent(item.getcontent(), position));
  textview.settext(builder);
  // 设置点击背景色
  textview.sethighlightcolor(getresources().getcolor(android.r.color.transparent));
//  textview.sethighlightcolor(0xff000000);

  textview.setmovementmethod(new circlemovementmethod(0xffcccccc, 0xffcccccc));

  textview.setonclicklistener(new onclicklistener() {
   @override
   public void onclick(view v) {
    if (listener != null) {
     listener.onitemclick(position, item);
    }
   }
  });

  return textview;
 }

 /**
  * 设置评论内容点击事件
  *
  * @param item
  * @param position
  * @return
  */
 public spannablestring setclickablespancontent(final string item, final int position) {
  final spannablestring string = new spannablestring(item);
  clickablespan span = new clickablespan() {
   @override
   public void onclick(view widget) {
    // todo: 2017/9/3 评论内容点击事件
    toast.maketext(mcontext, "position: " + position + " , content: " + item, toast.length_short).show();
   }

   @override
   public void updatedrawstate(textpaint ds) {
    super.updatedrawstate(ds);
    // 设置显示的内容文本颜色
    ds.setcolor(0xff686868);
    ds.setunderlinetext(false);
   }
  };
  string.setspan(span, 0, string.length(), spannable.span_exclusive_exclusive);
  return string;
 }

 /**
  * 设置评论用户名字点击事件
  *
  * @param item
  * @param bean
  * @return
  */
 public spannablestring setclickablespan(final string item, final userbean bean) {
  final spannablestring string = new spannablestring(item);
  clickablespan span = new clickablespan() {
   @override
   public void onclick(view widget) {
    // todo: 2017/9/3 评论用户名字点击事件
    toast.maketext(mcontext, bean.getusername(), toast.length_short).show();
   }

   @override
   public void updatedrawstate(textpaint ds) {
    super.updatedrawstate(ds);
    // 设置显示的用户名文本颜色
    ds.setcolor(0xff387dcc);
    ds.setunderlinetext(false);
   }
  };

  string.setspan(span, 0, string.length(), spannable.span_exclusive_exclusive);
  return string;
 }

 /**
  * 定义一个用于回调的接口
  */
 public interface onitemclicklistener {
  void onitemclick(int position, commentsbean bean);
 }
}

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;
 }
}

用法

<com.lvfq.myworkingtest.dynamic.view.commentsview
  android:id="@+id/commentview"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_margin="@dimen/dp_20" />

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 项目仿微信实现的朋友圈

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。