android recyclerview模拟聊天界面
程序员文章站
2023-11-27 08:36:40
本文实例为大家分享了android recyclerview模拟聊天界面的具体代码,供大家参考,具体内容如下
效果图:
实现代码:
package com...
本文实例为大家分享了android recyclerview模拟聊天界面的具体代码,供大家参考,具体内容如下
效果图:
实现代码:
package com.itheima74.chatui; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.view.view; import android.widget.button; import android.widget.edittext; import java.util.arraylist; /** * 聊天界面,使用recyclerview实现 * 效果不好,发送的消息不能靠右对齐, * 不知何故,怎么弄都弄不好,请教! * 问题的解决:用relativelayout代替linearlayout可以解决上述问题 */ public class mainactivity extends appcompatactivity { private recyclerview recyclerview; private edittext et_input; private arraylist<msg> mmsglist; private msgadapter mmsgadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); initdata(); initadapter(); } private void initadapter() { mmsgadapter = new msgadapter(mmsglist); recyclerview.setadapter(mmsgadapter); } /** * 初始化数据源 */ private void initdata() { mmsglist = new arraylist<>(); mmsglist.add(new msg("hello!", msg.type_receive)); mmsglist.add(new msg("hello! who is that?", msg.type_send)); mmsglist.add(new msg("this is jack,nice to meet you!", msg.type_receive)); } /** * 初始化控件 */ private void initview() { recyclerview = (recyclerview) findviewbyid(r.id.recyclerview); et_input = (edittext) findviewbyid(r.id.et_input); button bt_send = (button) findviewbyid(r.id.bt_send); linearlayoutmanager layoutmanager = new linearlayoutmanager(this); layoutmanager.setorientation(linearlayoutmanager.vertical); recyclerview.setlayoutmanager(layoutmanager); bt_send.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string content = et_input.gettext().tostring().trim(); // 如果用户没有输入,则是一个空串"" if (!content.isempty()) { mmsglist.add(new msg(content, msg.type_send)); // 通知数据适配器刷新界面 mmsgadapter.notifydatasetchanged(); // 定位到最后一行 recyclerview.scrolltoposition(mmsglist.size() - 1); // 输入框置空 et_input.settext(""); } } }); } }
<?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:background="#d8e0e8" android:orientation="vertical"> <android.support.v7.widget.recyclerview android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <edittext android:id="@+id/et_input" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="请输入要发送的内容" /> <button android:id="@+id/bt_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送" /> </linearlayout> </linearlayout>
package com.itheima74.chatui; /** * created by my on 2017/3/3. */ class msg { static final int type_receive = 1; static final int type_send = 2; string content; int type; msg(string content, int type) { this.content = content; this.type = type; } }
package com.itheima74.chatui; import android.support.v7.widget.recyclerview; import android.view.view; import android.view.viewgroup; import android.widget.textview; import java.util.arraylist; /** * created by my on 2017/3/3. */ class msgadapter extends recyclerview.adapter<msgadapter.viewholder> { private arraylist<msg> mmsglist; msgadapter(arraylist<msg> mmsglist) { this.mmsglist = mmsglist; } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { view view = view.inflate(parent.getcontext(), r.layout.recyclerview_item, null); return new viewholder(view); } @override public void onbindviewholder(viewholder holder, int position) { msg msg = mmsglist.get(position); if (msg.type == msg.type_receive) { holder.tv_receive.setvisibility(view.visible); holder.tv_send.setvisibility(view.gone); holder.tv_receive.settext(msg.content); } else { holder.tv_send.setvisibility(view.visible); holder.tv_receive.setvisibility(view.gone); holder.tv_send.settext(msg.content); } } @override public int getitemcount() { return mmsglist.size(); } static class viewholder extends recyclerview.viewholder { private textview tv_receive; private textview tv_send; viewholder(view itemview) { super(itemview); tv_receive = (textview) itemview.findviewbyid(r.id.tv_receive); tv_send = (textview) itemview.findviewbyid(r.id.tv_send); } } }
xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <textview android:id="@+id/tv_receive" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/message_left" android:gravity="center" android:text="who?" android:textsize="20sp" /> <textview android:id="@+id/tv_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_below="@id/tv_receive" android:background="@drawable/message_right" android:gravity="center" android:text="i am your father" android:textsize="20sp" /> </relativelayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
android recyclerview模拟聊天界面
-
android Listview模拟聊天界面
-
Android 模拟新闻APP显示界面滑动优化实例代码
-
Android 模拟新闻APP显示界面滑动优化实例代码
-
Android利用RecyclerView编写聊天界面
-
模拟QQ聊天小项目收尾---界面展示服务端与客户端进行信息交互(用到的知识:io,线程,Swing界面,面向对象思想...... )
-
android聊天界面软键盘和工具页切换(类似微信)
-
一个简洁漂亮的聊天界面:界面编写实战-android-kotlin
-
Android ListView仿微信聊天界面
-
Android模拟实现滑动解锁界面