聊天ListView使用ViewHolder

        聊天界面会展示至少两种布局,即收到消息和发送消息。这样一个ListView与平时使用的ListView的最大不同,在于它有两个不同的布局:收到的布局和发送的布局。需要利用Adapter实现这样的效果。即需要在获取布局的时候判断该获取哪个布局。

public abstract class

BaseAdapter

extends Object
implements ListAdapter SpinnerAdapter
java.lang.Object
   ↳android.widget.BaseAdapter

BaseAdapter这个类中提供了两个方法:

public intgetItemViewType (int position)

Added in API level 1

Get the type of View that will be created by getView(int, View, ViewGroup) for the specified item.

Parameters
positionThe position of the item within the adapter's data set whose view type we want.
Returns  返回第position个Item是何种类型
  • An integer representing the type of View. Two views should share the same type if one can be converted to the other ingetView(int, View, ViewGroup). Note: Integers must be in the range 0 togetViewTypeCount() - 1. IGNORE_ITEM_VIEW_TYPE can also be returned.

 

public intgetViewTypeCount ()

Added in API level 1

Returns the number of types of Views that will be created by getView(int, View, ViewGroup). Each type represents a set of views that can be converted ingetView(int, View, ViewGroup). If the adapter always returns the same type of View for all items, this method should return 1.

This method will only be called when when the adapter is set on the the AdapterView.

Returns 返回不同布局的数目
  • The number of types of Views that will be created by this adapter

首先需要实现两个布局——发送和接收

chat_item_itemin.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal" android:gravity="center_vertical"android:padding="10dp"android:layout_height="match_parent"android:layout_width="match_parent"><ImageViewandroid:id ="@+id/icon_in"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic"/><TextViewandroid:id="@+id/text_in"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:textSize="20sp"/>"</LinearLayout>


chat_item_itemout.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:gravity="center_vertical|right"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:id="@+id/text_out"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:textSize="20sp" /><ImageViewandroid:id="@+id/icon_out"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_launcher" /></LinearLayout>


 

 

为了封装聊天内容,便于在Adapter中获取数据信息,封装一个Bean类来保存聊天信息。编写javabean就是编写一个java的类,所以只要会写类就能编写一个bean,这个类创建的一个对象称做一个bean。为了能让使用这个bean的应用程序构建工具(比如JSP引擎)知道这个bean的属性和方法,只需在类的方法命名上遵守以下规则:

1. 如果类的成员变量的名字是xxx,那么为了更改或获取成员变量的值,即更改或获取属性,在类中可以使用两个方法:

getXxx(),用来获取属性xxx。

setXxx(),用来修改属性xxx。

2. 对于boolean类型的成员变量,即布尔逻辑类型的属性,允许使用"is"代替上面的"get"和"set"。

3. 类中方法的访问属性都必须是public的。

4. 类中如果有构造方法,那么这个构造方法也是public的并且是无参数的。

 

ChatItemListViewBean.java

package sunny.example.ahfourlistviewchat;//封装一个Bean来保存聊天信息
import android.graphics.Bitmap;
public class ChatItemListViewBean {private int type;private String text;private Bitmap icon;public ChatItemListViewBean(){}public int getType(){return type;}//在使用时setType 如bean1.setType(0);public void setType(int type){this.type = type;}public String getText(){return text;}public void setText(String text){this.text = text;}public Bitmap getIcon(){return icon;}public void setIcon(Bitmap icon){this.icon = icon;}
}

activity_main.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="5dp"><ListViewandroid:id="@+id/listView_chat"android:layout_width="match_parent"android:layout_height="match_parent"android:divider="@null"android:listSelector="@android:color/transparent" /></LinearLayout>

 

ChatItemListViewTest.java

package sunny.example.ahfourlistviewchat;import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import java.util.List;
import java.util.ArrayList;
public class ChatItemListViewTest extends ActionBarActivity{private ListView mListView;protected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mListView = (ListView)findViewById(R.id.listView_chat);ChatItemListViewBean bean1 = new ChatItemListViewBean();bean1.setType(0);bean1.setIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));bean1.setText("Hello. How are you?");ChatItemListViewBean bean2 = new ChatItemListViewBean();bean2.setType(1);bean2.setIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));bean2.setText("Fine. Thank you");List<ChatItemListViewBean> data = new ArrayList<ChatItemListViewBean>();data.add(bean1);data.add(bean2);mListView.setAdapter(new ChatItemListViewAdapter(this,data));}
}



 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/348788.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

动态改变ListView布局

在getView()时&#xff0c;通过判断选择加载不同的布局。 点击某个item的时候&#xff0c;变为foucus状态&#xff0c;其他的items还原。 下面用两个方法给item设置两个不同的布局。 //foucus状态&#xff0c;显示一个ImageViewprivate View addFocusView(int i) {ImageView…

jpa 关系拥有方_JPA:确定关系的归属方

jpa 关系拥有方使用Java Persistence API&#xff08;JPA&#xff09;时&#xff0c;通常需要在两个实体之间创建关系。 这些关系是通过使用外键在数据模型&#xff08;例如数据库&#xff09;中定义的&#xff0c;而在我们的对象模型&#xff08;例如Java&#xff09;中则使用…

JDBC连接备忘单

抽象 这是常见数据库的JDBC连接的快速参考。 我似乎必须大量查找此信息&#xff0c;因此我认为最好将所有参考资料放在一个地方。 德比 <dependency><groupId>org.apache.derby</groupId><artifactId>derbyclient</artifactId><version>1…

获取View坐标

滑动一个View&#xff0c;即移动一个View&#xff0c;改变其当前所处的位置&#xff0c;通过不断改变View的坐标实现滑动的效果。 1、Android坐标系&#xff1a;坐标原点在屏幕左上角。 public void getLocationOnScreen(int[] location) Computes the coordinates of this vi…

