Android studio recyclerview gridlayoutmanager

RecyclerView using GridLayoutManager in Android With Example

RecyclerView is the improvised version of a ListView in Android. It was first introduced in Marshmallow. Recycler view in Android is the class that extends ViewGroup and implements Scrolling Interface. It can be used either in the form of ListView or in the form of Grid View.

How to use RecyclerView as GridView?

While implementing Recycler view in Android we generally have to set layout manager to display our Recycler View. There are two types of layout managers for Recycler View to implement.

  1. Linear Layout Manager: In linear layout manager, we can align our recycler view in a horizontal or vertical scrolling manner by specifying its orientation as vertical or horizontal.
  2. Grid Layout Manager: In Grid Layout manager we can align our recycler in the form of a grid. Here we have to mention the number of columns that are to be displayed in the Grid of Recycler View.

Difference Between RecyclerView and GridView

1. View Holder Implementation

In GridView it was not mandatory to use View holder implementation but in RecyclerView it is mandatory to use View Holder implementation for Recycler View. It makes the code complex but many difficulties that are faced in GridView are solved in RecyclerView.

2. Performance of Recycler View

RecyclerView is an improvised version of ListView. The performance of Recycler View has been improvised. In RecyclerView the items are ahead and behind the visible entries.

Example of GridLayoutManager in RecyclerView

A sample image is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Project just refer to this article on How to Create new Project in Android Studio and make sure that the language is Java. To implement Recycler View three sub-parts are needed which are helpful to control RecyclerView. These three subparts include:

  • Card Layout: The card layout is an XML file that will represent each individual grid item inside your Recycler view.
  • View Holder: View Holder Class is the java class that stores the reference to the UI Elements in the Card Layout and they can be modified dynamically during the execution of the program by the list of data.
  • Data Class: Data Class is an object class that holds information to be displayed in each recycler view item that is to be displayed in Recycler View.

Step 2: Add google repository in the build.gradle file of the application project.

All Jetpack components are available in the Google Maven repository, include them in the build.gradle file

Step 3: Create a Card Layout for Recycler View Card Items

Источник

Android GridLayoutManager Example

Android Tutorial

Android GridLayoutManager is the RecyclerView.LayoutManager implementation to lay out items in a grid. In this tutorial, we’ll create an application that displays CardViews inside a RecyclerView in the form of a GridLayout. Also, we’ll implement an interface that makes RecyclerView item click similar to a ListView itemClickListener .

Android GridLayoutManager

We’ve implemented a RecyclerView using a LinearLayoutManager here. Now let’s use a GridLayoutManager to layout the RecyclerView as a grid.

Following is the constructor for a GridLayoutManager.

reverseLayout if set true then layout items from end to start.

To set the span size for each item, we invoke the method setSpanSizeLookup on the GridLayoutManager

Let’s implement RecyclerView using a GridLayoutManager in a new Android Studio project.

Android GridLayoutManager Example Project Structure

The project consists of a single Activity : MainActivity.java , an adapter class : RecyclerViewAdapter.java , a DataModel.java class and a custom GridLayoutManager class AutoFitGridLayoutManager.java .

The xml layout of the MainActivity.java class is defined in the file activity_main.xml as

Note: Don’t forget to add the following dependencies for Material Design widgets and CardView in the build.gradle file.

The DataModel.java class is given below:
package com.journaldev.recyclerviewgridlayoutmanager;

The DataModel class will hold the text, drawable icon and background colour of each item cell.

The RecyclerViewAdapter.java class is given below:

In the above code we’ve defined an ItemListener interface that’ll be implemented in the MainActivity.java class.

The xml layout for each RecyclerView item is given below.
recycler_view_item.xml

The AutoFitGridLayoutManager.java class is given below:

The span count is dynamically calculated based on the orientation, width and height available.

