/**
* @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);
}
}
}
android 一个ListView显示两种样式,比较简单,比如聊天的界面最常用
最新推荐文章于 2021-05-25 22:49:57 发布