Android开发之评论回复功能

Android开发之评论回复功能

  • 一:效果图
  • 二:具体代码
    • 1.首先是布局文件(activity_main)
    • 2.第二个布局文件(comment_item)
    • 3.第三个布局文件(reply_item)
    • 4.主java文件(MainActivity)
    • 5.CommentAdapter
    • 6.CommentBean
    • 7.NoScrollListView
    • 8.ReplyAdapter
    • 9.ReplyBean
    • 10.strings.xml
  • 三、资源下载地址:[Android开发之评论回复功能](https://download.csdn.net/download/qq_61963074/86434415)

目前,各种App的社区或者用户晒照片、发说说的地方,都提供了评论功能,于是在我的项目中也实现了这个功能。
废话不多说,直接上图和代码

一:效果图

Android开发之评论回复功能_第1张图片

二:具体代码

1.首先是布局文件(activity_main)

Android开发之评论回复功能_第2张图片

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="none"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:paddingTop="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                
                <ImageView
                    android:id="@+id/senderImg"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:scaleType="fitXY"
                    android:layout_marginRight="10dp"
                    android:contentDescription="@string/app_name"/>
                
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="30dp">
                        
                        <TextView
                            android:textSize="12sp"
                            android:textColor="#23238E"
                            android:id="@+id/senderNickname"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:gravity="center_vertical"
                            android:layout_alignParentLeft="true"/>
                        
                        <TextView
                            android:id="@+id/sendTime"
                            android:textSize="8sp"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:gravity="center_vertical"
                            android:layout_alignParentRight="true"/>
                    RelativeLayout>
                    
                    <TextView
                        android:id="@+id/sendContent"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:maxLines="4"
                        android:textSize="12sp"
                        android:layout_marginTop="5dp"/>
                    
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="40dp"
                        android:layout_marginTop="20dp">









                        
                        
                        <ImageView
                            android:id="@+id/commentImg"
                            android:layout_width="40dp"
                            android:layout_height="match_parent"
                            android:contentDescription="@string/app_name"
                            android:scaleType="fitXY"
                            android:src="@drawable/pl1" />

                        <ImageView
                            android:id="@+id/praiseImg"
                            android:layout_width="40dp"
                            android:layout_height="match_parent"
                            android:layout_alignParentRight="true"
                            android:contentDescription="@string/app_name"
                            android:scaleType="fitXY"
                            android:src="@drawable/dzy1" />
                    RelativeLayout>
                LinearLayout>
            LinearLayout>
            
            <com.example.NoScrollListView
                android:id="@+id/commentList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:divider="#00000000"/>
        LinearLayout>
    ScrollView>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/bottomLinear"
        android:orientation="horizontal"
        android:background="#CCCCCC">









        
        <TextView
            android:id="@+id/commentText"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/comment"
            android:textColor="#000000"/>


















    LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#CCCCCC"
        android:id="@+id/commentLinear"
        android:visibility="gone"
        android:orientation="horizontal">
        
        <EditText
            android:id="@+id/commentEdit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#FFFFFF"
            android:maxLength="100"
            android:minHeight="40dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:inputType="textMultiLine"
            android:gravity="left"
            android:maxLines="6"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_gravity="center_vertical"/>
        
        <Button
            android:id="@+id/commentButton"
            android:layout_width="50dp"
            android:layout_height="40dp"
            android:textColor="#000000"
            android:text="@string/leaveMessage"
            android:layout_marginRight="10dp"
            android:background="#FFFFFF"
            android:layout_gravity="center_vertical"/>
    LinearLayout>

LinearLayout>

2.第二个布局文件(comment_item)

Android开发之评论回复功能_第3张图片


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <ImageView
            android:id="@+id/commentItemImg"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:scaleType="fitXY"
            android:layout_marginRight="10dp"
            android:contentDescription="@string/app_name"/>
        
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            
            <TextView
                android:id="@+id/commentNickname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:textColor="#23238E"
                android:layout_alignParentLeft="true"/>
            
            <TextView
                android:id="@+id/replyText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:textColor="#23238E"
                android:text="@string/reply"
                android:layout_alignParentRight="true"/>
            
            <TextView
                android:id="@+id/commentItemTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="8sp"
                android:layout_below="@id/commentNickname"
                android:layout_alignParentLeft="true"/>
            
            <TextView
                android:id="@+id/commentItemContent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="12sp"
                android:layout_below="@id/commentItemTime"
                android:layout_alignParentLeft="true"/>
        RelativeLayout>
    LinearLayout>
    
    <com.example.NoScrollListView
        android:id="@+id/replyList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#00000000"
        android:layout_marginLeft="70dp"
        android:layout_marginBottom="10dp"/>
LinearLayout>

3.第三个布局文件(reply_item)


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
    
    <TextView
        android:id="@+id/replyContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="12sp"/>

LinearLayout>

4.主java文件(MainActivity)

package com.example;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;



import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private Button commentButton;		//评论按钮
    private EditText commentEdit;		//评论输入框
    private TextView senderNickname;	//发表者昵称
    private TextView sendTime;			//发表的时间
    private TextView sendContent;		//发表的内容
//    private TextView shareText;			//底部分享
    private TextView commentText;		//底部评论
//    private TextView praiseText;		//底部点赞
//    private TextView collectionText;	//底部收藏
    private ImageView senderImg;		//发送者图片
//    private ImageView shareImg;			//分享的图片
    private ImageView commentImg;		//评论的图片
    private ImageView praiseImg;		//点赞的图片
    private NoScrollListView commentList;//评论数据列表
    private LinearLayout bottomLinear;	//底部分享、评论等线性布局
    private LinearLayout commentLinear;	//评论输入框线性布局

    private int count;					//记录评论ID
    private int position;				//记录回复评论的索引
    private int[] imgs;					//图片资源ID数组
    private String[]  communicat;
    private boolean isReply;			//是否是回复
    private String comment = "";		//记录对话框中的内容
    private CommentAdapter adapter;
    private List<CommentBean> list;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initViews();
        init();
    }

    /**
     * 初始化UI界面
     */
    private void initViews(){
        commentButton = (Button) findViewById(R.id.commentButton);
        commentEdit = (EditText) findViewById(R.id.commentEdit);
        senderNickname = (TextView) findViewById(R.id.senderNickname);
        sendTime = (TextView) findViewById(R.id.sendTime);
        sendContent = (TextView) findViewById(R.id.sendContent);
//        shareText = (TextView) findViewById(R.id.shareText);
        commentText = (TextView) findViewById(R.id.commentText);
//        praiseText = (TextView) findViewById(R.id.praiseText);
//        collectionText = (TextView) findViewById(R.id.collectionText);
        senderImg = (ImageView) findViewById(R.id.senderImg);
//        shareImg = (ImageView) findViewById(R.id.shareImg);
        commentImg = (ImageView) findViewById(R.id.commentImg);
        praiseImg = (ImageView) findViewById(R.id.praiseImg);
        commentList = (NoScrollListView) findViewById(R.id.commentList);
        bottomLinear = (LinearLayout) findViewById(R.id.bottomLinear);
        commentLinear = (LinearLayout) findViewById(R.id.commentLinear);

        ClickListener cl = new ClickListener();
        commentButton.setOnClickListener(cl);
//        shareText.setOnClickListener(cl);
        commentText.setOnClickListener(cl);
//        praiseText.setOnClickListener(cl);
//        collectionText.setOnClickListener(cl);
//        shareImg.setOnClickListener(cl);
        commentImg.setOnClickListener(cl);
        praiseImg.setOnClickListener(cl);
    }

    /**
     * 初始化数据
     */
    private void init(){
        //初始化发布者信息
        @SuppressLint("ResourceType") InputStream is = getResources().openRawResource(R.drawable.by12);
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        senderImg.setImageBitmap(bitmap);
        senderNickname.setText("博主");
        sendTime.setText("13:24");
        sendContent.setText("欢迎各位学者分享自己的学习经验和问题");

        adapter = new CommentAdapter(this, getCommentData(),R.layout.comment_item,handler);
        commentList.setAdapter(adapter);
    }

    /**
     * 获取评论列表数据
     */
    private List<CommentBean> getCommentData(){
        imgs = new int[]{R.drawable.by12,R.drawable.dynamic2,
                R.drawable.dynamic3,R.drawable.dynamic4,R.drawable.by2,R.drawable.by7,R.drawable.by9,
                R.drawable.by8,R.drawable.by6,R.drawable.by1,R.drawable.by4,R.drawable.by10};
        communicat=new String[]{"中医的三个基本原理是什么?","据《三国志》记载名医华佗已开始使用全身麻醉剂进行各种外科手术,  那么请问这所指的麻醉剂是麻沸散吗。","晋代名医王叔和所著的《脉经》归纳了多少种脉象",
                "治疗血证有四法,即是指止血、祛瘀、宁血、补虚,那么请问此观点出自哪里?","《灵枢?海论》中有气海一说,那么气海是指?","六郁证即是指气、血、痰、湿、热、食,其观点出自《丹溪心法》。","一贯煎语出自《柳州医话》。",
        "自古医家认为羌活善治上半身风寒湿痹,那么请问哪一味药善治疗下半身风寒湿痹?","外科内治的三大法是指消、托、补吗。","“哮喘”这一病名首先见于《丹溪心法》","我国古代文献记载中有“小儿医”之说,其首次提出者为扁鹊,那么到唐代儿科被称作“少小科。",
        "小儿有一疾患称之为“脐风”,而成人已有一疾患称之为“破伤风”,那么请问指出“脐风”与“破伤风”为同一疾患的医书是《小儿卫生总微论方》。"};
        list = new ArrayList<CommentBean>();
        count = imgs.length;
        for(int i=0;i<imgs.length;i++){
            CommentBean bean = new CommentBean();
            bean.setId(i+1);
            bean.setCommentImgId(imgs[i]);
            bean.setCommentNickname("学者"+i);
            bean.setCommentTime("13:"+i+"5");
            bean.setCommnetAccount("12345"+i);
            bean.setCommentContent(communicat[i]);
            bean.setReplyList(getReplyData());
            list.add(bean);
        }
        return list;
    }

    /**
     * 获取回复列表数据
     */
    private List<ReplyBean> getReplyData(){
        List<ReplyBean> replyList = new ArrayList<ReplyBean>();
        return replyList;
    }

    /**
     * 显示或隐藏输入法
     */
    private void onFocusChange(boolean hasFocus){
        final boolean isFocus = hasFocus;
        (new Handler()).postDelayed(new Runnable() {
            public void run() {
                InputMethodManager imm = (InputMethodManager)
                        commentEdit.getContext().getSystemService(INPUT_METHOD_SERVICE);
                if(isFocus)  {
                    //显示输入法
                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                }else{
                    //隐藏输入法
                    imm.hideSoftInputFromWindow(commentEdit.getWindowToken(),0);
                }
            }
        }, 100);
    }

    /**
     * 判断对话框中是否输入内容
     */
    private boolean isEditEmply(){
        comment = commentEdit.getText().toString().trim();
        if(comment.equals("")){
            Toast.makeText(getApplicationContext(), "评论不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        commentEdit.setText("");
        return true;
    }

    /**
     * 发表评论
     */
    private void publishComment(){
        CommentBean bean = new CommentBean();
        bean.setId(count);
        bean.setCommentImgId(imgs[count%4]);
        bean.setCommentNickname("昵称"+count);
        bean.setCommentTime("13:"+count%6+"5");
        bean.setCommnetAccount("12345"+count);
        bean.setCommentContent(comment);
        list.add(bean);
        count++;
        adapter.notifyDataSetChanged();
    }

    /**
     * 回复评论
     */
    private void replyComment(){
        ReplyBean bean = new ReplyBean();
        bean.setId(count+10);
        bean.setCommentNickname(list.get(position).getCommentNickname());
        bean.setReplyNickname("我");
        bean.setReplyContent(comment);
        adapter.getReplyComment(bean, position);
        adapter.notifyDataSetChanged();
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 10){
                isReply = true;
                position = (Integer)msg.obj;
                commentLinear.setVisibility(View.VISIBLE);
                bottomLinear.setVisibility(View.GONE);
                onFocusChange(true);
            }
        }
    };

    /**
     * 事件点击监听器
     */
    private final class ClickListener implements OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.commentButton:	//发表评论按钮
                    if(isEditEmply()){		//判断用户是否输入内容
                        if(isReply){
                            replyComment();
                        }else{
                            publishComment();
                        }
                        bottomLinear.setVisibility(View.VISIBLE);
                        commentLinear.setVisibility(View.GONE);
                        onFocusChange(false);
                    }
                    break;
