这次的开发中,我起初我用的是SimpleAdapter,这个适配器不用再写一个自定义的适配器类,但是在显示ImageView控件时,无法正确显示图片。解决的办法是自定义一个类来实现SimpleAdapter.ViewBinder的setViewValue方法,如下
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
/**
* Created by mql14 on 2017/12/28.
*/
public class ListviewBinderimplements SimpleAdapter.ViewBinder {
@Override
public boolean setViewValue(Viewview, Object o, String s) {//如果view是ImageView,则设置View的图片显示
if((viewinstanceof ImageView) && (oinstanceof Bitmap)){
ImageView imageView = (ImageView) view;
Bitmap bmp = (Bitmap) o;
imageView.setImageBitmap(bmp);
return true;
}
else return false;
}
}
但是由于这次开发的新闻类app中需要在ListView中实现两种不同的列表项视图,那么用SimpleAdapter就无法实现,只能自己新定义一个类来继承BaseAdapter类
package com.example.mql14.nanyouzaobao;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import java.util.Map;
/**
* Created by mql14 on 2017/12/29.
*/
public class DetailNewsAdapterextends BaseAdapter{
private List>mData;
private LayoutInflatermInflater;
public DetailNewsAdapter(Context context, List> data){
mInflater=LayoutInflater.from(context);
this.mData=data;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public ObjectgetItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2;
}//两种列表项视图
@Override
public int getItemViewType(int position) {//根据pozition值来区分不同的列表项视图,position值从“0”开始,但是如果
if(position==0)return 0; //不实现这个方法也能行
else return 1;
}
@Override
public ViewgetView(int position, View convertView, ViewGroup parent) {//第一个列表项显示新闻详细内容,其他的
news_ViewHolder holder0=null; //列表项显示评论
remark_ViewHolder holder1=null;
String str_title;
String str_date;
String str_text;
String str_name;
String str_remark;
Bitmap btp_img;
int type=getItemViewType(position);
if(convertView ==null){
switch (type){
case 0:
holder0=new news_ViewHolder();//这边必须得new一下,否则会报空指针错误
convertView=mInflater.inflate(R.layout.newsdetailcontainer,null);
holder0.news_title=(TextView)convertView.findViewById(R.id.newsdetail_title);
holder0.news_img=(ImageView)convertView.findViewById(R.id.newsdetail_img);
holder0.news_date=(TextView)convertView.findViewById(R.id.newsdetail_date);
holder0.news_text=(TextView)convertView.findViewById(R.id.newsdetail);
convertView.setTag(holder0);
break;
case 1:
holder1=new remark_ViewHolder();
convertView=mInflater.inflate(R.layout.remarkcontainer,null);
holder1.remark_img=(ImageView)convertView.findViewById(R.id.remark_img);
holder1.remark_name=(TextView)convertView.findViewById(R.id.remark_username);
holder1.remark_remark=(TextView)convertView.findViewById(R.id.remark);
holder1.remark_date=(TextView)convertView.findViewById(R.id.remark_date);
break;
}
}
else{
switch (type){
case 0:
holder0=(news_ViewHolder)convertView.getTag();
break;
case 1:
holder1=(remark_ViewHolder)convertView.getTag();
break;
}
}
switch (type){
case 0:
str_title=mData.get(position).get("title").toString();
str_date=mData.get(position).get("date").toString();
str_text=mData.get(position).get("detail").toString();
btp_img=(Bitmap)mData.get(position).get("img");
holder0.news_title.setText(str_title);
holder0.news_date.setText(str_date);
holder0.news_text.setText(str_text);
holder0.news_img.setImageBitmap(btp_img);
break;
case 1:
str_name=mData.get(position).get("name").toString();
str_date=mData.get(position).get("date").toString();
str_remark=mData.get(position).get("remark").toString();
btp_img=(Bitmap)mData.get(position).get("img");
holder1.remark_name.setText(str_name);
holder1.remark_date.setText(str_date);
holder1.remark_remark.setText(str_remark);
holder1.remark_img.setImageBitmap(btp_img);
break;
}
return convertView;
}
public List>addItem(){
return null;
}
public class news_ViewHolder{
public ImageViewnews_img;
public TextViewnews_title;
public TextViewnews_date;
public TextViewnews_text;
}
public class remark_ViewHolder{
public ImageViewremark_img;
public TextViewremark_name;
public TextViewremark_remark;
public TextViewremark_date;
}
}
ListView的绘制是一个列表项一个列表项的绘制。