聊天ListView使用ViewHolder
2016-05-25 11:24
232 查看
聊天界面会展示至少两种布局,即收到消息和发送消息。这样一个ListView与平时使用的ListView的最大不同,在于它有两个不同的布局:收到的布局和发送的布局。需要利用Adapter实现这样的效果。即需要在获取布局的时候判断该获取哪个布局。
public abstract class
implements ListAdapter
SpinnerAdapter
BaseAdapter这个类中提供了两个方法:
public intgetItemViewType
Added in API level 1
Get the type of View that will be created by
Parameters
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 in
- 1.
public intgetViewTypeCount
Added in API level 1
Returns the number of types of Views that will be created by
This method will only be called when when the adapter is set on the the
Returns 返回不同布局的数目
The number of types of Views that will be created by this adapter
首先需要实现两个布局——发送和接收
chat_item_itemin.xml
chat_item_itemout.xml
为了封装聊天内容,便于在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
activity_main.xml
ChatItemListViewTest.java
![](https://img-blog.csdn.net/20160525114420157?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
public abstract class
BaseAdapter
extends Objectimplements ListAdapter
SpinnerAdapter
java.lang.Object | |
↳ | android.widget.BaseAdapter |
public intgetItemViewType
(int position)
Added in API level 1Get the type of View that will be created by
getView(int, View, ViewGroup)for the specified item.
Parameters
position | The position of the item within the adapter's data set whose view type we want. |
---|
An integer representing the type of View. Two views should share the same type if one can be converted to the other in
getView(int, View, ViewGroup). Note: Integers must be in the range 0 to
getViewTypeCount()
- 1.
IGNORE_ITEM_VIEW_TYPEcan also be returned.
public intgetViewTypeCount
()
Added in API level 1Returns 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 in
getView(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"> <ImageView android:id ="@+id/icon_in" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic"/> <TextView android: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"> <TextView android:id="@+id/text_out" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textSize="20sp" /> <ImageView android: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"> <ListView 4000 android: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)); } }
相关文章推荐
- Android Cordova 插件开发之Cordova安装
- acm 3 1008 dp
- 【前端开发】CSS中的优先级
- 初次使用QT5串口类QSerialPort
- python网络编程socket之多进程
- Windows下C语言的Socket编程例子(TCP和UDP)
- Java并发编程:线程池的使用
- Python之HelloWorld
- Windows下编译php_scws.php
- android process communication between two application
- 第五届蓝桥杯决赛 生物芯片(完全平方数)
- 笔试面试题15--作业调度算法
- 如何让类对象只在栈(堆)上分配空间?
- Swift --> Map & FlatMap
- 笔试题65. LeetCode OJ (52)
- Python学习-机器学习实战-ch07 AdaBoost
- win7,win8怎样配置iis(图文教程)
- Alias Method解决随机类型概率问题
- Java编程中“为了性能”一些尽量做到的地方
- 解决方案 git@github.com出现Permission denied (publickey)