Would you love to be a content creator?

Anyone can be a publisher today and this exciting opportunity opens up a world of possibilities. Today we are all publishers even if we are not aware of the fact. The democratisation of technology…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Optimizing RecyclerView Adapter

A little about RecyclerView

This blog will discuss about optimizing the use of the RecyclerView adapter. How to reduce the pain of having a big number ofRecyclerView by reducing the creation of adapter classes for each RecyclerView.

Common RecyclerView Adapter

Basically, all adapters look like in the code above. If we give more attention to the adapter, we can see that the difference on each adapter is only in the bind() function of the ViewHolder, and when the ViewHolder is declared in the onCreateViewHolder() function.

It’s very inconvenient if we always create a new class for a RecyclerView adapter, if there are only slight differences in each. The idea is to create a class to be the base of each RecyclerView adapter so that we don’t have to create a new adapter class every time we want to use the RecyclerView.

Base RecyclerView Adapter

First, add the RecyclerView library on the gradle.

There are 3 parameters on constructor of this class.

The first one is layoutId , this parameter used to inflate which layout we want to use as item to make it flexible and easy to use.

The second one is recyclable , it’s used to set the holder become recyclable or not. Be careful when set this value to true because it can affect the RecyclerView performance.

The last one is a unit called bindView . It’s called when we want to bind the data to the UI in the BaseViewHolder.

Other than that, we don’t put the items in the constructor. But we put it in a variable, and create a function to populate it.

Implementing the new RecyclerView Adapter!

Now it’s the time for us to try it in an activity. We will create a list of a fruits name with String type data.

Create an activity which the layout contains a RecyclerView.

Then create the item layout for RecyclerView .

Next, declare the ArrayList, theRecyclerView and the adapter on the activity with lateinit modifier. Because we will display a list data of String , don’t forget to annotate the adapter with String type data.

It’s time to initialize the adapter in onCreate() function!

Still in onCreate() function, init the RecyclerView and set its adapter.

Last, populate the adapter items with the dummyData that we initialized before.

Finally, the whole code in the activity would look like this:

Finally, let’s run on our device/emulator!

And.. VOILA!! Here is our RecyclerView with our new BaseRecyclerViewAdapter . Next time we want to create a new RecyclerView , we don’t need to create any new adapter for it. Just create an adapter variable inside the activity. Isn’t it far easier? So far, it helps me to write a better documented code especially on the adapter part.

Add a comment

Related posts:

Bitcoin Crash And Price Manipulation Exposed

In the course of a few months various cryptocurrency analysts have been attempting to explain the “sudden rise” and “sudden crash” in Bitcoin. Many have held interesting speculations about Bitcoin…

The World as I see It

I have a story to tell, it’s hard to know where to begin,. “The World as I see It” is published by Joan Elizabeth White.

Big Brother Naija S7

The seventh season of Big Brother Naija, the biggest reality television show in Nigeria and Africa, premiered on Saturday, July 23, 2022. The season debut came after the introduction of 12 roommates…