The MainActivity.java class is given below:

  1. The above class implements the interface RecyclerViewAdapter.ItemListener and overrides the method onItemClick that’s defined in the adapter class. By doing this, we’ve implemented the RecyclerView Click Listener within our Activity instead of the Adapter class(similar to the standard onItemClickListener defined for a ListView)
  2. A DataModel class holds the details for each RecyclerView item
  3. The LayoutManager of the RecyclerView can be defined by either instantiating the AutoFitGridLayoutManager class with the column width set as 500 or by invoking the GridLayoutManager class object and setting the number of columns as 2

Let’s see the output of the application with the standard GridLayoutManager code.

As you can see, each row has two items that span the column width in both orientations.

Now comment out the code for simple GridLayoutManager and run the code for AutoFitGridLayoutManager

Читайте также:  Что такое moon reader pro для android

The output of the application in action is given below.

As you can see in the above output, when the orientation changes to landscape, each row has three items, thereby dynamically sizing the items to auto-fit the column width.

This brings an end to this tutorial. You can download the final android GridLayoutManager project from the link given below.

Источник

Tutorialwing

In this tutorial, we will talk about different android RecyclerView LayoutManagers in android application. We will see how to create different types of user interface like Simple List, GridView or Staggered Grid using various android recyclerView layoutManagers in the application.

We have already discussed about RecyclerView Widget in detail. Here, we have already covered how to use RecyclerView, how to create adapter for RecyclerView, how to customise UI for single item in RecyclerView, how to set ItemClickListener in RecyclerView etc. Moreover, we will use code of previous post into this post 😉

Let’s see a brief introduction about RecyclerView –

Android RecyclerView is more advanced, flexible and efficient version of ListView. UIs like lists and grids can be created very easily using RecyclerView. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views. Use RecyclerView when you have large data collections whose elements change at run time based on user actions like click button, or some network operations like get requests, post requests, delete requests etc.

Video Output

Source Code

1. Getting Started

Show list of animals in Linear(Vertical and Horizontal), GridView(Vertical and Horizontal) and Staggered GridView(Vertical and Horizontal). Basically, we will cover all basic android recyclerView layoutManagers used in any application.

1.1. What is LayoutManagers?

As the name suggests, it’s main work is to manage the layout for the large data-set provided by adapter. It positions each item views into it’s appropriate position in the RecycleView. Also, It re-uses the views that are no longer visible to the user. During this, It may ask the adapter to replace the contents of that view with a different element from data-set. Recycling(or Re-using) views in this manners improves performance a lot since there is no need to create extra views and perform costly operations like findViewById() etc. in it now. Due to this feature, This widget is named as RecyclerView (Means a widget that re-uses the views).

1.2 Types Of Android RecyclerView LayoutManagers.

Although you can create your own custom LayoutManagers, There are three types of built-in recyclerView LayoutManagers. They are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager.

Now, we will go through each type one by one –

Android RecyclerView LinearLayoutManager

Use this LayoutManager when you want to show item in a vertical or horizontal scrolling list.
To show vertical scrolling list

To show horizontal scrolling list

Android RecyclerView GridLayoutManager

Use this LayoutManger when you want to show item in Grid i.e. Gallery etc. You can show Grid view which scrolls either vertically or horizontally.
To show Vertically scrolling Grid View

To show Horizontally scrolling Grid View

Android RecyclerView StaggeredGridLayoutManager

Use this LayoutManager when you want to show items in Staggered Grid.
To Show Vertically Scrolling StaggeredGrid View

To Show Horizontally Scrolling StaggeredGrid View

2. Using Different Android RecyclerView LayoutManagers

To show different types of LayoutManagers, We will use Menu options (Shown at top-right corner of the screen).
At First, we sill see how to create these menus. Then, we will show different views, created using different layoutManagers, on click of these menu options.

Note: We will use code of previous post into this post. So, get the source code of previous post first.

2.1 Create Option Menus

