Listview多条目展示
//---------------主要是适配器里面-------------------------------------
package com.bwie.test.adapter;
import java.net.ContentHandler;
import java.util.List;
import com.bwie.test.R;
import com.bwie.test.bean.News;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter{
private List<News> news;
private Context context;
//定义样式常量,注意常量值要从0开始
private static final int TYPE_LISTVIEW_NORMAL = 0;//有图片
private static final int TYPE_LISTVIEW_NO_PIC = 1;//无图片
public MyAdapter(List<News> news, Context context) {
super();
this.news = news;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return news.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return news.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
/**
* 获取listView条目类别的个数(样式)
*/
@Override
public int getViewTypeCount() {
return 2;
}
/**
* 获取具体的某个索引值下的条目的条目类型
*/
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
//判断以什么结束的属性
if ( news.get(position).getIMAGEURL().endsWith(".jpg")) {
return TYPE_LISTVIEW_NORMAL;
}
return TYPE_LISTVIEW_NO_PIC;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 获取当前条目的类型
int itemViewType = getItemViewType(position);
ViewHolder holder;
News newss=news.get(position);
if (convertView==null) {
holder=new ViewHolder();
switch (itemViewType) {
case TYPE_LISTVIEW_NORMAL:
convertView=View.inflate(context, R.layout.list_item1, null);
holder.iv=(ImageView) convertView.findViewById(R.id.iv);
convertView.setTag(holder);
break;
case TYPE_LISTVIEW_NO_PIC:
convertView=View.inflate(context, R.layout.list_item, null);
convertView.setTag(holder);
break;
default:
break;
}
holder.TITLE=(TextView) convertView.findViewById(R.id.tv_TITLE);
holder.SUBTITLE=(TextView) convertView.findViewById(R.id.tv_SUBTITLE);
holder.FROMNAME=(TextView) convertView.findViewById(R.id.tv_FROMNAME);
holder.SHOWTIME=(TextView) convertView.findViewById(R.id.tv_SHOWTIME);
}else{
holder=(ViewHolder) convertView.getTag();
switch (itemViewType) {
case TYPE_LISTVIEW_NORMAL:
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
DisplayImageOptions options=new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).build();
ImageLoader.getInstance().displayImage(newss.getIMAGEURL(), holder.iv, options);
break;
case TYPE_LISTVIEW_NO_PIC:
break;
default:
break;
}
}
holder.TITLE.setText(newss.getTITLE());
holder.SUBTITLE.setText(newss.getSUBTITLE());
holder.FROMNAME.setText(newss.getFROMNAME());
holder.SHOWTIME.setText(newss.getSHOWTIME());
return convertView;
}
static class ViewHolder{
public TextView TITLE;
public TextView SUBTITLE;
public TextView FROMNAME;
public TextView SHOWTIME;
public ImageView iv;
}
}
//--------------第一个Listview条目没有图片------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/tv_TITLE"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_SUBTITLE"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_FROMNAME"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_SHOWTIME"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</LinearLayout>
//-----------------有图片的Listview布局----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="fitXY"
android:id="@+id/iv"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:id="@+id/tv_TITLE"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_SUBTITLE"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_FROMNAME"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_SHOWTIME"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>