android Listview模拟聊天界面
程序员文章站
2023-11-27 08:52:52
本文实例为大家分享了android listview模拟聊天界面的具体代码,供大家参考,具体内容如下
代码:
package com.example.t...
本文实例为大家分享了android listview模拟聊天界面的具体代码,供大家参考,具体内容如下
代码:
package com.example.test; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.edittext; import android.widget.listview; import android.widget.textview; import android.widget.toast; import java.util.arraylist; import static com.example.test.r.id.tv_receive; import static com.example.test.r.id.tv_send; public class mainactivity extends appcompatactivity { private arraylist<msg> msgs; private edittext et_input; private myadapter myadapter; private listview lv; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); lv = (listview) findviewbyid(r.id.lv); et_input = (edittext) findviewbyid(r.id.et_input); findviewbyid(r.id.bt_send).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string content = et_input.gettext().tostring(); if (!content.isempty()) { msgs.add(new msg(content, msg.type_send)); myadapter.notifydatasetchanged(); lv.setselection(msgs.size() - 1); et_input.settext(""); } else { toast.maketext(mainactivity.this, "请输入内容!", toast.length_short).show(); } } }); msgs = new arraylist<>(); msgs.add(new msg("hello", msg.type_receive)); msgs.add(new msg("who is that?", msg.type_send)); msgs.add(new msg("this is lilei,nice to meet you!", msg.type_receive)); myadapter = new myadapter(); lv.setadapter(myadapter); } private class myadapter extends baseadapter { @override public int getcount() { return msgs.size(); } @override public msg getitem(int position) { return msgs.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { holder = new viewholder(); convertview = view.inflate(getapplicationcontext(), r.layout.listview_item, null); holder.tv_receive = (textview) convertview.findviewbyid(tv_receive); holder.tv_send = (textview) convertview.findviewbyid(tv_send); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } msg msg = getitem(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 if (msg.type == msg.type_send) { holder.tv_send.setvisibility(view.visible); holder.tv_receive.setvisibility(view.gone); holder.tv_send.settext(msg.content); } return convertview; } } private static class viewholder { textview tv_receive; textview tv_send; } }
package com.example.test; /** * 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; } }
xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ccc" android:orientation="vertical" tools:context="com.example.test.mainactivity"> <listview android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:divider="@null" /> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content"> <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>
<?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="vertical" android:padding="10dp"> <textview android:id="@+id/tv_receive" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="start" 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_gravity="end" android:background="@drawable/message_right" android:gravity="center" android:text="i am your father" android:textsize="20sp" /> </linearlayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 详解VUE前端按钮权限控制
推荐阅读
-
android recyclerview模拟聊天界面
-
android Listview模拟聊天界面
-
android 通过向viewpage中添加listview来完成滑动效果(类似于qq滑动界面)
-
Android ListView自定义Adapter实现仿QQ界面
-
Android 模拟新闻APP显示界面滑动优化实例代码
-
Android 模拟新闻APP显示界面滑动优化实例代码
-
Android 两个ListView联动,仿电影展示界面
-
Android利用RecyclerView编写聊天界面
-
模拟QQ聊天小项目收尾---界面展示服务端与客户端进行信息交互(用到的知识:io,线程,Swing界面,面向对象思想...... )
-
android聊天界面软键盘和工具页切换(类似微信)