聊天ListView
我们知道,在微信或者QQ聊天的时候,会出现至少两种布局,即收到的消息和自己发送的消息,这种效果可以用listView来实现。类似于下面这样的界面。

主要在Adapter的getView()里面下笔。
package com.example.chatting.chatting.adapter; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; import com.example.chatting.chatting.R;
import com.example.chatting.chatting.bean.Chat; import org.w3c.dom.Text; import java.util.List; /**
* Created by Administrator on 2017/11/9.
*/
public class ChattingAdapter extends BaseAdapter{ private List<Chat> list;
private LayoutInflater inflater;
private Context mContext;
// private Drawable mDefaultHeadImage; public ChattingAdapter(List<Chat> list, Context context){
this.list = list;
inflater = LayoutInflater.from(context);
mContext = context;
// mDefaultHeadImage = mContext.getResources().getDrawable(R.drawable.image_head);
} @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int position) {
return list.get(position);
} @Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return list.get(position).getType();
} //一定得重写该方法,否则只会出现一种布局
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 2;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
Chat chat = (Chat) getItem(position);
int type = getItemViewType(position);
ViewHolder viewHolder;
if(convertView == null){
if(type == 0) { // 我说的消息
System.out.println("****type0");
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_chat_out, null);
viewHolder.tvName = (TextView)convertView.findViewById(R.id.tv_name_out);
viewHolder.tvMsg = (TextView)convertView.findViewById(R.id.tv_msg_out);
viewHolder.headImage = (ImageView)convertView.findViewById(R.id.image_head_out);
}else{
System.out.println("****type1");
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_chat_in, null);
viewHolder.tvName = (TextView)convertView.findViewById(R.id.tv_name_in);
viewHolder.tvMsg = (TextView)convertView.findViewById(R.id.tv_msg_in);
viewHolder.headImage = (ImageView)convertView.findViewById(R.id.image_head_in);
}
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
} viewHolder.tvName.setText(chat.getNickName());
viewHolder.tvMsg.setText(chat.getMessage()); return convertView;
} private class ViewHolder{
public TextView tvName, tvMsg;
public ImageView headImage;
} }
通过
@Override
public int getItemViewType(int position) {
return list.get(position).getType();
} 来决定实例化哪个布局, 通过
@Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return 2;
    }
来决定布局的类型个数。
chat为封装聊天内容的java类:
package com.example.chatting.chatting.bean; import java.io.Serializable; /**
* Created by Administrator on 2017/11/9.
*/
public class Chat implements Serializable{
private String message;
private int type;
private String NickName;
private String imgURL; public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public String getNickName() {
return NickName;
} public void setNickName(String nickName) {
NickName = nickName;
} public String getImgURL() {
return imgURL;
} public void setImgURL(String imgURL) {
this.imgURL = imgURL;
}
}
接下来是两个布局的代码:
1、
list_chat_out:发送消息的布局
<?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:minHeight="80dp" > <ImageView
android:id="@+id/image_head_out"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@mipmap/me_press"
android:layout_alignParentRight="true"/> <TextView
android:id="@+id/tv_name_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/image_head_out"
android:layout_alignTop="@id/image_head_out"
android:layout_marginRight="5dp"
android:textColor="@color/colorTheme"
android:textSize="14sp"
android:text="name"/> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:layout_toLeftOf="@id/image_head_out"
android:layout_below="@id/tv_name_out"
android:layout_marginRight="10dp"
android:background="@color/colorTheme"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_msg_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textColor="@color/colorBlack"
android:textSize="18sp"
android:text="content"/>
</LinearLayout> </RelativeLayout>
2、list_chat_in:接收消息的布局
<?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:minHeight="80dp" > <ImageView
android:id="@+id/image_head_in"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@mipmap/me_press"
/> <TextView
android:id="@+id/tv_name_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image_head_in"
android:layout_alignTop="@id/image_head_in"
android:layout_marginLeft="5dp"
android:textColor="@color/colorTheme"
android:textSize="14sp"
android:text="name"/> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:layout_toRightOf="@id/image_head_in"
android:layout_below="@id/tv_name_in"
android:layout_marginLeft="10dp"
android:background="@color/colorGray"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_msg_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"
android:textColor="@color/colorBlack"
android:textSize="18sp"
android:text="content"/>
</LinearLayout> </RelativeLayout>
聊天ListView的更多相关文章
- Android群英传笔记——第四章:ListView使用技巧
		
Android群英传笔记--第四章:ListView使用技巧 最近也是比较迷茫,但是有一点点还是要坚持的,就是学习了,最近离职了,今天也是继续温习第四章ListView,也拖了其实也挺久的了,list ...
 - Android群英传知识点回顾——第四章:ListView常用优化技巧
		
