Android recyclerview grid gallery

android recyclerview multi-column gallery

How to create an android multi-column grid gallery.

This tutorial describes how to create an android recyclerview grid gallery by converting the original single column gallery to multiple columns.

CODE AVAILABLE ON GITHUB

You can get the code from here and then you need to Tag multi-column-gallery or else you can run this command

git clone --branch multi-column-gallery https://github.com/mobapptuts/recyclerview_image_gallery.git

CODE SAMPLES

activity_camara_intent.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CamaraIntentActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Take Photo"
        android:id="@+id/photoButton"
        android:onClick="takePhoto"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

    <android.support.v7.widget.RecyclerView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/galleryRecyclerView"
        android:layout_above="@id/photoButton"
        />


</RelativeLayout>

CamaraIntentActivity.java

private static int mColumnCount = 3;
   private static int mImageWidth;
   private static int mImageHeight;
  @Override
    protected void onCreate(Bundle savedInstanceState) {
  DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        mImageWidth = displayMetrics.widthPixels / mColumnCount;
        mImageHeight = mImageWidth * 4 / 3;

 GridLayoutManager layoutManager = new GridLayoutManager(this, mColumnCount);

 RecyclerView.Adapter imageAdapter = new ImageAdapter(mGalleryFolder,
                mImageWidth, mImageHeight);

 // final int cacheSize = maxMemorySize / 100;
        final int cacheSize = maxMemorySize / 10;

}
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
       if(requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) {

 RecyclerView.Adapter newImageAdapter = new ImageAdapter(mGalleryFolder,
                   mImageWidth, mImageHeight);

     }
   }

ImageAdapter.java

private static int mImageWidth, mImageHeight;
public ImageAdapter(File folderFile, int imageWidth, int imageHeight) {
       mImageWidth = imageWidth;
       mImageHeight = imageHeight;
       imagesFile = folderFile;
   }
@Override
   public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  ImageView imageView = new ImageView(parent.getContext());
       LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mImageWidth, mImageHeight);
       imageView.setLayoutParams(params);
       return new ViewHolder(imageView);
   }
@Override
   public void onBindViewHolder(ViewHolder holder, int position) {
       File imageFile = imagesFile.listFiles()[position];
  Bitmap bitmap = CamaraIntentActivity.getBitmapFromMemoryCache(imageFile.getName());
       if(bitmap != null) {
           holder.getImageView().setImageBitmap(bitmap);
       }
       else if(checkBitmapWorkerTask(imageFile, holder.getImageView())) {
           BitmapWorkerTask bitmapWorkerTask = new BitmapWorkerTask(holder.getImageView(),
                   mImageWidth, mImageHeight);
           AsyncDrawable asyncDrawable = new AsyncDrawable(holder.getImageView().getResources(),
                   placeHolderBitmap,
                   bitmapWorkerTask);
           holder.getImageView().setImageDrawable(asyncDrawable);
           bitmapWorkerTask.execute(imageFile);
       }
   }
public static class ViewHolder extends RecyclerView.ViewHolder {
       private ImageView imageView;

       public ViewHolder(View view) {
           super(view);

           //imageView = (ImageView) view.findViewById(R.id.imageGalleryView);
           imageView = (ImageView) view;
       }

       public ImageView getImageView() {
           return imageView;
       }
   }

BitmapWorkerTask.java

private static int TARGET_IMAGE_VIEW_WIDTH;
  private static int TARGET_IMAGE_VIEW_HEIGHT;
public BitmapWorkerTask(ImageView imageView, int imageWidth, int imageHeight) {
     TARGET_IMAGE_VIEW_WIDTH = imageWidth;
     TARGET_IMAGE_VIEW_HEIGHT = imageHeight;
     imageViewReferences = new WeakReference<ImageView>(imageView);
 }
About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>