本文執行個體為大家分享了Android實作朋友圈評論回複清單的具體代碼,供大家參考,具體内容如下
正文
還是老流程,先來看一下效果圖:

然後是主要實作代碼:
CommentsView
public class CommentsView extends LinearLayout {
private Context mContext;
private List 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,defStyleAttr);
setOrientation(VERTICAL);
this.mContext = context;
}
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,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;
}
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,string.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return string;
}
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,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;
private int textViewBgColor;
private int clickableSpanBgClor;
private BackgroundColorSpan mBgSpan;
private ClickableSpan[] mClickLinks;
public CircleMovementMethod(int clickableSpanBgClor) {
this.clickableSpanBgClor = clickableSpanBgClor;
}
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.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;
}
}
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 項目仿微信實作的朋友圈
以上就是本文的全部内容,希望對大家的學習有所幫助,也希望大家多多支援程式設計小技巧。
總結
如果覺得程式設計之家網站内容還不錯,歡迎将程式設計之家網站推薦給程式員好友。
本圖文内容來源于網友網絡收集整理提供,作為學習參考使用,版權屬于原作者。
小編個人微信号 jb51ccc
喜歡與人分享程式設計技術與工作經驗,歡迎加入程式設計之家官方交流群!