use of com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader in project glide by bumptech.
the class HorizontalGalleryFragment method onLoadFinished.
@Override
public void onLoadFinished(Loader<List<MediaStoreData>> loader, List<MediaStoreData> mediaStoreData) {
RequestManager requestManager = Glide.with(this);
RecyclerAdapter adapter = new RecyclerAdapter(getActivity(), mediaStoreData, requestManager);
RecyclerViewPreloader<MediaStoreData> preloader = new RecyclerViewPreloader<>(requestManager, adapter, adapter, 3);
recyclerView.addOnScrollListener(preloader);
recyclerView.setAdapter(adapter);
}
use of com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader in project glide by bumptech.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView giphyLogoView = findViewById(R.id.giphy_logo_view);
GlideApp.with(this).load(R.raw.large_giphy_logo).into(giphyLogoView);
RecyclerView gifList = findViewById(R.id.gif_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
gifList.setLayoutManager(layoutManager);
RequestBuilder<Drawable> gifItemRequest = GlideApp.with(this).asDrawable();
ViewPreloadSizeProvider<Api.GifResult> preloadSizeProvider = new ViewPreloadSizeProvider<>();
adapter = new GifAdapter(this, gifItemRequest, preloadSizeProvider);
gifList.setAdapter(adapter);
RecyclerViewPreloader<Api.GifResult> preloader = new RecyclerViewPreloader<>(GlideApp.with(this), adapter, preloadSizeProvider, 4);
gifList.addOnScrollListener(preloader);
gifList.setRecyclerListener(new RecyclerListener() {
@Override
public void onViewRecycled(ViewHolder holder) {
// This is an optimization to reduce the memory usage of RecyclerView's recycled view pool
// and good practice when using Glide with RecyclerView.
GifViewHolder gifViewHolder = (GifViewHolder) holder;
GlideApp.with(MainActivity.this).clear(gifViewHolder.gifView);
}
});
}
use of com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader 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;
}
use of com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader in project glide by bumptech.
the class FlickrPhotoList method onCreateView.
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View result = inflater.inflate(R.layout.flickr_photo_list, container, false);
list = result.findViewById(R.id.flickr_photo_list);
layoutManager = new LinearLayoutManager(getActivity());
list.setLayoutManager(layoutManager);
adapter = new FlickrPhotoListAdapter();
list.setAdapter(adapter);
preloadSizeProvider = new ViewPreloadSizeProvider<>();
RecyclerViewPreloader<Photo> preloader = new RecyclerViewPreloader<>(GlideApp.with(this), adapter, preloadSizeProvider, PRELOAD_AHEAD_ITEMS);
list.addOnScrollListener(preloader);
list.setItemViewCacheSize(0);
if (currentPhotos != null) {
adapter.setPhotos(currentPhotos);
}
final GlideRequests glideRequests = GlideApp.with(this);
fullRequest = glideRequests.asDrawable().centerCrop().placeholder(new ColorDrawable(Color.GRAY));
thumbRequest = glideRequests.asDrawable().diskCacheStrategy(DiskCacheStrategy.DATA).override(Api.SQUARE_THUMB_SIZE).transition(withCrossFade());
list.setRecyclerListener(new RecyclerView.RecyclerListener() {
@Override
public void onViewRecycled(RecyclerView.ViewHolder holder) {
PhotoTitleViewHolder vh = (PhotoTitleViewHolder) holder;
glideRequests.clear(vh.imageView);
}
});
if (savedInstanceState != null) {
int index = savedInstanceState.getInt(STATE_POSITION_INDEX);
int offset = savedInstanceState.getInt(STATE_POSITION_OFFSET);
layoutManager.scrollToPositionWithOffset(index, offset);
}
return result;
}
Aggregations