use of com.bumptech.glide.util.FixedPreloadSizeProvider in project glide by bumptech.
the class FlickrPhotoGrid method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle args = Preconditions.checkNotNull(getArguments());
photoSize = args.getInt(IMAGE_SIZE_KEY);
thumbnail = args.getBoolean(THUMBNAIL_KEY);
fullRequest = GlideApp.with(this).asDrawable().centerCrop();
thumbnailRequest = GlideApp.with(this).asDrawable().centerCrop().override(Api.SQUARE_THUMB_SIZE);
preloadRequest = thumbnail ? thumbnailRequest.clone().priority(Priority.HIGH) : fullRequest;
final View result = inflater.inflate(R.layout.flickr_photo_grid, container, false);
final int gridMargin = getResources().getDimensionPixelOffset(R.dimen.grid_margin);
int spanCount = getResources().getDisplayMetrics().widthPixels / (photoSize + (2 * gridMargin));
grid = result.findViewById(R.id.flickr_photo_grid);
layoutManager = new GridLayoutManager(getActivity(), spanCount);
grid.setLayoutManager(layoutManager);
grid.addItemDecoration(new RecyclerView.ItemDecoration() {
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(gridMargin, gridMargin, gridMargin, gridMargin);
}
});
grid.setRecyclerListener(new RecyclerView.RecyclerListener() {
@Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
PhotoViewHolder photoViewHolder = (PhotoViewHolder) holder;
GlideApp.with(FlickrPhotoGrid.this).clear(photoViewHolder.imageView);
}
});
int heightCount = getResources().getDisplayMetrics().heightPixels / photoSize;
grid.getRecycledViewPool().setMaxRecycledViews(0, spanCount * heightCount * 2);
grid.setItemViewCacheSize(0);
adapter = new PhotoAdapter();
grid.setAdapter(adapter);
FixedPreloadSizeProvider<Photo> preloadSizeProvider = new FixedPreloadSizeProvider<>(photoSize, photoSize);
RecyclerViewPreloader<Photo> preloader = new RecyclerViewPreloader<>(Glide.with(this), adapter, preloadSizeProvider, args.getInt(PRELOAD_KEY));
grid.addOnScrollListener(preloader);
if (currentPhotos != null) {
adapter.setPhotos(currentPhotos);
}
if (savedInstanceState != null) {
int index = savedInstanceState.getInt(STATE_POSITION_INDEX);
grid.scrollToPosition(index);
}
return result;
}
Aggregations