当我滚动列表视图时,我的图像再次被加载。
这是我的适配器的getview方法。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = convertView;
// if(itemView==null){
// final int x=position;
Log.e("view Touched..","dsfgvdvgd");
LayoutInflater inflator = ((Activity) context).getLayoutInflater();
// LayoutInflater inflator = getLayoutInflater();
itemView=inflator.inflate(resId, null);
// }
// name
TextView name=(TextView) itemView.findViewById(R.id.userName);
name.setText(usersSS.get(position).getUsername());
TextView state=(TextView) itemView.findViewById(R.id.state);
state.setText(usersSS.get(position).getState());
//image
ImageView img = (ImageView)itemView.findViewById(R.id.userimage);
if(position==3){
Log.e("image urls",usersSS.get(position).getUserthumb());
}
imageLoader.DisplayImage(usersSS.get(position).getUserthumb().replace(" ","%20"), img);
// rating stars..
int[] stars = {R.id.star1,R.id.star2,R.id.star3,R.id.star4,R.id.star5};
if(!usersSS.get(position).getRating().equals(""))
{
int i=Integer.parseInt(usersSS.get(position).getRating());
for(int j=0;j<i;j++)
{
ImageView imgv =(ImageView)itemView.findViewById(stars[j]);
imgv.setBackgroundResource(R.drawable.star_on);
}
}
Log.e("view","view");
return itemView;
}
我试着保留,if(convertView == null){}然后,首先加载错误的图像,滚动后加载正确的图像。
请帮助我解决问题。
答案 0 :(得分:1)
您可以使用picasso https://github.com/square/picasso或Universal Image Loader或Volley等库来进行图像加载。如果你使用这些库会太容易了。
我正在添加您可以与picasso一起使用的示例代码段。
首先我要说的是,请在getView中使用视图重用模式以及视图。
private static final int IMAGE_HEIGHT = 100;
private static final int IMAGE_WIDTH = 100;
public static class ViewHolder {
public ImageView imageView;
public TextView title;
public TextView subTitle;
}
@Override
public Object getItem(int position) {
return usersSS.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(null == convertView) {
convertView = inflater.inflate(R.layout.row_user_data, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.userImage);
holder.title = (TextView) convertView.findViewById(R.id.userName);
holder.subTitle = (TextView) convertView.findViewById(R.id.state);
} else {
holder = (ViewHolder) convertView.getTag();
}
User user = (User) getItem(position);
holder.title.setText(user.getUserName());
holder.subTitle.setText(user.getState);
if (!TextUtils.isEmpty(user.getUserThumb())) {
Picasso.with(context).load(user.getUserThumb)
.error(R.drawable.dummy_image)
.placeholder(R.drawable.dummyImage)
.resize(IMAGE_HEIGHT, IMAGE_WIDTH).centerCrop()
.into(holder.imageView);
}
return convertView;
}