//                case R.id.shareImg:			//分享按钮
//                case R.id.shareText:		//底部分享按钮
//                    Toast.makeText(getApplicationContext(), "分享成功", Toast.LENGTH_SHORT).show();
//                    break;
                case R.id.commentImg:		//评论按钮
                case R.id.commentText:		//底部评论按钮
                    isReply = false;
                    commentLinear.setVisibility(View.VISIBLE);
                    bottomLinear.setVisibility(View.GONE);
                    onFocusChange(true);
                    break;
                case R.id.praiseImg:		//点赞按钮
//                case R.id.praiseText:		//底部点赞按钮
                    Toast.makeText(getApplicationContext(), "点赞成功", Toast.LENGTH_SHORT).show();
                    break;
//                case R.id.collectionText:	//底部收藏按钮
//                    Toast.makeText(getApplicationContext(), "收藏成功", Toast.LENGTH_SHORT).show();
//                    break;
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //判断控件是否显示
        if(commentLinear.getVisibility() == View.VISIBLE){
            commentLinear.setVisibility(View.GONE);
            bottomLinear.setVisibility(View.VISIBLE);
        }
    }

}

5.CommentAdapter

package com.example;

import java.io.InputStream;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CommentAdapter extends BaseAdapter {

    private int resourceId;
    private Context context;
    private Handler handler;
    private List<CommentBean> list;
    private LayoutInflater inflater;
    public CommentAdapter(Context context,List<CommentBean> list
            ,int resourceId,Handler handler){
        this.list = list;
        this.context = context;
        this.handler = handler;
        this.resourceId = resourceId;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommentBean bean = list.get(position);
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(resourceId, null);
            holder.commentItemImg = (ImageView)
                    convertView.findViewById(R.id.commentItemImg);
            holder.commentNickname = (TextView)
                    convertView.findViewById(R.id.commentNickname);
            holder.replyText = (TextView)
                    convertView.findViewById(R.id.replyText);
            holder.commentItemTime = (TextView)
                    convertView.findViewById(R.id.commentItemTime);
            holder.commentItemContent = (TextView)
                    convertView.findViewById(R.id.commentItemContent);
            holder.replyList = (NoScrollListView)
                    convertView.findViewById(R.id.replyList);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        InputStream is = context.getResources().openRawResource(bean.getCommentImgId());
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        holder.commentItemImg.setImageBitmap(bitmap);
        holder.commentNickname.setText(bean.getCommentNickname());
        holder.commentItemTime.setText(bean.getCommentTime());
        holder.commentItemContent.setText(bean.getCommentContent());

        ReplyAdapter adapter = new ReplyAdapter(context, bean.getReplyList(), R.layout.reply_item);
        holder.replyList.setAdapter(adapter);
        TextviewClickListener tcl = new TextviewClickListener(position);
        holder.replyText.setOnClickListener(tcl);

        return convertView;
    }

    private final class ViewHolder{
        public ImageView commentItemImg;			//评论人图片
        public TextView commentNickname;			//评论人昵称
        public TextView replyText;					//回复
        public TextView commentItemTime;			//评论时间
        public TextView commentItemContent;			//评论内容
        public NoScrollListView replyList;			//评论回复列表
    }

    /**
     * 获取回复评论
     */
    public void getReplyComment(ReplyBean bean,int position){
        List<ReplyBean> rList = list.get(position).getReplyList();
        rList.add(rList.size(), bean);
    }

    /**
     * 事件点击监听器
     */
    private final class TextviewClickListener implements OnClickListener{
        private int position;
        public TextviewClickListener(int position){
            this.position = position;
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.replyText:
                    handler.sendMessage(handler.obtainMessage(10, position));
                    break;
            }
        }
    }

}

