RecyclerView inside Fragment with Android Studio

Janitra Ariena Sekarputri
Inside PPL B7
Published in
6 min readMay 11, 2020

In developing an Android application, there are two approaches. Native and Hybrid. Native applications developed for a specific platform, meanwhile hybrid applications developed for multiple platforms on a single base. In native, Android applications are written in Java-based, and iOS are written in Swift. My Software Development Project was requested by our client to use Native apps to develop our project. So, we use both Kotlin and Swift language for our project.

“The secret of getting ahead is getting started” — Mark Twain

My job during this development is developing the Android one. We use Android Studio as our IDE. It is an official IDE by Google for developing Android applications. Well, this is my last article related to my Software Development Project course. This time, I want to share my experience using Android Studio and Kotlin for the first time, especially in making a Recycler View, and to be more complicated, it is also inside a Fragment.

First thing first, please make sure you’ve already prepared this:

  1. Install the Android Studio.
  2. Install the Java Development Kit.
  3. Setup the Android Studio

Understand the Concept

Before we start, you need to know what is exactly RecyclerView, CardView, and Fragment. For a more complete explanation, please see Android Developers.

RecyclerView

Sometimes in an application, we want to display a large set of data. Here we certainly need a view that can handle it. Several different components work together to display the data. All container for the User Interface is a RecyclerView object that you add to the layout. LayoutManager such as LinearLayoutManager or GridLayourManager could be your layout manager to fill the RecyclerView.

source: Android Developers

CardView

Package view of Material Design which commonly used for wrapping data, package data in several views. This view has a drop shadow and a corner radius. This is a subclass of FrameLayout.

source: Android Developers

Fragment

A fragment represents a portion of User Interface in a FragmentActivity. Multiple fragments could be combined in a single activity and reuse a fragment in multiple activities. We implement this fragment because at the end we want to make this recycler view page could be slide to another page. Here’s the illustration of it.

source: Android Developers

Let’s jump to the implementation

Here I want to make a RecyclerView consist of CardView, which is inside a Fragment.

Prepare the Android Studio

1. Create a New Project on Android Studio. Choose Empty Activity.

2. Add your file name, then click Finish

Add the Dependencies

Adding the dependencies for RecyclerView and CardView on your build.gradle (app stage).

Then do the Sync Project with Gradle Files

Add RecyclerView to layout

Next, add RecyclerView code on your res/layout XML file. Because I want to make it inside a Fragment, which could work with FrameLayout. So, the Root Element is FrameLayout.

  1. Right-click the layout folder and go to New > Layout resource file
  2. Add your file name
  3. Select the Root Element with FrameLayout, then click OK

Create the layout with ConstraintLayout and RecyclerView.

Here’s the layout.

fragment_pertanyaan.xml

Create a CardView layout

Same as adding RecyclerView to layout, but the Root Element is CardView or androidx.cardview.widget.CardView.

I divided the row inside the CardView with LinearLayout, which is inside a RelativeLayout to makes them responsive. To insert the font and strings to the XML, don’t forget to add the resources first on the values folder. You can see the tutorial on my article about implement design guidelines here.

Here’s the layout.

card_pertanyaan.xml

Create an Adapter for the RecyclerView

On making RecylerView, there is must be an adapter to connect the RecyclerView with the CardView. The views in the list are represented by view holder objects. By extending RecyclerView.ViewHolder, these objects are instances of a class you defined. Each view holder is in charge of showing a single item with a view. This shows a list of questions, each view holder might represent a single question. The RecyclerView creates the same number of view holders as are expected to show the on-screen portion of the dynamic content. As the user scrolls through the list, the RecyclerView takes the off-screen views and rebinds them to the data which is scrolling onto the screen. The view holder objects are managed by an adapter, by extending RecyclerView.Adapter. This adapter created view holders as needed and also binds it to the data.

Here’s my model are a component that will be on the card later, such a code, category, and content. This fragment class will call the layout we’ve made before.

In the upcoming implementation, the data for the components are automatically fetched from the API. This code only shows if the data are input manually from the array.

For the simple implementation, create ViewHolder as an inner class on the RecyclerAdapter class.

DetailPertanyaan is an activity if the card has been clicked. For the supporting method in RecyclerAdapter class, add this code below the ViewHolder class.

onCreateViewHolder() needs to construct a RecyclerView.ViewHolder and set the view to display its contents. It would set the view by inflating the CardView layout.

onBindViewHolder() uses the view holder’s position to determine what the contents should be.

Set the Adapter to RecyclerView on Fragment class

This class will call the layout that consists of the RecyclerView and connect the adapter with the RecyclerView.

onViewCreated() will set the RecyclerAdapter to the RecyclerView layout.

Last but not least, don’t forget to Run your ‘app’ to see the result!

Voila!

I will not explain more about the further Fragment implementation, you can use TabLayout as the sections pager adapter and ViewPager to make the page could be slide to the next fragment. This tutorial I made only for one fragment. There are so many inspirations and tutorials out there to explore more about this layout. This card also could be clicked and go to another activity, and the tutorials are also available on the internet. Hehe

Closing Statement

On developing my project with Android Kotlin, there are so many lessons that I learned. This is not the only task that I do, there is also another task. I learned a lot about this new framework, I use Android Studio and Kotlin for the first time. I learned it from the basics. Before we start the project, our client gave us a free course in Udemy on Android Fundamentals. You also can learn it here.

When I first succeed in making the sign-in layout, I am feeling very very joyful! My layout for the sign-in page was made on the Design option. It is very easy because you will feel like design a mockup, just drag and choose the details on the Attributes. As time goes by, I easily understand how to make the UI for Android and made the layout on the Text option, so I literally code the layout. I also learn how to make a SwipeRefreshLayout, Onboarding pages, and account page. There are also different obstacles in every task I did. I can increase my ability in every task I did on this project. So, if you want to learn about the native mobile application you could use Kotlin as your programming language and Android Studio for the IDE.

With this article, I officially finished my individual review writings on what has been learned and implemented in developing a project. Thank you for reading all my individual review, I’ll be glad that you read this last sentence. Especially, thank you Mr. Mishbah for grading all these articles :D

See you in my other writings!

Happy implementing!

Reference

https://developer.android.com/

--

--