天天看点

RecyclerView实现聊天界面

RecyclerView实现聊天界面

1.聊天页面代码

class ChatActivity : BaseActivity() {

    private val chatList = ArrayList<Chat>()

    override fun initView(savedInstanceState: Bundle?) {
        setDarkStatusIcon(true)
        recycleView.layoutManager = GridLayoutManager(this, 1)
        chatList.add(Chat(0, getClickableSpan()))
        recycleView.adapter = ChatAdapter(chatList, this)
        send.setOnClickListener {
            val text = chatEditText.text.toString()
            if (TextUtils.isEmpty(text)) [email protected]
            chatList.add(Chat(1, SpannableString(text)))
            chatEditText.setText("")
            recycleView.adapter.notifyItemInserted(chatList.size - 1)
            recycleView.scrollToPosition(chatList.size - 1)
            addChat(text)
        }
    }

    override fun initData(savedInstanceState: Bundle?) {

    }

    private fun addChat(text: String) {
        when {
            TextUtils.equals("1", text) -> {
                chatList.add(Chat(0, SpannableString("技术咨询")))
            }
            TextUtils.equals("2", text) -> {
                chatList.add(Chat(0, SpannableString("业务咨询")))
            }
            else -> return
        }
        recycleView.adapter.notifyItemInserted(chatList.size - 1)
        recycleView.scrollToPosition(chatList.size - 1)
    }

    override fun getContentView(): Int {
        return R.layout.activity_chat
    }

    override fun getToolBarTitle(): String? {
        return "智能咨询"
    }

    inner class ChatAdapter(private val datas: List<Chat>, private val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

        @SuppressLint("InflateParams")
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            if (viewType == 0) {
                val view = LayoutInflater.from(context).inflate(R.layout.item_chat_1, null)
                return ChatLeftViewHolder(view, view.findViewById(R.id.text))
            } else {
                val view = LayoutInflater.from(context).inflate(R.layout.item_chat_2, null)
                return ChatRightViewHolder(view, view.findViewById(R.id.text))
            }
        }

        override fun getItemViewType(position: Int): Int {
            return datas[position].type
        }

        override fun getItemCount(): Int {
            return datas.size
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            if (holder is ChatLeftViewHolder) {
                holder.content.text = datas[position].content
                holder.content.movementMethod = LinkMovementMethod.getInstance()
            } else if (holder is ChatRightViewHolder) {
                holder.content.text = datas[position].content
            }
        }
    }

    private inner class ChatLeftViewHolder(itemView: View?, var content: TextView) : RecyclerView.ViewHolder(itemView)

    private inner class ChatRightViewHolder(itemView: View?, var content: TextView) : RecyclerView.ViewHolder(itemView)

    inner class Chat(val type: Int, val content: SpannableString)

    private fun getClickableSpan(): SpannableString {
        val spannableString = SpannableString("欢迎来到xxAPP,有什么可以帮到您的?技术咨询回复“1”,业务咨 询回复“2”,人工服务请拨打029-80010xxx")
        //设置下划线文字
        spannableString.setSpan(UnderlineSpan(), 48, 60, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        //设置文字的单击事件
        spannableString.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View?) {
                call("029-80010021")
            }
        }, 48, 60, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        //设置文字的前景色
        spannableString.setSpan(ForegroundColorSpan(Color.parseColor("#18ABFF")), 48, 60, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        return spannableString;
    }

    private fun call(phone: String) {
        val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$phone"))
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        startActivity(intent)
    }

}
           

2.布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar_include" />

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/recycleView"
        android:layout_weight="1"
        android:background="#eee"
        android:scrollbars="none" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="#f8f8f8"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/chatEditText"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginBottom="9dp"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="9dp"
            android:layout_weight="1"
            android:background="@drawable/chat_text_enter"
            android:paddingLeft="5dp"
            android:textColor="#333333"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="9dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="12dp"
            android:layout_marginTop="9dp"
            android:background="@drawable/bg_send"
            android:padding="9dp"
            android:text="发送"
            android:textColor="#ffffffff"
            android:textSize="14sp" />
    </LinearLayout>


</LinearLayout>
           

3.item1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

    <ImageView
        android:id="@+id/userPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/interactive_icon_advisory" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="9dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/triangle" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="54dp"
        android:background="@drawable/chat_text_enter"
        android:gravity="center|left"
        android:minHeight="40dp"
        android:minWidth="36dp"
        android:padding="10dp"
        android:textColor="#ff333333"
        android:textSize="14sp" />

</LinearLayout>
           

4.item2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="horizontal"
    android:padding="10dp">


    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="54dp"
        android:background="@drawable/chat_text_enter"
        android:gravity="right|center"
        android:minHeight="40dp"
        android:minWidth="36dp"
        android:padding="10dp"
        android:textColor="#ff333333"
        android:textSize="14sp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="9dp"
        android:layout_marginTop="8dp"
        android:src="@drawable/triangle_right" />

    <ImageView
        android:id="@+id/userPhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/chat_image_2" />

</LinearLayout>
           

5 .自定义对话背景 drawable chat_text_enter

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="4dp" />
    <solid android:color="#fff" />
</shape>
           

6.activity键盘设置

android:windowSoftInputMode="stateVisible|adjustResize"