Building complex screens in a RecyclerView with Epoxy
source link: https://proandroiddev.com/building-complex-screens-in-a-recyclerview-with-epoxy-97845e51f4da
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Building complex screens in a RecyclerView with Epoxy
1. Introduction
The RecylerView is one of the most used Android components when it comes to displaying a list of items in an Android application, and since the arrival of Jetpack in the Android ecosystem, some libraries have come to life to simplify its use.
But when it comes to developing an application that needs to display a heterogeneous list of items or that scrolls in several directions (vertically and horizontally) the use of RecyclerView can be complicated.
In this article we will see step by step how to simplify life when it comes to displaying complex lists using a library called Epoxy.
2. What is epoxy
Epoxy is an Android library for building complex screens in a RecyclerView developed by AirbnbEng
3. Some Advantages of Epoxy
Without epoxy
If you have used the RecyclerView before, you may be familiar with the steps you need to go through to view items in the list as shown in the following figure.
- Add a component of type RecyclerView in the layout.
- Create a view in an XML file that represents an element of the list.
- Create an Adapter to connect the data to the view to display it in the list.
With Epoxy
Of the three stages mentioned above, the most repetitive stage, which becomes more complex depending on the situation, is the third stage, which consists of creating an adapter, and when it comes to having a list with nested RecyclerViews, things can get a little more complicated.
And this is where Epoxy will help us in the sense that with Epoxy it is no longer necessary to create an adapter, Epoxy will do it for us and we will see how in the rest of the article.
4. Epoxy In Action
To illustrate the use of epoxy, we are going to develop an application that looks like the following figure
We will only focus on the first view of this image.
If we analyse this view very carefully, we notice that the RecyclerView contains a number of elements
- The title of the carousel.
- A carousel (a horizontal list of elements).
- A title of the tasks.
- A list of tasks.
Installation
Gradle is the only supported build configuration, so just add the dependency to your project build.gradle
file, and if you use Kotlin, your build.gradle
file will look like this.
Basic usage
After adding Epoxy to the Gradle file, you just have to create the UI of each element of the RecyclerView.
RecyclerView Items
1. Title Item
We are going to create a view that will represent the title in the RecyclerView, and with the dataBinding we will make it possible to inject the title from the outside to facilitate the reuse of this view.
As you have noticed, the layout takes a variable of type String, and the name of the layout begin with item we’ll see what we’ll use it for afterwards.
2. Carousel Item
The view that represents the carousel follows the same principle as the previous view, but this time the view has a variable of type OnClickListener that we have attached CardView in addition to a variable of type CarouselItem.
The complete layout code is as follows.
item_task_carousel.xml3. Task Item
The same principle is also applied before to the view that represents a task.
The source code for this view is as follows:
item_task.xml5. Model generation.
1. Epoxy Config
After creating the different views of our RecyclerView, it is now high time to configure Epoxy on how it will generate some components for us
Create a package-info.java file in any package of your project as shown in the following figure
Using the @EpoxyDataBindingPattern annotation on Config interface, you tell epoxy the criteria of the XML files for which it will generate a model that will be used in the code.
In our case layoutPrefix corresponds to the string “item”, this is the reason why all the layouts that will be used as views in the RecyclerView were prefixed by item, so Epoxy will generate a model for
item_title.xml
item_task.xml
item_task_carousesl.xml
2. Displaying data
After indicating to Epoxy the criteria of the layouts to be used, we have to build the project so that Epoxy can generate some classes for us,
After that, replace your RecyclerView
with EpoxyRecyclerView
in the MainActivity layout, and by default, EpoxyRecyclerView
is attached to the LinearLayoutManager
so there is no need to put it on.
To display the elements in the EpoxyRecyclerView
, just call the different methods that correspond to each item generated by Epoxy inside the withModels
method from EpoxyRecyclerView
- For
item_title.xml
the method to generate istitle(String)
- For
item_task.xml
, the method to generate istask(Task)
- And for
item_task_carousesl.xml
, the method istitleCarousel(CarouselItem)
All its methods take a lambda as parameter in which we can pass them to layout as a method
Remember that the file item_task.xml
had a variable of type Task
, that’s why on line 47, we called the method task()
by passing a Task object that we would like to display.
Each element must have a unique id to be displayed properly.
3. Carousel
For the carousel the process is a bit different (from line 18 to 33), you have to use the carousel extension method that comes with Epoxy, all the elements placed in the lambda of this method will be displayed in a horizontal list.
For each item, epoxy generates a class following this convention, for :
item_title.xml
:TitleBindingModel_
item_task.xml
:TaskBindingModel_
item_task_carousesl.xml
:TaskCarouselBindingModel_
Those classes extends EpoxyModel
And the method models() of the carousel take a list of EpoxyModel
here’s why we converted the CarouselItem
List into a List of TaskCarouselBindingModel_<CarouselItem>
to be displayed.
Conclusion
When developing an Android application sometimes you have to get help avoiding to recreate certain elements, and using Epoxy can save you a lot of time by avoiding you to type repetitive code.
If you want to test the application you can find the source code using the following link
And to know a little more about epoxy for could the link below
Recommend
-
147
Epoxy Epoxy is an Android library for building complex screens in a RecyclerView. Models are automatically generated from custom views or databinding layouts via annotation processing. These models are then used in an EpoxyController to d...
-
18
Epoxy: Interactive Model Iteration with Weak Supervision and Pre-Trained Embeddings Epoxy uses weak supervision and pre-trained embeddings to create models that can train at programmatically-interactive speeds...
-
4
Acid Decapsulation of Epoxy Molded IC Packages With Copper Wire BondsIEEE websites place cookies on your device to give you the best user experience. By using our websites, you agree to the placement of these cookies. To learn more, read ou...
-
15
Part 5. 要來介紹呈現電影列表的套件 Epoxy,主要是用這個套件來呈現比較複雜的列表,像是我們的首頁,穿插夾雜了橫滑和直滑的列表,用嵌套的 RecyclerView 來實作技術上來說一定做得到,只是你要多花時間和心力,而 Epoxy 套件提供一個更容易的實作方式,讓我...
-
6
Epoxy - An independent flexible XAML MVVM library
-
6
Transcript (c) 2021 Kouji Matsui F# Epoxy (English translated) 2021.5.22 FUN...
-
5
Benefits of building components instead of screens - September 24, 2021 Table of Content: Introduction Modern applications use many Views or UI compon...
-
2
Kouji (Kozy) Matsui on Twitter: "Epoxy 1.2.0 is out now! New supported OpenSilver, .NET 6.0 and F# 6.0 #epoxy #opensilver #xaml #mvvm https://t.co/lbOQ5YsVfH"Don’t miss what’s happeningPeople on Twitter are the first to know.
-
7
June 21, 2022 ...
-
2
This tutorial will teach you how to model complex screens in Jetpack Compose.Composable functions can represent different things on a screen. A composable function might represent a entire screen or individual elements on a screen.Le...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK