android 一个ListView显示两种样式,比较简单,比如聊天的界面最常用

/** 
 * @author intbird@163.com 
 * @time 20131227 
 */ 

public class MessageAdapter extends MyBaseAdapter {

	public final int styleLeft=1;
	public final int styleRight=2;
	public final int styleNum=3;
	
	private ArrayList<HashMap<String,String>> listData;
	private LayoutInflater mInflater;
	private WindowLeaveMessage mAct;

	@SuppressWarnings("unchecked")
	public MessageAdapter(WindowLeaveMessage act, List<? extends Map<String, ?>> data,
			int resource, String[] from, int[] to) {
		super(act, data, resource, from, to);
		listData=(ArrayList<HashMap<String, String>>) data;
		mInflater=LayoutInflater.from(act);
		mAct=act;
	}
	
	@Override
	public int getItemViewType(int position) {
		HashMap<String,String> map=listData.get(position);
		String sendId=map.get("senderId");
		if(sendId.equals(mAct.midMe)){
			return styleLeft ;
		}
		else{
			return styleRight ;
		}
	}
	
	@Override
	public int getViewTypeCount() {
		return styleNum;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewChat viewChat=null;
		int style=getItemViewType(position);
		HashMap<String,String> map=listData.get(position);
		if(convertView==null){
			switch(style){
				case styleLeft:
					viewChat=new ViewChat();
					convertView=mInflater.inflate(R.layout.item_list_msg_left_item, null);
					viewChat.setView(convertView);
					convertView.setTag(viewChat);
					break;
				case styleRight:
					viewChat=new ViewChat();
					convertView=mInflater.inflate(R.layout.item_list_msg_right_item,null);
					viewChat.setView(convertView);
					convertView.setTag(viewChat);
					break;
			}
		}
		else{
			switch (style) {
			case styleLeft:case styleRight:
				viewChat=(ViewChat)convertView.getTag();
				break;
			}
		}
		switch (style) {
		case styleLeft:case styleRight:
			viewChat.setValue(map);
			break;
		}
		return convertView;
	}
	
	private class ViewChat{
		private RelativeLayout rootView;
		private TextView tvTime;
		private ProgressBar progress;
		private TextView tvContent;
		private LinearLayout linearImgs;
		public void setView(View view){
			rootView=(RelativeLayout)view.findViewById(R.id.rootView);
			tvTime=(TextView)view.findViewById(R.id.tv_time);
			tvContent=(TextView)view.findViewById(R.id.tv_content);
			linearImgs=(LinearLayout)view.findViewById(R.id.linear_imgs);
			progress=(ProgressBar)view.findViewById(R.id.msg_progress);
		}
		public void setValue(final HashMap<String, String> map) {
			//方便隐藏
			if(map.containsKey("sendok")){
				rootView.setVisibility(View.GONE);
			}else rootView.setVisibility(View.VISIBLE);
			//显示发送中
			if(map.containsKey("sending")){
				progress.setVisibility(View.VISIBLE);
			}else progress.setVisibility(View.GONE);
			//显示时间
			if(map.containsKey("time")){
				tvTime.setText(map.get("time"));
			}else tvTime.setVisibility(View.GONE);
			//显示内容
			if(map.get("content")!=null&&map.get("content").length()!=0){
				tvContent.setText(map.get("content"));
				tvContent.setVisibility(View.VISIBLE);
				//点击内容查看大内容
				tvContent.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						MessageBigText fragBigText=new MessageBigText();
						fragBigText.text=map.get("content");
						mAct.startFragment(fragBigText, true);
					}
				});
			}
			else	tvContent.setVisibility(View.GONE);
			//显示图片
			linearImgs.removeAllViews();
			if(map.containsKey("hasImage")&&map.get("hasImage").equals("1")){
				ArrayList<HashMap<String,String>> listMap=JsonHelper.getListMapByJsonArrayString(map.get("ImageInfo"));
				//有图片则创建
				for(int index=0;index<listMap.size();index++){
					final HashMap<String,String> mapData=listMap.get(index);
					//加载图片小图
					ImageView iv=new ImageView(mAct);
					setViewImage(iv, mapData.get("smallImage"));
					//点击小图看大图
					iv.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							MessageBigImage fragBigImage=new MessageBigImage();
							fragBigImage.url=mapData.get("largeImage");
							mAct.startFragment(fragBigImage, true);
						}
					});
					linearImgs.addView(iv);
				}
				linearImgs.setVisibility(View.VISIBLE);
			}
			else	linearImgs.setVisibility(View.GONE);
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值