ListView的多种样式条目展示
- 这里给大家介绍一下简单的ListView的多种样式展示
- 在布局文件中和往常一样写一个ListViwe的布局
<ListView
android:id="@+id/main_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
package com.bwei.test.BaseAdapter;
import java.util.List;
import com.bwei.test.R;
import com.bwei.test.bean.News;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.R.color;
import android.R.layout;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyBaseAdapter extends BaseAdapter {
private Context context;
private List<News> newlist;
private DisplayImageOptions Options;
private static final int TYPE_LISTVIEW_NORMAL = 0;
private static final int TYPE_LISTVIEW_NO_PIC = 1;
public MyBaseAdapter(Context context, List<News> newlist,
DisplayImageOptions options) {
super();
this.context = context;
this.newlist = newlist;
Options = options;
}
@Override
public int getCount() {
return newlist.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int itemViewType = getItemViewType(position);
ViewHolder viewHolder;
if (convertView==null) {
viewHolder=new ViewHolder();
switch (itemViewType) {
case TYPE_LISTVIEW_NORMAL:
convertView=LayoutInflater.from(context).inflate(R.layout.title_pic, null);
viewHolder.imageView=(ImageView) convertView.findViewById(R.id.imageView1);
ImageLoader.getInstance().displayImage(newlist.get(position).getImgs(), viewHolder.imageView,Options);
convertView.setTag(viewHolder);
break;
case TYPE_LISTVIEW_NO_PIC:
convertView=LayoutInflater.from(context).inflate(R.layout.title, null);
convertView.setTag(viewHolder);
break;
default:
break;
}
viewHolder.textcopyright=(TextView) convertView.findViewById(R.id.copyright);
viewHolder.texttime=(TextView) convertView.findViewById(R.id.data);
viewHolder.texttitle=(TextView) convertView.findViewById(R.id.title);
viewHolder.texttitle.setText(
newlist.get(position).getTitle());
viewHolder.texttime.setText(
newlist.get(position).getPubDate());
viewHolder.textcopyright.setText(
newlist.get(position).getCopyright());
} else {
viewHolder=(ViewHolder) convertView.getTag();
switch (itemViewType) {
case TYPE_LISTVIEW_NORMAL:
ImageLoader.getInstance().displayImage(
newlist.get(position).getImgs(),
viewHolder.imageView,Options);
break;
case TYPE_LISTVIEW_NO_PIC:
break;
default:
break;
}
viewHolder.texttitle.setText(
newlist.get(position).getTitle());
viewHolder.texttime.setText(
newlist.get(position).getPubDate());
viewHolder.textcopyright.setText(
newlist.get(position).getCopyright());
}
return convertView;
}
/**
* 获取具体的某个索引值下的条目的条目类型
*/
@Override
public int getItemViewType(int position) {
if ( newlist.get(position).getImgs().endsWith(".jpg")) {
return TYPE_LISTVIEW_NORMAL;
}
return TYPE_LISTVIEW_NO_PIC;
}
/**
* 获取listView条目类别的个数(样式)
*/
@Override
public int getViewTypeCount() {
return 2;
}
class ViewHolder{
ImageView imageView;
TextView texttitle,textcopyright,texttime;
}
}