4.1 ListView常用优化技巧 4.1.1 使用ViewHolder模式提高效率 4.1.2 设置项目间分割线 4.1.3 隐藏ListView的滚动条 4.1.4 取消ListView的Ite ...
 - Android群英传笔记——摘要,概述,新的出发点,温故而知新,可以为师矣!
		
Android群英传笔记--摘要,概述,新的出发点,温故而知新,可以为师矣! 当工作的越久,就越感到力不从心了,基础和理解才是最重要的,所以买了两本书,医生的<Android群英传>和主席 ...
 - 自定义一个ListView实现聊天界面
		
摘要 ListView可以称得上Android中最常用也最难用的控件了,几乎所有的应用程序都会用到它.由于手机屏幕空间都比较有限,能够一次性在屏幕上显示的内容并不多,当我们的程序中有大量的数据需要展示 ...
 - ListView数据更新后,自动滚动到底部(聊天时常用)| Listview Scroll to the end of the list after updating the list
		
转:http://www.cnblogs.com/bjshsqlt/p/3311830.html If you would like to after you have updated by list ...
 - ListView:聊天界面
		
一.最终成型图 二.主界面xml布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...
 - Android—简单的仿QQ聊天界面
		
最近仿照QQ聊天做了一个类似界面,先看下界面组成(画面不太美凑合凑合呗,,,,):
 - xamarin android ListView手动分组
		
xamarin的listview控件其实自带有分组方法,关于xamarin listview的自带分组方法请自行参考官方文档,我这里只写自己写的分组方法.xamarin自带的分组好是好,功能多,但是加 ...
 - xamarin优化listView.ScrollTo
		
在xamarinz中关于listview的滚动,我这里有点小优化,我做了一个类似QQ的聊天页面,上面是一个listview,下面时一个editText,当在手机上使用时,发现在android平台下,如 ...
 
随机推荐
- Linux的用户管理(基础篇)
			
用户相关 临时切换用户: su 用户名 完全切换用户: su – 用户名 查看当前登入的用户名: whoami 查看当前用户下的一切环境变量: env 登出当前登入的用户: logout 查看系统的用 ...
 - 内存管理-buddy[代码]
			
基于2.6.32内核源码分析 首选内存区和gfp描述符关系运算 64位系统默认没有开启CONFIG_HIGHMEM选项,因此只有4个内存区DMA(0),DMA32(1),NORMAL(2),MOVAB ...
 - tensorflow 1.0 学习:参数和特征的提取
			
在tf中,参与训练的参数可用 tf.trainable_variables()提取出来,如: #取出所有参与训练的参数 params=tf.trainable_variables() print(&q ...
 - ES6进阶之路
			
1.说出至少5个ES6的新特性,并简述它们的作用. . let关键字,用于声明只在块级作用域起作用的变量. . const关键字,用于声明一个常量. . 结构赋值,一种新的变量赋值方式.常用于交换变量 ...
 - 输入一个URL之后发生了什么?
			
简明扼要地说: DNS解析 TCP“三次握手”来建立连接 发送HTTP请求 服务器处理请求并返回HTTP报文 TCP“四次挥手”来关闭连接 客户端拿到资源并解析渲染页面
 - TensorFlow和深度学习-无需博士学位(TensorFlow and deep learning without a PhD)
			
1. 概述 原文地址: TensorFlow and deep learning,without a PhD Learn TensorFlow and deep learning, without a ...
 - 谈谈 JAVA 的对象序列化
			
所谓的『JAVA 对象序列化』就是指,将一个 JAVA 对象所描述的所有内容以文件 IO 的方式写入二进制文件的一个过程.关于序列化,主要涉及两个流,ObjectInputStream 和 Objec ...
 - python变量和变量赋值的几种形式
			
动态类型的语言 python是动态类型的语言,不需要声明变量的类型. 实际上,python中的变量仅仅只是用来保存一个数据对象的地址.无论是什么数据对象,在内存中创建好数据对象之后,都只是把它的地址保 ...
 - 华为交换机以 LACP 模式实现链路聚合
			
LACP 链路聚合模式简介 以太网链路聚合是指将多条以太网物理链路捆绑在一起成为一条逻辑链路,从而实现增加链路带宽的目的.链路聚合分为手工模式和LACP模式. LACP模式需要有链路聚合控制协议LAC ...
 - IdentityServer4 中文文档 -12- (快速入门)添加外部认证支持
			
IdentityServer4 中文文档 -12- (快速入门)添加外部认证支持 原文:http://docs.identityserver.io/en/release/quickstarts/4_e ...