ViewHolder对偶数和奇数位置充气布局 [英] ViewHolder inflating layout on even and odd position

查看:340
本文介绍了ViewHolder对偶数和奇数位置充气布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要两个布局膨胀的偶数和奇数的使用ViewHolder列表视图项目的立场的基础。在偶数位置,我需要一个不同的布局,并在奇数位具有相同的元素,但不同的布局另一个。我实现它,但是,它给我随机布局在不同的位置,不论其地位。需要做来解决它是什么。谢谢你。

 公共SimpleAdapter(ArrayList的< WishListData> wishDataList,上下文的背景下,
        ListView的swipelistview){
    超(背景下,android.R.layout.simple_list_item_1,wishDataList);
    notifyDataSetChanged();
    SimpleAdapter.wishListData = wishDataList;
    this.swipelistview = swipelistview;    米preferences =新的preferences(背景);
    SCREEN_WIDTH = M preferences.getScreenWidth();
    SCREEN_HEIGHT = M preferences.getScreenHeight();
    mFunctions =新UserFunctions();
    this.context =背景;
    imageloader1 =新ImageLoader1(背景);
    userImageLoader =新UserImageLoader(背景);}@覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){    ViewHolder viewHolder = NULL;
    查看排= convertView;
    如果(行== NULL){
        LayoutInflater充气=((活动)上下文).getLayoutInflater();
        的System.out.println(位置+位置);
        如果((位置%2)== 0){            行= inflater.inflate(R.layout.single_wish_view_right,父母,
                    假);        }否则如果((位置%2)== 1){
            排吹气=
                    .inflate(R.layout.single_wish_view,父母,假);        }
        viewHolder =新ViewHolder();
        viewHolder.back =(LinearLayout中)一行
                .findViewById(R.id.back);
        viewHolder.lenear =(LinearLayout中)一行
                .findViewById(R.id.linear);        viewHolder.front =(RelativeLayout的)行
                .findViewById(R.id.front);
        viewHolder.likeButton =(ImageButton的)行
                .findViewById(R.id.likemain);
        viewHolder.deathWish =(TextView中)一行
                .findViewById(R.id.death_wish);
        viewHolder.time =(TextView中)一行
                .findViewById(R.id.time_wish);
        viewHolder.name =(TextView中)row.findViewById(R.id.name1);
        viewHolder.commentButton =(ImageButton的)行
                .findViewById(R.id.comment);
        viewHolder.shareButton =(ImageButton的)行
                .findViewById(R.id.sharemain);
        viewHolder.helpButton =(ImageButton的)行
                .findViewById(R.id.help);        viewHolder.profilePic =(ImageView的)行
                .findViewById(R.id.profile_image);
        viewHolder.likecount =(TextView中)一行
                .findViewById(R.id.likecountadapter);
        commentcount =(TextView中)一行
                .findViewById(R.id.commentcountadapter);
        viewHolder.tagImg =(ImageView的)行
                .findViewById(R.id.tag_arrow);
        viewHolder.image1 =(ImageView的)行
                .findViewById(R.id.image1);
        viewHolder.image2 =(ImageView的)行
                .findViewById(R.id.image2);
        row.setTag(viewHolder);
    }其他{
        viewHolder =(ViewHolder)row.getTag();
    }
    mWishesData = wishListData.get(位置);
    viewHolder.wishlikecount = mWishesData.getDeathWishLike();
    如果((位置%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));    }否则如果((位置%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);
    }否则如果((位置%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));    }否则如果((位置%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));
    }

查看持有人:

 公共静态类ViewHolder {
    公众诠释wishlikecount;
    公众诠释wishcommentcount;
    回来的LinearLayout,lenear;
    RelativeLayout的前面;
    TextView的死亡之愿;
    的ImageButton likeButton,commentButton,shareButton,helpButton;
    TextView的时间,名称,likecount;
    ImageView的tagImg,此搜索,图像2,profilePic;
}


解决方案

问题是,当意见被回收,ListView控件可以给你回来时,你预期左布局的正确的布局。你应该重写<一个href=\"https://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)\"><$c$c>getItemViewType()和<一个href=\"https://developer.android.com/reference/android/widget/Adapter.html#getViewTypeCount()\"><$c$c>getViewTypeCount()在你的适配器实现;这些方法确保ListView的为您提供了相应的视图类型时,它提供了一个循环,以 getView()

  @覆盖
公众诠释getViewTypeCount(){
    //返回视图类型的总数。这个值不应该在运行时改变
    返回2;
}@覆盖
公众诠释getItemViewType(INT位置){
    //返回0和之间的值(getViewTypeCount - 1)
    返回位置%2;
}@覆盖
公共查看getView(INT位置,查看convertView,容器的ViewGroup){
    INT layoutResource; //由视图类型确定
    INT viewType = getItemViewType(位置);
    开关(viewType){
    情况下0:
        layoutResource = R.layout.list_item_even;打破;
    情况1:
        layoutResource = R.layout.list_item_odd;打破;
    }    ViewHolder持有人;
    如果(convertView!= NULL){
        支架=(ViewHolder)convertView.getTag();
    }其他{
        convertView = inflater.inflate(layoutResource,集装箱,FALSE);
        持有人=新ViewHolder();
        ...
        convertView.setTag(保持器);
    }
    ...
    返回convertView;
}

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

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