Android media viewer adding RecyclerView

Introduction

The android media viewer adding RecyclerView tutorial series describes how to add the RecyclerView to the android application.

The RecyclerView will be the mechanism for laying out the thumbnails on the display. The RecyclerView will also be using a GridLayoutManager to displaying the images in a grid layout.  The MediaStoreAdapter will also have to be provided to the RecyclerView, which will be responsible for providing the thumbnails.

Once the MediaStoreAdapter has been created the cursor from the CursorLoader will be provided to it.

Get Code

The code can be found on github from the following instructions below

https://github.com/mobapptuts/media-thumbnail-viewer.git Tag

media-viewer-recyclerview

or you can run this command

git clone https://github.com/mobapptuts/media-thumbnail-viewer.git –branch

media-viewer-recyclerview

This video describes how to import the code from github using android studio and also how to use git tags

Steps

Add the RecyclerView support to the gradle build file

build.gradle

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'
}

Add a RecyclerView to the main layout file

<?xml version="1.0" encoding="utf-8"?>
<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="com.mobapptut.mediathumbviewer.MediaThumbMainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/thumbnailRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

 

Create activity members for the RecyclerView and it’s adapter

private RecyclerView mThumbnailRecyclerView;
    private MediaStoreAdapter mMediaStoreAdapter;

 In the onCreate method setup the RecyclerView, LayoutManager and adapter

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_thumb_main);

        mThumbnailRecyclerView = (RecyclerView) findViewById(R.id.thumbnailRecyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        mThumbnailRecyclerView.setLayoutManager(gridLayoutManager);
        mMediaStoreAdapter = new MediaStoreAdapter(this);
        mThumbnailRecyclerView.setAdapter(mMediaStoreAdapter);

        checkReadExternalStoragePermission();
    }

 Provide the MediaStore cursor to the adapter

@Override
   public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
       mMediaStoreAdapter.changeCursor(data);
   }

Clear the cursor in the adapter

@Override
   public void onLoaderReset(Loader<Cursor> loader) {
       mMediaStoreAdapter.changeCursor(null);
   }

 

Android media viewer adding RecyclerView summary

This concludes the initial part of the MediaStore thumbnail viewer, where the thumbnails should now be displayed in grid format.

The main focus of this tutorial was to add a RecyclerView to the main layout. Then create RecyclerView, GridLayoutManager & MediaStoreAdapter members. Add the GridLayoutManager & MediaStoreAdapter objects to the RecyclerView.

The provide the CursorLoader cursor to the Adapter.

 

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>