我正在使用自定义ArrayAdapter实现ListFragment来填充列表。每个行项都有一个ImageView和三个TextView。正在通过XML解析数据,并且图像正在异步加载。
我遇到的问题是ListView填充并且看起来很好,但滚动时出现问题。我可以同时在屏幕上显示7个项目。当我滚动到第8个时,它会突然改变,所以下一行应该出现。它只能在可被8整除的行上(即行8,16,24等)。
我正在使用ViewHolder模式来确保使用此ListView获得良好的速度。我认为问题在于某处,但我已经四处搜索,看起来我正在正确地执行此模式,并且我已经用尽了一些东西来检查以解决此问题。我究竟做错了什么?谢谢!
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyViewHolder holder;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(R.layout.browse_result_list_item, null, false);
holder = new MyViewHolder();
holder.adTitle = (TextView) row.findViewById(R.id.adTitle);
holder.region = (TextView) row.findViewById(R.id.region);
holder.time = (TextView) row.findViewById(R.id.time);
holder.thumbnail = (ImageView) row.findViewById(R.id.browseThumbnail);
row.setTag(holder);
} else {
holder = (MyViewHolder) row.getTag();
}
SearchResult result = mObjects.get(position);
holder.adTitle.setText(result.getTitle().substring(0, result.getTitle().length()-3)); // three ... at the end, remove them
holder.region.setText(result.getRegion());
holder.time.setText(result.getPostingTime());
// Download the image thumbnail
ArrayList<String> urls = result.getImageUrls();
if (urls.size() > 0)
download(urls.get(0), holder.thumbnail);
else // No image for this post, put a placeholder
holder.thumbnail.setImageResource(R.drawable.ic_action_picture);
return row;
}
private static class MyViewHolder {
public static TextView adTitle;
public static TextView region;
public static TextView time;
public static ImageView thumbnail;
}
编辑:由于@frozenkoi,我找到了solution。结束是ViewHolder中的静态变量导致问题。它们现在只是公开的,而且这个类是静态的,问题已经解决了。
答案 0 :(得分:3)
感谢@frozenkoi,我找到了解决方案。结束是ViewHolder中的静态变量导致问题。它们现在只是公开的,而且这个类是静态的,问题已经解决了。
private static class MyViewHolder {
public TextView adTitle;
public TextView region;
public TextView time;
public ImageView thumbnail;
}
答案 1 :(得分:3)
首先将convertView=null;
方法定义为int type = getItemViewType(position)
并在此方法中评论 else 条件并添加Textview, ImageView
;。
还要从ViewHolder类中的if(convertView == null){
switch (type) {
case 0:
//First view[Row layout]
break;
case 1:
//Second view[Row layout]
break;
//another Case here....
convertView.setTag(holder);
//remove else part when used convertView =null;
/*else {
holder = (MyViewHolder) row.getTag();
}*/
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
map = list.get(position);
message_type = map.get("message_type");
if (message_type.equalsIgnoreCase("TEXT")) {
return 0;
} else {
return 1;
}
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
if (getCount() != 0)
return getCount();
return 2;
}
等每个View项中删除static关键字。
注意:覆盖你的getViewTypeCount()和getItemViewType() 适配器。
and trunc(data_elab) = TO_DATE('05/08/2015','dd/MM/yyyy');
立即行动! 完成!
答案 2 :(得分:2)
只需在适配器类中添加这些代码行即可。
@Override
public int getViewTypeCount() {
if (getCount() != 0)
return getCount();
return 1;
}
希望它能解决您的问题。
答案 3 :(得分:0)
只有在定义了视图持有者变量静态时才会出现问题。 否则listview在滚动中有或没有视图符号时效果很好。
答案 4 :(得分:0)
在我的情况下,还原在if else
情况下被忘记了:
/*public class ContactAdapter extends BaseAdapter {
public List<Contact> _data;
private ArrayList<Contact> arraylist;
Context _c;
ViewHolder holder;
public ContactAdapter(List<Contact> contacts, Context context) {
_data = contacts;
_c = context;
this.arraylist = new ArrayList<Contact>();
this.arraylist.addAll(_data);
}*/
@Override
public View getView(final int i, View convertView, final ViewGroup viewGroup) {
View view = convertView;
final Contact data = (Contact) _data.get(i);
ViewHolder holder ;
if (view == null) {
Context context = viewGroup.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.contact_list, null,false);
holder = new ViewHolder();
holder.title = (TextView) view.findViewById(R.id.name);
holder.imageView = (ImageView) view.findViewById(R.id.pic);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.title.setText(data.getName());
// Set image if is null string
if(!data.getThumb().equals("null")){
try {
Bitmap myBitmap = BitmapFactory.decodeFile(ImageStorage.getImage(data.getThumb()).getAbsolutePath());
holder.imageView.setImageBitmap(myBitmap);
}catch (Exception e){e.printStackTrace();
holder.imageView.setImageResource(R.mipmap.contactimage);
}
}else // I had forgotten this else
holder.imageView.setImageResource(R.mipmap.contactimage);
// same action with background color
if(data.getId()==data.getFromContact() ){
view.setBackgroundColor(Color.RED);
} else{
view.setBackgroundColor(Color.WHITE); //**revert back it**
}
return view;
}
ViewHolder
private static class ViewHolder {
ImageView imageView;
TextView title;
}