public class MoreStyleNewsListViewAdapter extends BaseAdapter {
private Activity mActivity;
private List newsList;
private FinalBitmap imageLoader;
public MoreStyleNewsListViewAdapter(Activity mActivity,List newsList){
this .mActivity= mActivity;
this .newsList= newsList;
imageLoader = ((SunNewsApplication)mActivity.getApplication()).getFinalBitmap();
}
private final int TYPE_COUNT=2 ;
/**
* 返回数据项的显示类型数据
* 0 1 2
*/
@Override
public int getItemViewType( int position) {
// TODO Auto-generated method stub
return newsList!= null ?newsList.get(position).getStyle():-1 ;
}
/**
* 返回类型个数
*/
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return TYPE_COUNT;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
Log.d("jereh","getCount()" );
return newsList.size();
}
@Override
public Object getItem( int position) {
// TODO Auto-generated method stub
Log.d("jereh","getItem()" );
return newsList.get(position);
}
@Override
public long getItemId( int position) {
// TODO Auto-generated method stub
Log.d("jereh","getItemId()" );
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder= null;
NewsItem item = newsList.get(position);
if (convertView== null){
holder = new ViewHolder();
// 将 layout.xml 转换为 View
switch(item.getStyle()){
case 0 :
convertView =LayoutInflater.from(mActivity).inflate(R.layout.news_item1, null);
holder.ivImg1 = (ImageView)convertView.findViewById(R.id.ivNewsImg);
break;
case 1 :
convertView =LayoutInflater.from(mActivity).inflate(R.layout.news_item2, null);
holder.ivImg1 = (ImageView)convertView.findViewById(R.id.ivImg1);
holder.ivImg2 = (ImageView)convertView.findViewById(R.id.ivImg2);
holder.ivImg3 = (ImageView)convertView.findViewById(R.id.ivImg3);
break;
}
holder.tvTilte = (TextView)convertView.findViewById(R.id.tvTitle);
convertView.setTag(holder);// 记录个标识
} else{
holder = (ViewHolder)convertView.getTag();
}
// 向 ui 元素绑定数据
holder.tvTilte.setText(item.getTitle());
imageLoader.display(holder.ivImg1, item.getImgUrl()[ 0]); // 加载图片,先从缓存中加载,内存没有再从网络加载
switch(item.getStyle()){
case 1 :
imageLoader.display(holder.ivImg2, item.getImgUrl()[ 1]); // 加载图片,先从缓存中加载,内存没有再从网络加载
imageLoader.display(holder.ivImg3, item.getImgUrl()[2]); // 加载图片,先从缓存中加载,内存没有再从网络加载
break;
}
Log.d( "jereh","getView()" );
return convertView;
}
private class ViewHolder{
private TextView tvTilte;
private ImageView ivImg1;
private ImageView ivImg2;
private ImageView ivImg3;
}
}