Pinch gesture in Recycler View grid layout

Goal: Change grid layout on pinch gesture in recycler view

In this tutorial we will fetch images from gallery and show them in a grid layout in recycler view. You will be able to change layout on pinch gesture. Following are the screen shots of different layouts.

Screenshot_2016-06-29-15-39-23      Screenshot_2016-06-29-15-40-52    Screenshot_2016-06-29-15-39-01


Step 1:

Create a new project in android studio and add read external storage permission in AndroidManifest.xml

Step 2:

add recycler view in layout file for the activity

Step 3:

Add code in activity. Here we are not checking if external storage permission is given or not for Android M and above. (You can add that in the code).

  1. Create three instances of GridLayoutManagers (first with 1 column,  2nd with 2 columns and 3rd with 3 columns).
  2. Get the instance of RecyclerView in activity’s onCreate()
  3. Add GridLayoutManager with 2 columns to it (by default we are keeping two columns)
  4. Create an adapter and add it to recycler view
  5. fetch photos from gallery and add to adapter and call notifyDatasetChanged() for the adapter.

Here is the activity code:

Step 4: 

Here is Adapter’s code

Step 5:

Add Grid item layout (for adapter items)

Complete code can be found here

Author: Ankit on June 29, 2016
Category: Android

4 thoughts on “Pinch gesture in Recycler View grid layout

Your comment:

Your Name


Last articles