舔狗的尊严 2016-04-28 14:26 采纳率: 100%
浏览 1636
已采纳

listview复用两个viewholder,两个viewholder的成员变量是完全不一样的

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolderTop holderTop = null;
ViewHolderNotTop holderNotTop = null;
int state = post.getData().get(position).getState();
System.out.println("state: "+state);
if (convertView == null||!flag[0]||!flag[1]) {
switch (state) {
case 0:
holderNotTop = new ViewHolderNotTop();
convertView = inflater.inflate(R.layout.listviewitem_for_post,
null);
holderNotTop.tv_post_title = (TextView) convertView
.findViewById(R.id.tv_post_title);
holderNotTop.tv_postContent = (TextView) convertView
.findViewById(R.id.tv_postContent);
holderNotTop.tv_writePostTime = (TextView) convertView
.findViewById(R.id.tv_writePostTime);
holderNotTop.postCommentNumber = (TextView) convertView
.findViewById(R.id.postCommentNumber);
holderNotTop.girlview_photo = (GridView) convertView
.findViewById(R.id.girlview_photo);
initPost(position, holderNotTop.tv_post_title,
holderNotTop.tv_postContent,
holderNotTop.tv_writePostTime,
holderNotTop.postCommentNumber,
holderNotTop.girlview_photo);
if (!flag[1]) {
convertView.setTag(R.string.notTop,holderNotTop);
flag[1] = true;
}
break;
case 1:
holderTop = new ViewHolderTop();
convertView = inflater.inflate(
R.layout.listview_item_for_topcomment, null);
holderTop.tv_postTitle = (TextView) convertView
.findViewById(R.id.tv_postTitle);
holderTop.tv_postTitle.setText(post.getData().get(position)
.getTitle());
if (!flag[0]) {
convertView.setTag(R.string.istop,holderTop);
flag[0] = true;
}
break;
}
} else {// 不用实例控件
switch (state) {
case 0:
holderNotTop =(ViewHolderNotTop) convertView.getTag(R.string.notTop);
initPost(position, holderNotTop.tv_post_title,
holderNotTop.tv_postContent,
holderNotTop.tv_writePostTime,
holderNotTop.postCommentNumber,
holderNotTop.girlview_photo);

            break;
        case 1:
            holderTop = (ViewHolderTop) convertView.getTag(R.string.istop);
            holderTop.tv_postTitle.setText(post.getData().get(position)
                    .getTitle());
            break;
        }

    }
    return convertView;
}


class ViewHolderTop {
    TextView tv_postTitle;
}

class ViewHolderNotTop {
    TextView tv_post_title;
    TextView tv_postContent;
    TextView tv_writePostTime;
    TextView postCommentNumber;
    GridView girlview_photo;
}


现在我的问题好像是它们两个前面都复用了,但是当listview的item条数超过屏幕就好像像会刷新,就找不到一个标志了()holderTop = (ViewHolderTop) convertView.getTag(R.string.istop);
            holderTop.tv_postTitle.setText(post.getData().get(position)
                    .getTitle());的holderTop是获取出问题的,报空指针了,貌似之前没刷新时没这问题,是可以正常的获取到该viewholder对象的。  在网上查找的获取是convertview.setTag(int,Object)和convertview.getTag(int),因为尝试了setTag(Object)和getTag()貌似不行!!   请大牛们指点下,在这感谢了.....
  • 写回答

3条回答 默认 最新

  • danielinbiti 2016-04-28 15:06
    关注
     如果listview中有多种布局,继承baseadapter,重写getItemViewType
     类似 http://www.tuicool.com/articles/jAFnMb
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库