6.CommentBean

package com.example;

import java.util.ArrayList;
import java.util.List;

public class CommentBean {

    private int id;					//评论记录ID
    private int commentImgId;		//评论人图片索引
    private String commnetAccount;	//评论人账号
    private String commentNickname;	//评论人昵称
    private String commentTime;		//评论时间
    private String commentContent;	//评论内容
    private List<ReplyBean> replyList = new ArrayList<ReplyBean>();
    //回复内容列表
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getCommentImgId() {
        return commentImgId;
    }
    public void setCommentImgId(int commentImgId) {
        this.commentImgId = commentImgId;
    }
    public String getCommnetAccount() {
        return commnetAccount;
    }
    public void setCommnetAccount(String commnetAccount) {
        this.commnetAccount = commnetAccount;
    }
    public String getCommentNickname() {
        return commentNickname;
    }
    public void setCommentNickname(String commentNickname) {
        this.commentNickname = commentNickname;
    }
    public String getCommentTime() {
        return commentTime;
    }
    public void setCommentTime(String commentTime) {
        this.commentTime = commentTime;
    }
    public String getCommentContent() {
        return commentContent;
    }
    public void setCommentContent(String commentContent) {
        this.commentContent = commentContent;
    }
    public List<ReplyBean> getReplyList() {
        return replyList;
    }
    public void setReplyList(List<ReplyBean> replyList) {
        this.replyList = replyList;
    }

}

