我需要使用ViewHolder基于listview项目的偶数和奇数位置来扩展两个布局。在偶数位置,我需要不同的布局,在奇数位置,另一个具有相同的元素但布局不同。我实现了它,然而,它给了我在不同位置的随机布局,而不管它们的位置如何。需要做些什么来解决它。感谢。
public SimpleAdapter(ArrayList<WishListData> wishDataList, Context context,
ListView swipelistview) {
super(context, android.R.layout.simple_list_item_1, wishDataList);
notifyDataSetChanged();
SimpleAdapter.wishListData = wishDataList;
this.swipelistview = swipelistview;
mPreferences = new Preferences(context);
SCREEN_WIDTH = mPreferences.getScreenWidth();
SCREEN_HEIGHT = mPreferences.getScreenHeight();
mFunctions = new UserFunctions();
this.context = context;
imageloader1 = new ImageLoader1(context);
userImageLoader = new UserImageLoader(context);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
View row = convertView;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
System.out.println("position "+ position);
if ((position % 2) == 0) {
row = inflater.inflate(R.layout.single_wish_view_right, parent,
false);
} else if ((position % 2) == 1){
row = inflater
.inflate(R.layout.single_wish_view, parent, false);
}
viewHolder = new ViewHolder();
viewHolder.back = (LinearLayout) row
.findViewById(R.id.back);
viewHolder.lenear = (LinearLayout) row
.findViewById(R.id.linear);
viewHolder.front = (RelativeLayout) row
.findViewById(R.id.front);
viewHolder.likeButton = (ImageButton) row
.findViewById(R.id.likemain);
viewHolder.deathWish = (TextView) row
.findViewById(R.id.death_wish);
viewHolder.time = (TextView) row
.findViewById(R.id.time_wish);
viewHolder.name = (TextView) row.findViewById(R.id.name1);
viewHolder.commentButton = (ImageButton) row
.findViewById(R.id.comment);
viewHolder.shareButton = (ImageButton) row
.findViewById(R.id.sharemain);
viewHolder.helpButton = (ImageButton) row
.findViewById(R.id.help);
viewHolder.profilePic = (ImageView) row
.findViewById(R.id.profile_image);
viewHolder.likecount = (TextView) row
.findViewById(R.id.likecountadapter);
commentcount = (TextView) row
.findViewById(R.id.commentcountadapter);
viewHolder.tagImg = (ImageView) row
.findViewById(R.id.tag_arrow);
viewHolder.image1 = (ImageView) row
.findViewById(R.id.image1);
viewHolder.image2 = (ImageView) row
.findViewById(R.id.image2);
row.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) row.getTag();
}
mWishesData = wishListData.get(position);
viewHolder.wishlikecount = mWishesData.getDeathWishLike();
if ((position % 4) == 0) {
viewHolder.lenear.setBackgroundColor(Color.rgb(255, 168, 0));
viewHolder.back.setBackgroundResource(R.drawable.a2bg);
viewHolder.tagImg.setBackgroundResource(R.drawable.a11);
viewHolder.front.setBackgroundColor(Color.rgb(255, 168, 0));
} else if ((position % 4) == 1) {
viewHolder.lenear.setBackgroundColor(Color.rgb(253, 81, 43));
viewHolder.back.setBackgroundResource(R.drawable.a3bg);
viewHolder.tagImg.setBackgroundResource(R.drawable.a21);
viewHolder.front.setBackgroundColor(Color.rgb(253, 81, 43));
// viewHolder.Adlayout.invalidate();
// viewHolder.Adlayout.setVisibility(View.GONE);
} else if ((position % 4) == 2) {
viewHolder.lenear.setBackgroundColor(Color.rgb(155, 89, 182));
viewHolder.back.setBackgroundResource(R.drawable.a4bg);
viewHolder.tagImg.setBackgroundResource(R.drawable.a31);
viewHolder.front.setBackgroundColor(Color.rgb(155, 89, 182));
} else if ((position % 4) == 3) {
viewHolder.lenear.setBackgroundColor(Color.rgb(46, 204, 113));
viewHolder.back.setBackgroundResource(R.drawable.a1bg);
viewHolder.tagImg.setBackgroundResource(R.drawable.a41);
viewHolder.front.setBackgroundColor(Color.rgb(46, 204, 113));
}
查看持有人:
public static class ViewHolder {
public int wishlikecount;
public int wishcommentcount;
LinearLayout back, lenear;
RelativeLayout front;
TextView deathWish;
ImageButton likeButton, commentButton, shareButton, helpButton;
TextView time, name, likecount;
ImageView tagImg, image1, image2, profilePic;
}
答案 0 :(得分:6)
问题是当视图被回收时,ListView可以在您预期“左”布局时返回“正确”的布局。您应该在适配器实现中覆盖getItemViewType()
和getViewTypeCount()
;这些方法确保ListView在向getView()
@Override
public int getViewTypeCount() {
// return the total number of view types. this value should never change at runtime
return 2;
}
@Override
public int getItemViewType(int position) {
// return a value between 0 and (getViewTypeCount - 1)
return position % 2;
}
@Override
public View getView(int position, View convertView, ViewGroup container) {
int layoutResource; // determined by view type
int viewType = getItemViewType(position);
switch(viewType) {
case 0:
layoutResource = R.layout.list_item_even; break;
case 1:
layoutResource = R.layout.list_item_odd; break;
}
ViewHolder holder;
if (convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
convertView = inflater.inflate(layoutResource, container, false);
holder = new ViewHolder();
...
convertView.setTag(holder);
}
...
return convertView;
}