https://kotlinlang.org logo
#compose
Title
# compose
r

Robert Menke

03/18/2022, 9:56 PM
I can’t seem to find an explanation of the
contentType
parameter for
items
that I can understand. Let’s say I have a
LazyColumn
where each item is a
FooItem
. What am I supposed to be supplying to
contentType
?
Copy code
fun items(
        count: Int,
        key: ((index: Int) -> Any)? = null,
        contentType: (index: Int) -> Any? = { null },
        itemContent: @Composable LazyItemScope.(index: Int) -> Unit
    ) {
        error("The method is not implemented")
    }
d

Desmond Teo

03/19/2022, 4:28 AM
from the docs:
a factory of the content types for the item. The item compositions of the same type could be reused more efficiently. Note that null is a valid type and items of such type will be considered compatible.
somewhat similar to RecyclerView adapter’s
getItemViewType
, where it uses the view type to determine the type of ViewHolder for each item, I’m guessing you can return a different value for each type of composable you are calling for the items. If all the items are using the same composable, then it shouldn’t matter.
r

Robert Menke

03/19/2022, 1:11 PM
So if every composable is using the same type leaving it null is fine? So contentType is basically like a key for the type of composable?
a

Andrey Kulikov

03/20/2022, 1:50 AM
yes, just leaving it null is the same as always returning the same value. so only specify it if you have multiple different types
s

Stylianos Gakis

08/05/2022, 3:32 PM
One thing which is not 100% clear from the docs imo is that since it refers to the contentType as
type
it makes me think that if I have two different items with both the same type, a
String
(say “asd” and “asdf”) then it’d be considered the same. But I imagine it doesn’t actually look at the
Type
but it does an .equals check between them?
a

Andrey Kulikov

08/05/2022, 3:34 PM
if contentType function you provided returns equals objects for different indexes they are considered of the same “content type”. it has nothing to do with classes of the objects you pass
s

Stylianos Gakis

08/05/2022, 3:52 PM
Yes so the check is done with an
.equals()
as I assumed. I only mentioned that the docs, specifically the text
Copy code
* @param contentType a factory of the content types for the item. The item compositions of
 * the same type could be reused more efficiently. Note that null is a valid type and items of such
 * type will be considered compatible.
Did make me think for a second that it is may be referring to the type of object which is returned there with all the mentions of the word “type” in there. But that is most likely me doing a bad interpretation of what is being said. Out of pure curiosity, where is the source code where that check is done, I’ve tried traversing cs.android.com and I could not find anything blob thinking upside down The one I am interested in shows “This method is not implemented” so I am not sure how I could take a look myself.
51 Views