Follow below steps to create menus.
a. Create menu folder in res/layout.
b. Then, create menu_main.xml in res/layout/menu folder.
After creating menu_main.xml file, copy/paste below code into it.

c. Copy paste below code into MainActivity.java

It will create menu in app. You will see menus if you run the app at this point.

2.2 Create Layout for Horizontal Views

To show horizontal scrollable view, create an xml file list_item_horizontal.xml in res/layout. Then, Add following code into it.

2.3 Show views using different android recyclerView LayoutManagers

Write code to show different views on menu item click. Add below code into onOptionsItemSelected method in MainActivity.java file.

Final MainActivity.java class code

Below is final MainActivity.java class code

Output

If you run the app, you will get vertical scrollable list. You can change the view type as well. You just need to select the options from menu (right-top corner of the screen). For more details, check the output shown as above.

Final Folder Structure

Tutorialwing – RecyclerView Folder structure

Tutorialwing – RecyclerView Folder structure

Note: We have used code of Android RecyclerView tutorial into this post to show example. Please go through previous post as well to understand this post clearly.

Conclusion

RecyclerView is very useful widget when you want to display large data-set. It can be used to display views like Simple List, Grid or Staggered Grid. It can be used to create horizontal as well as vertical scrollable list using different layoutManagers. Here, we have shown example for each LayoutManagers on menu item click.

Источник

RecyclerView Using GridLayoutManager With Example In Android Studio

In Android, RecyclerView is an advance and flexible version of ListView and GridView. It is a container used to display large amount of data sets that can be scrolled very efficiently by maintaining a limited number of views. RecyclerView was introduced in Material Design in API level 21 (Android 5.0 i.e Lollipop).

Material Design brings lot of new features in Android that changed a lot the visual design patterns regarding the designing of modern Android applications. This new widget is a big step for displaying data because the GridView is one of the most commonly used UI widget. In RecyclerView android provides a lot of new features which are not present in existing ListView or GridView.

Basic RecyclerView XML code:

Читайте также:  Редактор персонажа титан квест андроид

Gradle Dependency to use RecyclerView:

The RecyclerView widget is a part of separate library valid for API 7 level or higher. Add the following dependency in your Gradle build file to use recyclerview.

Table Of Contents

RecyclerView As GridView In Android:

In this article we will discuss how to use a RecyclerView As GridView. For that we need to understand LayoutManager component of RecyclerView. Layout Manager is a very new concept introduced in RecyclerView for defining the type of Layout which RecyclerView should use. It Contains the references for all the views that are filled by the data of the entry. We can create a Custom Layout Manager by extending RecyclerView.LayoutManager Class but RecyclerView Provides three types of in-built Layout Managers.

1. LinearLayoutManager: It is used for displaying Vertical or Horizontal List. To understand it please read RecyclerView as Listview
2. GridLayoutManager: It is used for displaying the items in the form of Grids.
3. StaggeredGridLayoutManager: It is used to show the items in staggered Grid.

In this article our main focus is on GridLayoutManager because it is used to display the data in the form of grids. By using this Layout Manager we can easily create grid items. A common example of grid items is our phone’s gallery in which all the images are displayed in the form of grids.

GridLayoutManager is used for displaying the data items in grid format and we can easily define the orientation for the items. In Simple words we can say that we use the GridLayoutManager for displaying RecyclerView as a GridView.

Public constructors for GridLayoutManager: Below we define the public constructor for GridLayoutManager that should be used for defining orientation(vertical or horizontal) of RecyclerView.

1- GridLayoutManager (Context context, int spanCount): It is used to create a Vertical GridLayoutManager. In this constructor first parameter is used to set the current context and second parameter is used to set the span Count means the number of columns in the grid.
Example: In below code snippet we show how to use this constructor in Android.

With Default Vertical Orientation:

With Horizontal Orientation:

