Span of GridLayout is not applied in Epoxy

Viewed 54

I'm using Epoxy to represent nested recyclerviews.

Other than that, I want to place the inner child(Nested) RecyclerView in GridLayout format, but even if I set span, it only appears in vertical format.

I tried using spanCount and spanSizeOverride (didn't understand what function this is) in Controller to solve it, but it doesn't work.

What's wrong?

This is an example picture and code.

enter image description here

item_parent

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardCornerRadius="4dp"
    app:cardElevation="3dp"
    android:layout_margin="10dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEST"
            android:textSize="24sp"
            android:background="@color/gray_400"
            android:layout_marginTop="12dp"
            android:layout_marginLeft="12dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <View
            android:id="@+id/borderline"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black"
            android:layout_marginTop="12dp"
            app:layout_constraintTop_toBottomOf="@id/title"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/nested_rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@id/borderline"
            android:gravity="center_horizontal"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            android:orientation="horizontal" />
        <View
            android:id="@+id/borderline2"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black"
            android:visibility="gone"
            app:layout_constraintTop_toBottomOf="@id/nested_rv"/>
        <ImageView
            android:id="@+id/expanded"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/borderline2"
            app:layout_constraintBottom_toBottomOf="parent"
            android:src="@drawable/ic_arrow_drop_down2"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

ParentContoller

class ParentEpoxyController : EpoxyController() {
    private var data : List<ParentItem> = listOf()
            set(value) {
                field = value
                requestModelBuild()
            }

    override fun buildModels() {
        data.forEachIndexed { index, s ->
            ParentDataModel_()
                .id(index)
                .title(s.workout)
                .list(s.list)
                .onBind { model, holder, position ->

                    // Set Nested RV
                    holder.nestedRV.visibility = if(s.isExpandable) View.VISIBLE else View.GONE

                    val controller = ChildEpoxyController()
//                    controller.spanCount = 4// not applied
                    holder.nestedRV.adapter = controller.adapter
                    controller.setItem(data[position].list)

                    holder.expandBtn.setOnClickListener {
                        if(s.isExpandable) {
                            holder.nestedRV.visibility = View.GONE
                            holder.borderline.visibility = View.GONE
                        }
                        else {
                            holder.nestedRV.visibility = View.VISIBLE
                            holder.borderline.visibility = View.VISIBLE

                        }
                        s.isExpandable = !s.isExpandable
                    }
                    requestModelBuild()
                }
                .addTo(this)
        }
    }
}

ChildController

class ChildEpoxyController : EpoxyController() {
    private var data: List<String> = listOf()
            set(value) {
                field = value
                requestModelBuild()
            }
    override fun buildModels() {
        data.forEachIndexed { index, s ->
            ChildDataModel_()
                .id(index)
                .child(s)
                .spanSizeOverride { totalSpanCount, position, itemCount ->
                    4 // span size that i want, but not applied
                }
                .addTo(this)
        }
    }

    fun setItem(list: List<String>) {
        data = list
    }
}
0 Answers
Related