当前位置:首页 > 芯闻号 > 充电吧
[导读]聊天界面会展示至少两种布局,即收到消息和发送消息。这样一个ListView与平时使用的ListView的最大不同,在于它有两个不同的布局:收到的布局和发送的布局。需要利用Adapter实现这样的效果。

聊天界面会展示至少两种布局,即收到消息和发送消息。这样一个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.

ParametersThe 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

 

"


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

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


 

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");
		
		Listdata = new ArrayList();
		data.add(bean1);
		data.add(bean2);
		mListView.setAdapter(new ChatItemListViewAdapter(this,data));
	}
}



 

position
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

伴随现代科技的飞速发展,日常生活用品也在经历着翻天覆地的变化,随着消费者对口腔健康的日益重视,电动牙刷市场呈现出持续增长的态势,据统计,到2025年,全球电动牙刷市场的规模有望达到数十亿美元。然而,市场上的电动牙刷产品种...

关键字: 电动牙刷 电机

时至今日,我们越来越依赖数字数据来记录生活的点滴和工作的成果。然而,随着数据的不断累积,我们时常会遇到一个问题:网盘空间不够用。此外,网盘的安全性也是我们关注的焦点,一旦网盘服务商出现问题或数据泄露,我们的宝贵数据就可能...

关键字: 数据存储 硬盘

为抢攻AI PC商机,苹果(Apple)预计7日亮相的新iPad Pro率先搭载自研M4芯片,并挟M4芯片强势登场之势为Mac全系列改头换面,首批M4 Mac估今年底至明年初陆续上线;据悉苹果M4采台积电N3E制程,随苹...

关键字: 台积电 3nm 苹果 M4 芯片

业内消息,近日芯片巨头高通公司发布了2024财年第二财季财报,其中营收为93.89亿美元,同比增长1%;净利润为23.26亿美元,同比增长37%;不按照美国通用会计准则,调整后净利润为27.61亿美元,同比增长14%。

关键字: 高通 财报 AI

5 月 3 日,市场研究机构 Counterpoint 发布了 2024 年第一季度全球智能手机的统计数据。结果显示,2024 年第一季度全球智能手机市场出货量达到 2.969 亿部,同比增长 6%。这一增长主要归功于欧...

关键字: 智能手机

业内消息,此前官宣华为Pura 70 Ultra、华为Pura 70 Pro开启先锋计划,正式在国内开售,近日华为并将目光投向海外,5月2日华为Pura 70系列手机在马来西亚开启预售(即日起至2024年5月24日)。

关键字: 麒麟9010 华为 Pura 70

业内消息,近日两名中国公民因涉嫌向一家中国科技公司 “非法出口” 半导体切割机被起诉,该科技公司早前被美国商务部列入实体名单。

关键字: 半导体设备

近期,亿欧EqualOcean主办的商用机器人出海论坛如约而至,为行业内的企业和企业家提供了一个深入探讨和交流的平台。普渡机器人创始人兼CEO张涛应邀出席,与行业同仁共同破解出海之路的难题,并分享了普渡机器人在国际市场取...

关键字: AI 机器人

4月25日,Hi nova推出旗下最新产品Hi nova 12 SE。无论在影像能力上,还是在美学设计以及网络体验等方面,它都展现出了强大的产品力。

关键字: 手机

宇凡微公司凭借其创新的独家扫振一体电机方案,为电动牙刷行业带来了革命性的突破。该方案不仅完美集成了电机本体与扫振功能的伺服系统,实现了电动牙刷的高效、稳定和舒适的使用体验,而且还为成品制造商提供了完美的成本优化方案,降低...

关键字: 伺服电机
关闭
关闭