티스토리 뷰

Section

Let's implement CharactersComponentSpec to display text list.

@OnCreateChildren
    internal fun onCreateChildren(c: SectionContext): Children {
        val builder: Children.Builder = Children.create()

        (1..10).forEach {
            builder.child(
                    SingleComponentSection.create(c)
                            .key(it.toString())
                            .component(
                                    Text.create(c)
                                            .text("Item$it")
                                            .textSizeSp(20f)
                                            .heightDip(48f)
                                            .build()
                            )
                            .build()
            )
        }

        return builder.build()
    }

You may remember that we need to implement @OnCreateChildren annotated function to display list items.
I just created ten texts like image below.

val builder: Children.Builder = Children.create()

Create a Children.Builder to create Children instance.

   (1..10).forEach {
            builder.child(
                    SingleComponentSection.create(c)
                            .key(it.toString())
                            .component(
                                    Text.create(c)
                                            .text("Item$it")
                                            .textSizeSp(20f)
                                            .heightDip(48f)
                                            .build()
                            )
                            .build()
            )
    }

Create a SingleComponentSection. SingleComponentSection is single section. You can think it for just
one single view.
Set key. This is important because we are creating ten items and need to distinguish each item like RecyclerView's getId().
Add component to SingleComponentSection. We create a Text. Text is Litho version of TextView. As you see, you can set Text's properties by calling method chain. Those methods are easily reable because property names are similar to TextView's properties.
You don't need to call .build() of TextView. You can pass LithoView or LithoView.Builder to component or child. A top-level builder needs to call .build() and if you don't have any .build() functions in LithoView children.
Again, I think we could create an Kotlin extension function to reduce boilerplate.

'Android > Litho' 카테고리의 다른 글

6. StateValue  (0) 2019.11.10
5. HeroItemSpec  (0) 2019.11.10
4. GroupSection Spec LifeCycle  (0) 2019.11.09
2. Sample Application - Intialisation  (0) 2019.11.09
1. Litho - Get Started  (0) 2019.11.09
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함