ListView的多種樣式條目展示
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:域名注冊、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、烏當(dāng)網(wǎng)站維護(hù)、網(wǎng)站推廣。
其他的這里就不多說了,直接介紹適配器里的操作
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 Listnewlist; //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 newlist, DisplayImageOptions options) { super(); this.context = context; this.newlist = newlist; Options = options; } @Override public int getCount() { //ListView的總共條數(shù) 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; } /* * 注意我們要填充的布局個數(shù)和我們要展示的樣式個數(shù)一樣 * 最好一個樣式一個填充布局 * * */ @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub // 獲取當(dāng)前條目的類型 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 //判斷以什么結(jié)束的屬性 if ( newlist.get(position).getImgs().endsWith(".jpg")) { return TYPE_LISTVIEW_NORMAL; } return TYPE_LISTVIEW_NO_PIC; } /** * 獲取listView條目類別的個數(shù)(樣式) */ @Override public int getViewTypeCount() { // TODO Auto-generated method stub return 2; } //容器 class ViewHolder{ ImageView imageView; TextView texttitle,textcopyright,texttime; } }
以上是多條目樣式展示的一種
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!