李宏毅 课程打包_按功能而不是按层打包课程

李宏毅 课程打包大多数企业Java应用程序在设计上都有一些相似之处。 这些应用程序的打包通常由它们使用的框架&#xff08;如Spring&#xff0c;EJB或Hibernate等&#xff09;驱动。或者&#xff0c;您可以按功能对打包进行分组。 像任何其他有关建模的项目一样&#xff0c;这也…

error inflating class binaryXML LayoutParams addRule()

报出异常的原因是由于少添加了构造方法&#xff0c;三个构造方法需要写完整&#xff0c;不能只写一个。参数为(Context, AttributeSet)&#xff0c;其中第二个参数用来将xml文件中的属性初始化。 自定义控件若需要在xml文件中使用&#xff0c;就必须重写带如上两个参数的构造方…

AppD方法:Java 9支持

通过从应用程序中学习企业APM产品&#xff0c;发现更快&#xff0c;更高效的性能监控。 参加AppDynamics APM导览&#xff01; 阅读有关Java 9模块化功能带来的挑战以及AppDynamics保持在该领域的领导者的严格要求的更多信息。 我们很高兴宣布Java 17全面支持&#xff0c;这是…

Java反射,但速度更快

在编译时不知道Java类的最快方法是什么&#xff1f; Java框架通常会这样做。 很多。 它可以直接影响其性能。 因此&#xff0c;让我们对不同的方法进行基准测试&#xff0c;例如反射&#xff0c;方法句柄和代码生成。 用例 假设我们有一个简单的Person类&#xff0c;其中包含名…

Git时间

1、Git是目前世界上最先进的分布式版本控制系统。和集中式版本控制系统相比&#xff0c;分布式版本控制系统的安全性要高很多&#xff0c;因为每个人电脑里都有完整的版本库&#xff0c;某一个人的电脑坏掉了不要紧&#xff0c;随便从其他人那里复制一个就可以了。而集中式版本…

横、竖分割线

竖分割线&#xff1a; <View android:layout_width"0.5px" android:layout_height"120dp" android:background"#B8B8B8" android:visibility"visible" /> <!-- 竖直线 -->横分割线&#xff1a;<Viewandroid:layout…

java中update_Java 7 Update 21安全改进的详细信息

java中updateOracle昨天发布了三个Java更新 。 重要的是要注意它们包含一些与安全性相关的更改。 一段时间以来&#xff0c;这些变更中的大多数已经宣布&#xff0c;首先要注意的是Oracle按计划交付。 Oracle公司Java平台安全经理米尔顿史密斯&#xff08;Milton Smith&…

访问GitHub超慢的解决办法

是github某个CDN(Content Delivery Network&#xff0c;即内容分发网络?)被屏蔽所致。 附件--->记事本&#xff08;选择以管理员身份运行&#xff09;——文件——打开C:\Windows\System32\drivers\etc 右下角选择“所有文件” 选择hosts 打开 如图在这行下面添加绑定IP…

学习使用Whally GraalVM!

介绍 在Truffle在神圣的Graal中服务&#xff1a;Graal和Truffle在JVM上进行多语种语言解释的帖子中&#xff0c;我们得到了简短的介绍&#xff0c;并对Graal&#xff0c;Truffle及其周围的一些概念进行了深入研究。 但是&#xff0c;如果不深入研究实用性&#xff0c;那么任何技…

colos.xml

<?xml version"1.0" encoding"utf-8"?> <!-- /* //device/apps/common/assets/res/any/colors.xml ** ** Copyright 2006, The Android Open Source Project ** ** Licensed under the Apache License, Version 2.0 (the "License");…

Android样式开发---shape

Thanks to:转载自Keegan小钢 原文链接&#xff1a;http://keeganlee.me/post/android/20150830 一个应用&#xff0c;应该保持一套统一的样式&#xff0c;包括Button、EditText、ProgressBar、Toast、Checkbox等各种控件的样式&#xff0c;还包括控件间隔、文字大小和颜色、阴影…

主键能否@onetoone_双向@OneToOne主键关联

主键能否onetoone现在该继续有关Hibernate的文章了。 最后一个致力于单向OneToOne关联 。 因此&#xff0c;今天我将向您展示如何获取双向OneTonOne主键关联 。 本教程中基于前一篇文章的示例。 让我们开始吧。 我将使用以前创建的相同表。 为了建立双向一对一关联&#xff0c…

Android样式开发--selector

Thanks to 转载自Keegan小钢 原文链接&#xff1a;http://keeganlee.me/post/android/20150905 上一篇详细讲了shape的用法&#xff0c;讲解了怎么用shape自定义矩形、圆形、线形和环形&#xff0c;以及有哪些需要注意的地方。不过&#xff0c;shape只能定义单一的形状&#xf…

Amazon Glacier的Scala客户端

Amazon Glacier是一项安全&#xff0c;耐用且成本极低的云存储服务&#xff0c;用于数据归档和长期备份。 Glacier提供了一种冷藏数据存档解决方案&#xff0c;这意味着已存储的数据不可立即检索。 您首先需要请求检索数据&#xff0c;访问时间可能从几分钟到几小时不等&#x…

Drawable Resources

https://developer.android.com/guide/topics/resources/drawable-resource.html#Transition

SwipeRefreshLayout官方推荐下拉刷新

SwipeRefreshLayoutpublic class SwipeRefreshLayout extends ViewGroup implements NestedScrollingParent, NestedScrollingChildjava.lang.Object↳android.view.View↳android.view.ViewGroup↳ android.support.v4.widget.SwipeRefreshLayout API doc&#xff1a;http://…