当前位置:首页 > 芯闻号 > 充电吧
[导读]聊天界面会展示至少两种布局,即收到消息和发送消息。这样一个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
本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

近日,一则有关“砺算科技濒临破产”的消息在业内不胫而走。虽然东芯股份有意收购其40%的股权,帮助砺算科技渡过难关,但这一投资事项能否最终完成仍存在不确定性。

关键字: GPU

May 14, 2024 ---- TrendForce集邦咨询研究最新显示,OLED桌上型显示器(Monitor)2024年第一季出货总量约为20万台,年成长率121%。第二季在品牌新机陆续上市后,当季成长幅度预估将达...

关键字: OLED 显示器

业内消息,近日日本软件银行集团(SoftBank Group)旗下安谋国际科技公司(Arm)计划研发人工智能(AI)芯片,先成立一个AI芯片部门,目标是明年春季建立AI芯片原型产品,然后将量产工作交由代工厂制造,预估20...

关键字: ARM AI芯片

《芯片与科学法案》(CHIPS)为美国芯片研究、开发、制造和劳动力发展提供了527亿美元的资助。

关键字: 美国芯片法案 芯片与科学法案 芯片

援引彭博社消息,近日新当选的熊本县知事木村隆(Takashi Kimura)表示,他已准备好确保获得广泛的支持,以吸引台积电在当地建立第三家日本芯片工厂。

关键字: 日本 台积电 芯片工厂

5 月 13 日消息,从“上海临港”微信公众号获悉,特斯拉上海储能超级工厂建设项目已完成施工许可证核发。这是特斯拉在美国本土以外的首个储能超级工厂项目,工厂计划于今年 5 月开工,明年一季度实现量产。

关键字: 特斯拉 储能

据消息源 jasonwill101 透露,高通公司目前正在重新设计骁龙 8 Gen 4 处理器,新的目标频率为 4.26GHz,这一变化主要是为了应对苹果 M4 / A18 / Pro 处理器。

关键字: 高通 骁龙 8 Gen 4 芯片

最新消息,今天凌晨 OpenAI 在春季更新直播官宣发布最新旗舰生成式 AI 模型 GPT-4o,GPT-4o 将 ChatGPT 变成一名带有文本、「视觉」与语音互动的实时语音助手。OpenAI 表示升级版的 Chat...

关键字: OpenAI 生成式 AI大模型 GPT-4o

三星电子最近进行了重大的组织重组,以增强其在下一代机器人业务方面的能力,并将其视为关键增长领域。作为重组的一部分,该公司解散了负责开发三星首款可穿戴机器人“Bot Fit”的机器人业务团队。

关键字: 三星电子 解散 Bot Fit 机器人

NAS这些年可吸引了不少数码发烧友的注意,但也渐渐在家庭用户中风靡。究其原因,大概还是因为太多人因为现在数据过于庞大,而一个NAS基本上就能解决一个家庭的数据存储难题。在这一背景下,铁威马F4-424 Pro凭借其出色的...

关键字: NAS 数据存储 处理器
关闭
关闭