您好,登錄后才能下訂單哦!
ListView的多種樣式條目展示
<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; } }
以上是多條目樣式展示的一種
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。