1. 程式人生 > >android 實現listview的adapter多種佈局方式

android 實現listview的adapter多種佈局方式

這兩天在實現某模組的排行榜功能,看了UI給的效果圖和切圖,感覺有點鬱悶,因為平時使用listview時,子項都是隻有一種佈局方式,而這次卻有兩種。於是專門研究了下,發現重寫adapter的getItemViewType()和getViewTypeCount()方法就可以實現多種佈局方式,把自己的實現貼出來共享下。
步驟:
1. 重寫 getViewTypeCount() – 返回你有多少個不同的佈局;
2. 重寫 getItemViewType(int) – 由position返回view type id;
3. 根據view item的型別,在getView中建立正確的convertView。

我用的listview的adapter是:

package com.eebbk.syncenglish.view;

import java.util.List;

import com.eebbk.bbksn.BBKSn;
import com.eebbk.englishwords.bean.UserCrownRankVo;
import com.eebbk.englishwords.data.config.pojo.UserInfo;
import com.eebbk.open.common.util.ListUtils;
import com.eebbk.syncenglish.R;
import
com.eebbk.syncenglish.util.Utils; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.text.TextUtils; import
android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView.FindListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; public class UserRankListAdapter extends BaseAdapter { private static final int RANT_NUM_0 = 0; private static final int RANT_NUM_1 = 1; private static final int RANT_NUM_2 = 2; private static final int RANT_NUM_3 = 3; private static final int RANT_NUM_4 = 4; private static final int RANT_NUM_5 = 5; private static final int RANT_NUM_6 = 6; private static final int RANT_NUM_7 = 7; private static final int RANT_NUM_8 = 8; private static final int RANT_NUM_9 = 9; /** 前三名次 */ private static final int TYPE_TOP = 0; /** 其他名次 */ private static final int TYPE_OTHER = 1; private Context mContext = null; private List<UserCrownRankVo> mUserCrownRankList = null; private LayoutInflater mInflater = null; private String mMachineId = null; private DisplayImageOptions options; public void setUserCrownRankList(List<UserCrownRankVo> userCrownRankList) { this.mUserCrownRankList = userCrownRankList; notifyDataSetChanged(); } public UserRankListAdapter(Context context, List<UserCrownRankVo> userCrownRankList) { this.mContext = context; this.mUserCrownRankList = userCrownRankList; mInflater = LayoutInflater.from(mContext); mMachineId = BBKSn.getBBKSn(); options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisc(true).cacheOnDisk(true).considerExifParams(true).bitmapConfig(Bitmap.Config.ARGB_8888).showImageOnLoading(R.drawable.default_head).build(); } @Override public int getItemViewType(int position) { if (position < 3) { return TYPE_TOP; } else { return TYPE_OTHER; } } @Override public int getViewTypeCount() { return 2; } @Override public int getCount() { return ListUtils.isEmpty(mUserCrownRankList) ? 0 : mUserCrownRankList.size(); } @Override public Object getItem(int position) { return ListUtils.isEmpty(mUserCrownRankList) ? null : mUserCrownRankList.get(position); } @Override public long getItemId(int position) { return position; } @SuppressLint("InflateParams") @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; int viewType = getItemViewType(position); if (convertView == null) { holder = new ViewHolder(); if (viewType == TYPE_TOP) { convertView = mInflater.inflate(R.layout.layout_rank_list_top_item, null); } else { convertView = mInflater.inflate(R.layout.layout_rank_list_item, null); } holder.itemBgIv = (ImageView) convertView.findViewById(R.id.item_bg_id); holder.rankLayout = (RelativeLayout) convertView.findViewById(R.id.userrank_rank_layout_id); holder.nickNameTv = (TextView) convertView.findViewById(R.id.user_nickname_id); holder.beanNumTv = (TextView) convertView.findViewById(R.id.user_bean_num_id); holder.crownNumTv = (TextView) convertView.findViewById(R.id.user_crown_num_id); holder.rankIv1 = (ImageView) convertView.findViewById(R.id.rank_iv_id_1); holder.rankIv2 = (ImageView) convertView.findViewById(R.id.rank_iv_id_2); holder.headIconIv = (ImageView) convertView.findViewById(R.id.user_head_icon_id); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if (ListUtils.isEmpty(mUserCrownRankList) || position >= mUserCrownRankList.size()) { return convertView; } UserCrownRankVo userCrownRankVo = mUserCrownRankList.get(position); int rankNum = userCrownRankVo.getRanknum(); String nameStr = userCrownRankVo.getUserAlias(); if(!TextUtils.isEmpty(mMachineId) && userCrownRankVo.getMachineId().equal(mMachineId) ){ UserInfo userInfo = Utils.GetUserInfo(mContext); if( userInfo != null && !TextUtils.isEmpty(userInfo.mNickName)){ nameStr = userInfo.mNickName; } } setUserHeadIcon(holder.headIconIv,userCrownRankVo.getMachineId(),userCrownRankVo.getHeadPortrait()); holder.nickNameTv.setText(nameStr); holder.beanNumTv.setText(userCrownRankVo.getDiligenceBeanNum() + ""); holder.crownNumTv.setText(userCrownRankVo.getCrown() + ""); if(viewType == TYPE_OTHER){ holder.rankLayout.setVisibility(View.VISIBLE); setUserRankPic(holder.rankIv1,holder.rankIv2,rankNum); }else{ setItemBackground(holder.itemBgIv,rankNum); if(rankNum == 1){ holder.nickNameTv.setTextColor(0xffff05dd); holder.crownNumTv.setTextColor(0xffff05dd); }else if(rankNum == 2){ holder.nickNameTv.setTextColor(0xffa405ff); holder.crownNumTv.setTextColor(0xffa405ff); }else if(rankNum == 3){ holder.nickNameTv.setTextColor(0xff0da4fe); holder.crownNumTv.setTextColor(0xff0da4fe); } } return convertView; } private void setUserRankPic(ImageView rankNumIv1,ImageView rankNumIv2,int userRank){ if(userRank < 10){ rankNumIv2.setVisibility(View.GONE); int numPicId = getRankNumPicId(userRank); rankNumIv1.setBackgroundResource(numPicId); }else{ rankNumIv2.setVisibility(View.VISIBLE); int numPicId1 = getRankNumPicId(userRank/10); int numPicId2 = getRankNumPicId(userRank%10); rankNumIv1.setBackgroundResource(numPicId1); rankNumIv2.setBackgroundResource(numPicId2); } } private void setUserHeadIcon(ImageView headIconIv,String userId,String headIconUrl){ if(!TextUtils.isEmpty(mMachineId) && userId.equal(mMachineId)){ setMyHeadIconIv(headIconIv); }else{ setUserOtherHeadIconIv(headIconIv,headIconUrl); } } private void setUserOtherHeadIconIv(ImageView headIconIv ,String headIconUrl ){ ImageLoader.getInstance().displayImage(headIconUrl, headIconIv, options); } private void setMyHeadIconIv(ImageView headIconIv) { UserInfo userInfo = Utils.GetUserInfo(mContext); if (userInfo != null) { Bitmap headBitmap = Utils.GetUserHeadPic(userInfo); if (headBitmap != null) { headIconIv.setImageBitmap(Utils.getBitmapWithBackGround(headBitmap)); } else { headIconIv.setImageResource(R.drawable.default_head); } } else { headIconIv.setImageResource(R.drawable.default_head); } } private void setItemBackground(ImageView itemBgIv, int rank) { int rId = R.drawable.user_rank_item_no_other; switch (rank) { case 1: rId = R.drawable.user_rank_item_no_1; break; case 2: rId = R.drawable.user_rank_item_no_2; break; case 3: rId = R.drawable.user_rank_item_no_3; break; default: break; } itemBgIv.setBackgroundResource(rId); } class ViewHolder { ImageView itemBgIv; TextView nickNameTv; TextView beanNumTv; TextView crownNumTv; RelativeLayout rankLayout; ImageView rankIv1; ImageView rankIv2; ImageView headIconIv; } private int getRankNumPicId(int num) { int numPicId = 0; switch (num) { case RANT_NUM_0: numPicId = R.drawable.rank_0; break; case RANT_NUM_1: numPicId = R.drawable.rank_1; break; case RANT_NUM_2: numPicId = R.drawable.rank_2; break; case RANT_NUM_3: numPicId = R.drawable.rank_3; break; case RANT_NUM_4: numPicId = R.drawable.rank_4; break; case RANT_NUM_5: numPicId = R.drawable.rank_5; break; case RANT_NUM_6: numPicId = R.drawable.rank_6; break; case RANT_NUM_7: numPicId = R.drawable.rank_7; break; case RANT_NUM_8: numPicId = R.drawable.rank_8; break; case RANT_NUM_9: numPicId = R.drawable.rank_9; break; default: break; } return numPicId; } }

需要特別注意的是:getItemViewType()裡的type必須從0開始計數,並且讓getViewTypeCount>getItemViewType,不然會用的時候回報錯,這是我趟坑的收穫之一。一開始我寫的是,TYPE_TOP = 1 , TYPE_OTHER = 2,結果遇到陣列越界異常。於是寫成TYPE_TOP = 0 , TYPE_OTHER = 1。

最終效果:

多佈局排行榜效果

寫的比較粗淺,歡迎評論一起探討!