Search in sources :

Example 1 with FixedPreloadSizeProvider

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;
}
Also used : FixedPreloadSizeProvider(com.bumptech.glide.util.FixedPreloadSizeProvider) Rect(android.graphics.Rect) RecyclerViewPreloader(com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader) Bundle(android.os.Bundle) Photo(com.bumptech.glide.samples.flickr.api.Photo) ImageView(android.widget.ImageView) RecyclerView(android.support.v7.widget.RecyclerView) View(android.view.View) GridLayoutManager(android.support.v7.widget.GridLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView)

Aggregations

Rect (android.graphics.Rect)1 Bundle (android.os.Bundle)1 GridLayoutManager (android.support.v7.widget.GridLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 ImageView (android.widget.ImageView)1 RecyclerViewPreloader (com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader)1 Photo (com.bumptech.glide.samples.flickr.api.Photo)1 FixedPreloadSizeProvider (com.bumptech.glide.util.FixedPreloadSizeProvider)1