ListView:聊天界面
一.最终成型图

二.主界面xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/msgList"
android:layout_weight="1"
android:divider="#0000"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/send_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="2"
android:hint="请输入内容"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"
android:id="@+id/send"
android:onClick="btnClick"
/>
</LinearLayout>
</LinearLayout>
三.Msg类:
public class Msg {
public static final int TYPE_RECEIVED = 0;
public static final int TYPE_SEND = 1;
private String content;
private int type;
public Msg(String content, int type) {
this.content = content;
this.type = type;
}
public String getContent() {
return content;
}
public int getType() {
return type;
}
}
四.ListView 子项的布局,msg_item.xml:
<?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"
android:padding="15dp">
<LinearLayout
android:id="@+id/left_layout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="left"
android:background="@drawable/left">
<TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:background="@drawable/right">
<TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
五.ListView 的适配器类,让它继承自ArrayAdapter,并将泛型指定为Msg 类。新建类MsgAdapter,代码如下:
public class MsgAdapter extends ArrayAdapter<Msg> {
private int resourceId;
public MsgAdapter(Context context, int resource, List<Msg> objects) {
super(context, resource, objects);
resourceId = resource;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
Msg msg = getItem(position);
ViewHolder viewHolder;
if( convertView == null ){
view = LayoutInflater.from(getContext()).inflate(resourceId,null);
viewHolder = new ViewHolder();
viewHolder.left_layout = (LinearLayout)view.findViewById(R.id.left_layout);
viewHolder.right_layout = (LinearLayout)view.findViewById(R.id.right_layout);
viewHolder.left_msg = (TextView)view.findViewById(R.id.left_msg);
viewHolder.right_msg = (TextView)view.findViewById(R.id.right_msg);
view.setTag(viewHolder);
}else{
view = convertView;
viewHolder = (ViewHolder)view.getTag();
}
if( msg.getType() == Msg.TYPE_RECEIVED ){
viewHolder.left_layout.setVisibility(View.VISIBLE);
viewHolder.right_layout.setVisibility(View.GONE);
viewHolder.left_msg.setText(msg.getContent());
}else{
viewHolder.left_layout.setVisibility(View.GONE);
viewHolder.right_layout.setVisibility(View.VISIBLE);
viewHolder.right_msg.setText(msg.getContent());
}
return view;
}
class ViewHolder{
LinearLayout left_layout;
LinearLayout right_layout;
TextView left_msg;
TextView right_msg;
};
}
六.修改MainActivity 中的代码,来为ListView 初始化一些数据,并给发送按钮加入事件响应
public class MainActivity extends Activity {
private List<Msg> msgList = new ArrayList<Msg>();
private MsgAdapter msgAdapter;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
listView = (ListView)findViewById(R.id.msgList);
msgAdapter = new MsgAdapter(MainActivity.this,
R.layout.msg_item,
msgList);
listView.setAdapter(msgAdapter);
}
protected void btnClick(View view){
if( view.getId() == R.id.send ){
EditText send_msg = (EditText)findViewById(R.id.send_msg);
String message = send_msg.getText().toString().trim();
if( message.length() >0 ){
Msg msg = new Msg(message, Msg.TYPE_SEND);
msgList.add(msg);
msgAdapter.notifyDataSetChanged();
listView.setSelection(msgList.size());
send_msg.setText("");
}
}
}
private void initList(){
Msg msg;
msg = new Msg("你好啊,朋友", 0);
msgList.add(msg);
msg = new Msg("我很好", 1);
msgList.add(msg);
msg = new Msg("what are you doing?", 0);
msgList.add(msg);
}
}
ListView:聊天界面的更多相关文章
- 自定义一个ListView实现聊天界面
摘要 ListView可以称得上Android中最常用也最难用的控件了,几乎所有的应用程序都会用到它.由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示 ...
- Android—简单的仿QQ聊天界面
最近仿照QQ聊天做了一个类似界面,先看下界面组成(画面不太美凑合凑合呗,,,,):
- Android学习笔记(十二)——实战:制作一个聊天界面
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 运用简单的布局知识,我们可以来尝试制作一个聊天界面. 一.制作 Nine-Patch 图片 : Nine-Pa ...
- 自定义android精美聊天界面
编写精美聊天界面,那就肯定要有收到的消息和发送的消息. 首先还是编写主界面,修改activity_chat.xml中的代码,如下所示: <?xml version="1.0" ...
- 高仿qq聊天界面
高仿qq聊天界面,给有需要的人,界面效果如下: 真心觉得做界面非常痛苦,给有需要的朋友. chat.xml <?xml version="1.0" encoding=&quo ...
- Android开发学习之路--UI之简单聊天界面
学了很多的ui的知识,这里就来实现个聊天的界面,首先来实现个layout的xml,代码如下: <?xml version="1.0" encoding="utf-8 ...
- RecyclerView 作为聊天界面,被键盘遮挡的解决办法
最近项目在重构,使用 RecyclerView 替换了 ListView 作为 IM 的聊天界面.然后遇到了一个问题就是当键盘弹出来的时候,键盘会遮挡住 RecyclerView 的一部分,造成聊天内 ...
- RV 多样式 MultiType 聊天界面 消息类型 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- android 仿微信聊天界面,以及语音录制功能
extends:http://104zz.iteye.com/blog/1709840 本例为模仿微信聊天界面UI设计,文字发送以及语言录制UI. 1先看效果图: 第一:chat.xml设计 ...
随机推荐
- vim编辑器快捷运用
vim下可以使用常用的箭头键 但是 还有其它键可以让你更快的达到目标 hjkl 这是代替箭头键功能的 H M L 跳到屏幕的顶上 中间 下方 w 跳到下一个单词的开始e 跳到单词的结束b 向后跳 gg ...
- bottle框架学习(2):变量定义等
try: from simplejson import dumps as json_dumps, loads as json_lds except ImportError: # pragma: no ...
- MVC如何在路由器(RouteConfig)定义后缀.html
一.配置文件web.config添加一下设置 <system.webServer> <modules runAllManagedModulesForAllRequests=" ...
- js-offsetX、pageX、clientX、layerX、screenX
真心地我也是懵逼的 clientX,clientY:针对屏幕有效区域,不包括滚动部分,坐标(0,0)一直在有效区域的左上角 X,Y: 针对屏幕有效区域,不包括滚动部分,坐标(0, ...
- Netty源码学习(二)NioEventLoopGroup
0. NioEventLoopGroup简介 NioEventLoopGroup可以理解为一个线程池,内部维护了一组线程,每个线程负责处理多个Channel上的事件,而一个Channel只对应于一个线 ...
- HDU 2586.How far away ?-在线LCA(ST)-代码很认真的写了注释(捞到变形)
2018.9.10 0:40 重新敲一遍,然后很认真的写了注释,方便自己和队友看,刚过去的一天的下午打网络赛有一题用到了这个,但是没写注释,队友改板子有点伤,因为我没注释... 以后写博客,代码要写注 ...
- ZOJ18th省赛 Lucky 7
[线上网址](http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=378) BaoBao has just found ...
- Educational Codeforces Round 31 A. Book Reading【暴力】
A. Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Python的工具包[1] -> pandas数据预处理 -> pandas 库及使用总结
pandas数据预处理 / pandas data pre-processing 目录 关于 pandas pandas 库 pandas 基本操作 pandas 计算 pandas 的 Series ...
- Trapping Rain Water (Bar Height) -- LeetCode
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...