last week I started working with the RecyclerView, and you may wonder what is the RecyclerView, but actually, you are already familiar with the RecyclerView, when you scroll down your contacts that’s a RecyclerView and when you scroll up and down searching for a lovely kitty video on Youtube you are using a RecyclerView. So what is the RecyclerView?
The RecyclerView is a modernized version of the ListView (which show items in a list) and the GridView (which show items in a grid). But why we use a RecyclerView if we can use ListView or GridView?
Let’s take ListView for a small comparison, the ListView creates a new view for your items each time you scroll down on your screen, so if you have like 1000 contacts it creates 1000 views and stores them all in the memory, and that’s very time consuming and not efficient in memory terms. So here comes the RecyclerView to recycle the unused views, it simply fills your device’s screen with a list of items and creates a couple of empty views in the top and the bottom of the screen that will not show to the user. For example when the user scrolls up the screen the views on the top; outside the screen’s boundary are used and filled with new data and display them on the bottom of the screen, while the top views move outside the boundary of the screen, from here comes the name RecyclerView.
To create a simple RecyclerView you need an Adapter to handle the data collection and bind it to the views, LayoutManager to help to position the items. The adapter uses a ViewHolder to holds references to the relevant views. So basically you create a layout for a single entry and the ViewHolder holds the references to all the views in this layout and the adapter collect the data and inflates the layout and handle filling of the data in the views and displaying it on the screen.
The second topic I worked with was Android Persistence. Android allows to persist the application data via the file system and it creates a specific directory for each application. Android supports files, preferences and SQLite database to store data in the local file system. If your application creates an SQLite database it will be saved in the main application directory under the name database, you can check it yourself just open your Whatsapp folder from the File Manager and you will find a folder named database. Files saved in the files folder and your application setting are saved as XML files in the shared_prefs folder. The shared_prefs contains key-value pairs of primitive data types. An example of using shared_prefs it to save the login state of the user to your application.
Then comes the Android networking topic and how your Android access the web using the HTTP request. HTTP request is as the name implies, a certain format to request something from the server and the server will respond to your request and retrieve your requested data (200 OK) or other responses like the famous respond (404 page not found), and for us here in Sudan the beloved response (403 Forbidden: Access is denied).
To perform network operation with standard Java API can be tons of work. There are several open source libraries are available out there, Retrofit is one of them and it’s the one the company uses.
Retrofit is a REST client for Android and Java by Square and it makes it relatively easy to retrieve and upload structured data such as JSON via a REST based web service. JSON (JavaScript Object Notation) is simply a data format (key-value format) so the client and the server can understand each other when they exchange data.
To work with Retrofit you need basically three classes, Model class which is used to map JSON data to, Interface to deal with the HTTP operations and Retrofit.Builder class to define the URL endpoint for the HTTP operations.
I am still on the Retrofit topic where there are still lots to cover so that’s it for this week.
Till the next week.
Useful time with these lines
There are more to come.
Stay tuned 🙂