2- GridLayoutManager (Context context, int spanCount, int orientation, boolean reverseLayout): In this constructor first parameter is used to set the current context, second parameter is used to set the span Count means the number of columns in the grid, third parameter is used to set the Layout Orientation should be vertical or horizontal and last param is a boolean value when sets to true layout from end to start means grids are arranged from end to start.

Example: In below code snippet we show how to use this constructor in Android.

Comparison between RecyclerView and GridView

There are a lot of new features in RecyclerView that are not present in existing GridView. The RecyclerView is more flexible, powerful and a major enhancement over GridView. I will try to give you a detailed insight into it. Below we discuss some important features of RecyclerView that should clear the reason why RecyclerView is better than GridView.

1. Custom Item Layouts: GridView can only layout the grid items in Vertical Arrangement in which we set the number of columns and rows are automatically creates according to number of items. GridView cannot be customized according to our requirements. Suppose we need to show items in horizontal arrangement in which we want to fix number of rows and columns are automatically creates according to number of items but that thing is not feasible with default GridView. But with introduction of Recyclerview we can easily create a horizontal or Vertical arrangement for grid items. By using GridLayoutManager component of RecyclerView we can easily define the orientation of grid items and spanCount used for number of columns if orientation is vertical or number of rows if orientation is horizontal.

2. Use Of ViewHolder Pattern: GridView Adapters do not require the use of ViewHolder but RecyclerView require the use of ViewHolder that is used to store the reference of View’s. In GridView it is recommended to use the ViewHolder but it is not compulsion but in RecyclerView it is mandatory to use ViewHolder that is the main difference between RecyclerView and GridView. ViewHolder is a static inner class in our Adapter which holds references to the relevant view’s. By using these references our code can avoid time consuming findViewById() method to update the widgets with new data.

3. GridView Adapters: In GridView we use many Adapter’s like ArrayAdapter for displaying simple array data, Base and SimpleAdapters for custom grids with images and text. In RecyclerView we only use RecyclerView.Adapter to setting the grid items. In Below code snippet we show how our CustomAdapter looks when we extends RecyclerView.Adapter class and use ViewHolder in it.

4. Item Animator: GridView are lacking in support of Good Animation’s. RecyclerView brings a new dimensions in it. By using RecycleView.ItemAnimator class we can easily animate the view’s.

5. Item Decoration: In GridView’s Dynamically decorating items like Adding divider or border was never easy but in RecyclerView by using RecycleView.ItemDecorator class we have a huge control on it.

Conclusion: At the end we can say that RecyclerView is much more customizable than the existing GridView and gives a lot of control and power to its developers.

Example Of RecyclerView As Vertical GridView In Android Studio:

Below is the example of RecyclerView As GridView in which we display grids of Person Names with their images with default vertical orientation by using RecyclerView. In this example we are using GridLayoutManager with vertical orientation and 2 span count value to display the items. Firstly we declare a RecyclerView in our XML file and then get the reference of it in our Activity. After that we creates two ArrayList’s for Person Names and Images. After that we set a GridLayoutManager and finally we set the Adapter to show the grid items in RecyclerView. Whenever a user clicks on an item the full size image will be displayed on the next screen.

Читайте также:  Утилита для синхронизации андроид с пк


Step 1 : Create a New Project And Name It RecyclerViewExample.

Step 2: Open Gradle Scripts > build.gradle and add RecyclerView Library dependency in it.

Step 3: Open res -> layout -> activity_main.xml (or) main.xml and add following code:
In this step we create a RecyclerView in our XML file.

Step 4: Create a new drawable XML file in Drawable folder and name it custom_item_layout.xml and add the following code in it for creating a custom grid item.
In this step we create a drawable XML file in which we add the code for creating custom grid items.

Step 5: Create a new XML file rowlayout.xml for grid item of RecyclerView and paste the following code in it.
In this step we create a new xml file for item row in which we creates a TextView and ImageView to show the data in grid format.

