问题描述
- 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()貌似不行!! 请大牛们指点下,在这感谢了.....
解决方案
如果listview中有多种布局,继承baseadapter,重写getItemViewType
类似 http://www.tuicool.com/articles/jAFnMb
解决方案二:
如果listview中有多种布局,继承baseadapter,重写getItemViewType
类似 http://www.tuicool.com/articles/jAFnMb
解决方案三:
我就按先后顺序采纳哈 谢谢大家的热心解答
确实是跟getItemViewType(int position)方法和getViewTypeCount()方法有关,应该是跟适配器里面的机制有关
时间: 2024-11-02 00:03:26