Android RecyclerView Code

Android RecyclerView Code

What is RecyclerView?

RecyclerView is a widget that is a more flexible and advanced version of GridView and ListView. It is a container for displaying large datasets that can be scrolled efficiently by maintaining a limited number of views. You can use the RecyclerView widget when the data collection whose elements change at runtime depends on the network event or user action.

 View

The Android platform uses the View and Viewgroup classes to draw items on the screen. These classes are abstract and extend to different implementations to suit a use-case. For example, TextView has a simple purpose of displaying text content on screen. EditText originates from the same view class and adds more functionality to enable the user to input data.

It is possible to create your own custom views to be able to have more flexibility when developing a user interface. The view class provides methods by which we can draw on the screen and means to pass in parameters such as width, height, and our own custom attributes, which we would like to add to our scene to behave as we wish.

ViewGroups

The viewgroup class is a kind of view, but unlike the simple view class, whose responsibility is simply displaying, the viewgroup gives us the ability to have multiple views in a view, which we can refer to as a whole. In this case, the view that is created at the top level that we add other simple views (we can also add view groups) is called “parent”, and the views that are added inside are called “children”. It is said.

View code

var arr1 = [1,2,3] //imagine a simple View as an Array
//we can imagine this as a NumberTextView which doesn't really exist
//but we could imagine there's one that makes it easy to use numbers
var arr2 = ["a","b","c"] // We can imagine this as another simple view

var nestedArr = [arr1,arr2] //in our anology, we can now group views
//together and the structure that would hold that would be what we call the ViewGroup

RecyclerView.Adapter

Groundwork # 1 – Adapter Pattern

An adapter is a device that changes the characteristics of a system or device to an otherwise incompatible device or system. Some of them modify signal or power characteristics, while others simply change the physical form of one connector to another.

A simple example we find in real life to explain an adapter is when we need to connect devices together, but they have connection ports that do not match each other. This may be the case when you move to a different country where they use different types of sockets. If you carry your phone or laptop charger, it will be impossible to connect it to a power outlet. However, you will not give up but just get an adapter that will come between the power outlet and your charger and enable charging.

This is the case in programming when we want to combine two data structures together to accomplish a task, but their default port has no way to communicate with each other.

We will use a simple example of a device and a charger. We have two examples of chargers. An american one and a british one

class AmericanCharger() {
    var chargingPower = 10
}
class BritishCharger(){
    var charginPower = 5
}

We’ll then create two devices

class AmericanDevice() 
class BritishDevice()

var myAmericanPhone = new AmericanDevice()
var myBritishPhone = new BritishDevice()
sealed trait Device
class AmericanDevice : Device{
    fun charge(charger:AmericanCharger){
        //Do some American charging
    }
}
class BritishDevice: Device{
    fun charge(charger:BritishCharger){
        //Do some British charging
    }
}

submit questions and problems

Leave a Reply

Your email address will not be published. Required fields are marked *