聊天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平台下,如 ...
随机推荐
- Scala - 快速学习03 - 基础语法
1- 变量 变量 mutable variable 在程序运行过程中其值可能发生改变的量 关键词var定义变量,定义时直接进行求值 常量 immutable variable 在程序运行过程中其值不会 ...
- 基于vue-cli3.0构建功能完善的移动端架子,主要功能包括
webpack 打包扩展 css:sass支持.normalize.css._mixin.scss._variables.scss vw.rem布局 跨域设置 eslint设置 cdn引入 路由设计. ...
- 十大经典排序算法(python实现)(原创)
个人最喜欢的排序方法是非比较类的计数排序,简单粗暴.专治花里胡哨!!! 使用场景: 1,空间复杂度 越低越好.n值较大: 堆排序 O(nlog2n) O(1) 2,无空间复杂度要求.n值较大: 桶排序 ...
- [Jenkins]Jenkins构建时提示java.io.IOException: No space left on device
突然发现Jenkins的Job全部都停了,打开Jenkins发现所有的slave机器,均提示: 点开Dead(!),提示Thread has died,如下图: 看图好像说是Jenkins所在的服务器 ...
- 小程序this.setData
data: { isChecked: [ { key: true },{ key: true },{ key: true} ]} 如上,如果我想动态修改isChecked里面指定某个下标的值怎么办? ...
- docker - 容器里安装redis
在docker中安装redis 使用命令行安装redis 下载并解压 wget http://download.redis.io/releases/redis-3.2.6.tar.gz tar -xv ...
- 通过keras例子理解LSTM 循环神经网络(RNN)
博文的翻译和实践: Understanding Stateful LSTM Recurrent Neural Networks in Python with Keras 正文 一个强大而流行的循环神经 ...
- Oracle数据库over函数的使用
转自: https://blog.csdn.net/a1065423444/article/details/75635611 over()函数写法over(partition by expr2 or ...
- 分布式系统监视zabbix讲解五之web监控--技术流ken
Web 监控 概况 你可以使用 Zabbix 检查几个网站可用性方面. 如果要使用 Web 检测功能,必须在 编译Zabbix 的时候加入 cURL(libcurl) 的支持. 要使用 Web 监控, ...
- 杭电ACM2000--ASCII码排序
ASCII码排序 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...