Questions about Android recyclerview

Asked 2 years ago, Updated 2 years ago, 24 views

In recyclerview on Android,

I want to know the usage of getItemId and setHasStableIds methods.

Why are those two here?

android

2022-09-22 14:43

1 Answers

The setHasStablesIds(boolean) method is a method that states that you want to give a unique ID for each item item that connects to the adapter.

If you call notifyDataSetChanged when the data in the list is changed, the id of the item does not change if you declare setHasStablesIds(true).

Instead, if you override getItemId even if setHasStablesIds(true), NO_ID will be returned It'll work

There can be many reasons for using getItemId by giving unique itemId, such as modifying, deleting, and searching data.

example)

class Car {
     int number;
     String name;
}
@Override
public long getItemId(int position){
    Car car = mListOfCars.get(position);
    return (car.name).hashcode();
}

In this way, you can create a unique id and turn it over.


2022-09-22 14:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.