Home | History | Annotate | Download | only in contentproviderpaging
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.contentproviderpaging;
     18 
     19 import com.bumptech.glide.Glide;
     20 
     21 import android.content.Context;
     22 import android.content.res.Resources;
     23 import android.support.v4.content.res.ResourcesCompat;
     24 import android.support.v7.widget.RecyclerView;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 
     29 import java.util.ArrayList;
     30 import java.util.List;
     31 
     32 /**
     33  * Adapter for RecyclerView, which manages the image documents.
     34  */
     35 class ImageAdapter extends RecyclerView.Adapter<ImageViewHolder> {
     36 
     37     private final Context mContext;
     38 
     39     /** Holds the information for already retrieved images. */
     40     private final List<ImageDocument> mImageDocuments = new ArrayList<>();
     41 
     42     /**
     43      * The total size of the all images. This number should be the size for all images even if
     44      * they are not fetched from the ContentProvider.
     45      */
     46     private int mTotalSize;
     47 
     48     ImageAdapter(Context context) {
     49         mContext = context;
     50     }
     51 
     52     @Override
     53     public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     54         View view = LayoutInflater.from(parent.getContext())
     55                 .inflate(R.layout.viewholder_image, parent, false);
     56         return new ImageViewHolder(view);
     57     }
     58 
     59     @Override
     60     public void onBindViewHolder(ImageViewHolder holder, int position) {
     61         Resources resources = mContext.getResources();
     62         if (mImageDocuments.size() > position) {
     63             Glide.with(mContext)
     64                     .load(mImageDocuments.get(position).mAbsolutePath)
     65                     .placeholder(R.drawable.cat_placeholder)
     66                     .into(holder.mImageView);
     67             holder.mTextView.setText(String.valueOf(position + 1));
     68         } else {
     69             holder.mImageView.setImageDrawable(
     70                     ResourcesCompat.getDrawable(resources, R.drawable.cat_placeholder, null));
     71         }
     72     }
     73 
     74     /**
     75      * Add an image as part of the adapter.
     76      *
     77      * @param imageDocument the image information to be added
     78      */
     79     void add(ImageDocument imageDocument) {
     80         mImageDocuments.add(imageDocument);
     81     }
     82 
     83     /**
     84      * Set the total size of all images.
     85      *
     86      * @param totalSize the total size
     87      */
     88     void setTotalSize(int totalSize) {
     89         mTotalSize = totalSize;
     90     }
     91 
     92     /**
     93      * @return the number of images already fetched and added to this adapter.
     94      */
     95     int getFetchedItemCount() {
     96         return mImageDocuments.size();
     97     }
     98 
     99     @Override
    100     public int getItemCount() {
    101         return mTotalSize;
    102     }
    103 
    104     /**
    105      * Represents information for an image.
    106      */
    107     static class ImageDocument {
    108 
    109         String mAbsolutePath;
    110 
    111         String mDisplayName;
    112     }
    113 }
    114