7.NoScrollListView

package com.example;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class NoScrollListView extends ListView {
    public NoScrollListView(Context context, AttributeSet attrs) {
        super(context,attrs);
    }
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, mExpandSpec);
    }
}

8.ReplyAdapter

package com.example;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ReplyAdapter extends BaseAdapter {

    private int resourceId;
    private List<ReplyBean> list;
    private LayoutInflater inflater;
    private TextView replyContent;
    private SpannableString ss;
    private Context context;
    public ReplyAdapter(Context context,List<ReplyBean> list
            ,int resourceId){
        this.list = list;
        this.context = context;
        this.resourceId = resourceId;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ReplyBean bean = list.get(position);
        convertView = inflater.inflate(resourceId, null);
        replyContent = (TextView)
                convertView.findViewById(R.id.replyContent);

        final String replyNickName = bean.getReplyNickname();
        final String commentNickName = bean.getCommentNickname();
        String replyContentStr = bean.getReplyContent();
        //用来标识在 Span 范围内的文本前后输入新的字符时是否把它们也应用这个效果
        //Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括)
        //Spanned.SPAN_INCLUSIVE_EXCLUSIVE(前面包括,后面不包括)
        //Spanned.SPAN_EXCLUSIVE_INCLUSIVE(前面不包括,后面包括)
        //Spanned.SPAN_INCLUSIVE_INCLUSIVE(前后都包括)
        ss = new SpannableString(replyNickName+"回复"+commentNickName
                +":"+replyContentStr);
        ss.setSpan(new ForegroundColorSpan(Color.BLUE),0,
                replyNickName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(Color.BLUE),replyNickName.length()+2,
                replyNickName.length()+commentNickName.length()+2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //为回复的人昵称添加点击事件
        ss.setSpan(new TextSpanClick(true), 0,
                replyNickName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //为评论的人的添加点击事件
        ss.setSpan(new TextSpanClick(false),replyNickName.length()+2,
                replyNickName.length()+commentNickName.length()+2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        replyContent.setText(ss);
        //添加点击事件时,必须设置
        replyContent.setMovementMethod(LinkMovementMethod.getInstance());
        return convertView;
    }

    private final class TextSpanClick extends ClickableSpan{
        private boolean status;
        public TextSpanClick(boolean status){
            this.status = status;
        }
        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);//取消下划线
        }
        @Override
        public void onClick(View v) {
            String msgStr = "";
            if(status){
                msgStr = "我是回复的人";
            }else{
                msgStr = "我是评论的人";
            }
            Toast.makeText(context, msgStr, Toast.LENGTH_SHORT).show();
        }
    }

}

9.ReplyBean

package com.example;

public class ReplyBean {

    private int id;					//内容ID
    private String replyAccount;	//回复人账号
    private String replyNickname;	//回复人昵称
    private String commentAccount;	//被回复人账号
    private String commentNickname;	//被回复人昵称
    private String replyContent;	//回复的内容
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getReplyAccount() {
        return replyAccount;
    }
    public void setReplyAccount(String replyAccount) {
        this.replyAccount = replyAccount;
    }
    public String getReplyNickname() {
        return replyNickname;
    }
    public void setReplyNickname(String replyNickname) {
        this.replyNickname = replyNickname;
    }
    public String getCommentAccount() {
        return commentAccount;
    }
    public void setCommentAccount(String commentAccount) {
        this.commentAccount = commentAccount;
    }
    public String getCommentNickname() {
        return commentNickname;
    }
    public void setCommentNickname(String commentNickname) {
        this.commentNickname = commentNickname;
    }
    public String getReplyContent() {
        return replyContent;
    }
    public void setReplyContent(String replyContent) {
        this.replyContent = replyContent;
    }

}

10.strings.xml

<resources>
    <string name="app_name">My Applicationstring>
    <string name="share">分享string>
    <string name="comment">评论string>
    <string name="praise">点赞string>
    <string name="collection">收藏string>
    <string name="reply">回复string>
    <string name="leaveMessage">留言string>
    <string name="action_settings">Settingsstring>
resources>

三、资源下载地址:Android开发之评论回复功能

到这里评论回复功能就可以实现了,感兴趣的小伙伴们可以试试
不懂的地方可以问我哦

你可能感兴趣的:(android,android,studio,java)