恒邦吧,aomox,邬一进
本文实例为大家分享了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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android studio 解决logcat无过滤工具栏的操作
Android Studio 恢复小窗口停靠模式(Docked Mode)
Android studio保存logcat日志到本地的操作
Android Studio快捷键生成TAG、Log.x日志输出介绍
网友评论