这两天在实现某模块的排行榜功能,看了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。
最终效果:
写的比较粗浅,欢迎评论一起探讨!
时间: 2024-10-10 23:59:19