我需要两个布局来根据使用 ViewHolder 的列表视图项目的偶数和奇数位置进行膨胀。在偶数位置,我需要不同的布局,而在奇数位置,另一个具有相同元素但布局不同的布局。我实现了它,但是,它给了我在不同位置的随机布局,而不管它们的位置。需要做些什么来解决它。谢谢。
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;
}