Android ListView 条目多样式展示实例详解
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; //ImageLoader private DisplayImageOptions Options; //定义样式常量,注意常量值要从0开始 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() { //ListView的总共条数 return newlist.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } /* * 注意我们要填充的布局个数和我们要展示的样式个数一样 * 最好一个样式一个填充布局 * * */ @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub // 获取当前条目的类型 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) { // TODO Auto-generated method stub //判断以什么结束的属性 if ( newlist.get(position).getImgs().endsWith(".jpg")) { return TYPE_LISTVIEW_NORMAL; } return TYPE_LISTVIEW_NO_PIC; } /** * 获取listView条目类别的个数(样式) */ @Override public int getViewTypeCount() { // TODO Auto-generated method stub return 2; } //容器 class ViewHolder{ ImageView imageView; TextView texttitle,textcopyright,texttime; } }
以上是多条目样式展示的一种
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
赞 (0)