我正在构建一个自定义gridview并使用picasso进行图像下载。我听说毕加索会自动缓存。但是当我滚动时,图像会被刷新。我想我犯了一个错误。请检查我的代码并帮助我找到错误。我已在下面发布了我的代码。
public class MoviesGridAdapter extends BaseAdapter{
private Context context;
private ArrayList<Movie> movies;
private LayoutInflater inflater;
private View gridView;
private final String BASE_IMAGE_URL = ""; // unable to expose the link
private Picasso mPicasso;
public MoviesGridAdapter(Context context,ArrayList<Movie> movies) {
this.context = context;
this.movies = movies;
mPicasso = Picasso.with(context);
mPicasso.setIndicatorsEnabled(true);
}
@Override
public int getCount() {
return movies.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
gridView = convertView;
ViewHolder holder;
// when the view got recycled
if(gridView == null){
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
gridView = new View(context);
holder = new ViewHolder();
gridView = inflater.inflate(R.layout.grid_inner_layout,null);
holder.movieView = (ImageView) gridView.findViewById(R.id.imageView);
gridView.setTag(holder);
} else {
// if the view still available
gridView = convertView;
holder = (ViewHolder) gridView.getTag();
}
// loading the image with picasso image loader
mPicasso.
.load(BASE_IMAGE_URL + movies.get(position).getPosterUrl())
.resize(500, 750)
.error(R.mipmap.ic_launcher)
.into(holder.movieView);
return gridView;
}
static class ViewHolder{
ImageView movieView;
}
}
gridview xml
<GridView
android:id="@+id/moviesGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:columnWidth="150dp"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
gridview项目布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="250dp"
android:id="@+id/imageView"
android:background="@android:drawable/screen_background_dark_transparent"/>
</LinearLayout>
谢谢!