ViewHolder inflating layout on even and odd position

半世苍凉 提交于 2019-11-30 19:41:06

问题


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));
    }

View Holder :

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;
}

回答1:


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;
}


来源:https://stackoverflow.com/questions/26480433/viewholder-inflating-layout-on-even-and-odd-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!