ViewHolder 在偶数和奇数位置膨胀布局 [英] ViewHolder inflating layout on even and odd position
问题描述
我需要两个布局来使用 ViewHolder 根据列表视图项的偶数和奇数位置进行膨胀.在偶数位置我需要一个不同的布局,在奇数位置另一个具有相同元素但布局不同的布局.然而,我实现了它,它为我提供了不同位置的随机布局,而不管它们的位置.需要做什么来解决它.谢谢.
I need two layout to inflate on basis of even and odd position of listview items using ViewHolder. At even position I need a different layout and at Odd position another one having same elements but different layout. I implemented it, however, it gives me random layouts at different positions irrespective of their position. What needs to be done to resolve it. Thanks.
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;
}
推荐答案
问题是当视图被回收时,ListView 可以在您期望左"布局时返回右"布局.您应该覆盖 getItemViewType()
和 <适配器实现中的 href="https://developer.android.com/reference/android/widget/Adapter.html#getViewTypeCount()">getViewTypeCount()
;这些方法确保 ListView 在向 getView()
The problem is when the views are recycled, ListView can give you back a "right" layout when you expected a "left" layout. You should override getItemViewType()
and getViewTypeCount()
in your adapter implementation; these methods ensure that ListView gives you the appropriate view type when it provides a recycled view to 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;
}
这篇关于ViewHolder 在偶数和奇数位置膨胀布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!