Search in sources :

Example 1 with RecyclerViewPreloader

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);
}
Also used : RequestManager(com.bumptech.glide.RequestManager) RecyclerViewPreloader(com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader)

Example 2 with RecyclerViewPreloader

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);
        }
    });
}
Also used : RecyclerViewPreloader(com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader) RecyclerListener(android.support.v7.widget.RecyclerView.RecyclerListener) Drawable(android.graphics.drawable.Drawable) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ViewPreloadSizeProvider(com.bumptech.glide.util.ViewPreloadSizeProvider) ViewHolder(android.support.v7.widget.RecyclerView.ViewHolder) RecyclerView(android.support.v7.widget.RecyclerView) ImageView(android.widget.ImageView)

Example 3 with RecyclerViewPreloader

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;
}
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)

Example 4 with RecyclerViewPreloader

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;
}
Also used : RecyclerViewPreloader(com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader) Photo(com.bumptech.glide.samples.flickr.api.Photo) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ImageView(android.widget.ImageView) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) ColorDrawable(android.graphics.drawable.ColorDrawable) RecyclerView(android.support.v7.widget.RecyclerView)

Aggregations

RecyclerViewPreloader (com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader)4 RecyclerView (android.support.v7.widget.RecyclerView)3 ImageView (android.widget.ImageView)3 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)2 View (android.view.View)2 Photo (com.bumptech.glide.samples.flickr.api.Photo)2 Rect (android.graphics.Rect)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 Drawable (android.graphics.drawable.Drawable)1 Bundle (android.os.Bundle)1 GridLayoutManager (android.support.v7.widget.GridLayoutManager)1 RecyclerListener (android.support.v7.widget.RecyclerView.RecyclerListener)1 ViewHolder (android.support.v7.widget.RecyclerView.ViewHolder)1 TextView (android.widget.TextView)1 RequestManager (com.bumptech.glide.RequestManager)1 FixedPreloadSizeProvider (com.bumptech.glide.util.FixedPreloadSizeProvider)1 ViewPreloadSizeProvider (com.bumptech.glide.util.ViewPreloadSizeProvider)1