Step 6 : Now open app -> java -> package -> MainActivity.java and add the below code.

In this step firstly we get the reference of RecyclerView. After that we creates two ArrayList’s for Person Names and Images. After that we set a GridLayoutManager and finally we set the Adapter to show the grid items in RecyclerView.

Step 7: Create a new class CustomAdapter.java inside package and add the following code.
In this step we create a CustomAdapter class and extends RecyclerView.Adapter class with Viewholder in it. After that we implement the overrided methods and create a constructor for getting the data from Activity. In this custom Adapter two methods are more important first is onCreateViewHolder in which we inflate the layout item xml and pass it to View Holder and other is onBindViewHolder in which we set the data in the view’s with the help of ViewHolder. Finally we implement the setOnClickListener event on itemview and on click of item we display the selected image in full size in the next Activity.

Step 8: Create a new XML file activity_second.xml and add below code in it.
In this step we create a ImageView in our XML file to show the selected image in full size.

Step 9: Create a new Activity and name it SecondActivity.class and add the below code in it.
In this step we get the reference of ImageView and then get Intent which was set from adapter of Previous Activity and then finally we set the image in ImageView.

Output:

Now run the App and you will see person name which can be scrolled in vertical direction created using RecyclerView as Gridview.

Example of RecyclerView As Horizontal GridView In Android Studio

Below is the example of RecyclerView As GridView in which we display the grids of Person Names with their images with horizontal orientation by using RecyclerView. In this example we are using GridLayoutManager with horizontal orientation and 3 span count value which defines the number of rows. Firstly we declare a RecyclerView in our XML file and then get the reference of it in our Activity. After that we creates two ArrayList’s for Person Names and Images. After that we set a GridLayoutManager and finally we set the Adapter to show the grid items in RecyclerView. Whenever a user clicks on an item the full size image will be displayed on the next screen.


Step 1: Create a New Project and name it RecyclerViewExample.

Step 2: Open Gradle Scripts > build.gradle and add RecyclerView Library dependency in it.

Step 3: Open res -> layout -> activity_main.xml (or) main.xml and add following code:
In this step we create a RecyclerView in our XML file.

Step 4: Create a new drawable XML file in Drawable folder and name it custom_item_layout.xml and add the following code in it for creating a custom grid item.
In this step we create a drawable XML file in which we add the code for creating custom grid items.

Step 5: Create a new XML file rowlayout.xml for grid item of RecyclerView and paste the following code in it.
In this step we create a new xml file for item row in which we creates a TextView and ImageView to show the data in grid format.

Step 6 : Now open app -> java -> package -> MainActivity.java and add the below code.

In this step firstly we get the reference of RecyclerView. After that we creates two ArrayList’s for Person Names and Images. After that we set a GridLayoutManager with horizontal orientation and false value for reverseLayout to show the grids to show the items from start to end and then finally we set the Adapter to show the grid items in RecyclerView.

Step 7: Create a new class CustomAdapter.java inside package and add the following code.
In this step we create a CustomAdapter class and extends RecyclerView.Adapter class with ViewHolder in it. After that we implement the overrided methods and create a constructor for getting the data from Activity, in this custom Adapter two methods are more important first is onCreateViewHolder in which we inflate the layout item xml and pass it to View Holder and other is onBindViewHolder in which we set the data in the view’s with the help of ViewHolder. Finally we implement the setOnClickListener event on itemview and on click of item we display the selected image in full size in the next Activity.

Step 8: Create a new XML file activity_second.xml and add below code in it.
In this step we create a ImageView in our XML file to show the selected image in full size.

Step 9: Create a new Activity and name it SecondActivity.class and add the below code in it.
In this step we get the reference of ImageView and then get Intent which was set from adapter of Previous Activity and then finally we set the image in ImageView.

Now run the App and you will person name which can be scrolled in horizontal direction created using RecyclerView as GridView.

Источник

Оцените статью