Search code examples
androidkotlinlithofacebook-litho

Litho ListRecyclerConfiguration Kotlin with linearLayoutInfoFactory


I'm new to Kotlin, and I want to use Facebook's litho library, I have found the java way to create a recycler configuration but I am unable to do the same in Kotlin.

              RecyclerCollectionComponent.create(c)
                  .disablePTR(true)
                  .recyclerConfiguration(new ListRecyclerConfiguration(LinearLayoutManager.HORIZONTAL, /*reverse layout*/ false, SNAP_TO_CENTER))
                  .section(
                      DataDiffSection.create(c)
                          .data(generateData(32))
                          .renderEventHandler(ListSection.onRender(c))
                          .build())
                  .canMeasureRecycler(true))

So how would i do this in Kotlin? So far I have this, but its not working.

.recyclerConfiguration(
            ListRecyclerConfiguration.create()
                .linearLayoutInfoFactory(LinearLayoutInfoFactory {
                    c, LinearLayoutManager.HORIZONTAL, false
                })
                .build()
        )

It doesn't seem to like the Linearlayoutinfo factory constructor, I have checked github examples and couldn't find it. If I have more understanding of the conversion of Java to Kotlin I would probably understand how to do this easily.

Edit: Error from Android Studio:

Unexpected tokens (use ';' to seperate expressons on the same line)

I imagine this is because of the syntax, but i think the real issue is around the construction of the LinearLayoutInfoFactory.


Solution

  • I've managed to get this working, the issue was around the creation of the recycler configuration, i was trying to initialise the interface and not the actual implementation of the interface.

    RecyclerCollectionComponent.create(c)
            .recyclerConfiguration(
                ListRecyclerConfiguration.create()
                    .orientation(LinearLayoutManager.HORIZONTAL)
                    .snapMode(0)
                    .build()
            )
            .section(
                DataDiffSection.create<DiscoverListDataModel>(SectionContext(c))
                    .data(dataModels)
                    .renderEventHandler(DiscoverListComponent.onRender(c))
                    .onCheckIsSameItemEventHandler(DiscoverListComponent.isSameItem(c))
                    .onCheckIsSameContentEventHandler(DiscoverListComponent.isSameContent(c))
                    .build()
            )
            .canMeasureRecycler(true)
            .disablePTR(true)
            .build()