idanatz / OneAdapter

A Viewholderless Adapter for RecyclerView, who supports builtin diffing, states (paging, empty...), events (clicking, swiping...), and more.
MIT License
470 stars 45 forks source link

Can't display items #7

Closed NoahReyson closed 5 years ago

NoahReyson commented 5 years ago

Hi,

I recently discovered your API and it looks great at first look.

But I tried to implement a very basic example and impossible to get a result.

Here my files:

MainActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recycler = findViewById<RecyclerView>(R.id.recycler)

        val oneAdapter = OneAdapter()
            .attachItemModule(CardModule())
            .attachTo(recycler)

        val list = (0 until 100).map { CardModel("Card $it") }.toList()

        oneAdapter.setItems(list)

    }
}

CardModel

class CardModel(val title: String = "Title"): Diffable {

    private val id: Int = Random().nextInt()

    override fun areContentTheSame(other: Any): Boolean {
        return other is CardModel && title == other.title
    }

    override fun getUniqueIdentifier(): Long {
        return id.toLong()
    }
}

CardModule

class CardModule: ItemModule<CardModel>() {

    override fun onBind(model: CardModel, viewBinder: ViewBinder) {
        viewBinder.findViewById<TextView>(R.id.card_module_title).text = model.title
    }

    override fun provideModuleConfig(): ItemModuleConfig = object: ItemModuleConfig() {
        override fun withLayoutResource(): Int = R.layout.card_module
    }

}

card_module.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/card_module_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_height="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

There is no crash nor explicit error. My MainActivity just stay perfectly empty.

Is there something I misunderstood ?

idanatz commented 5 years ago

Hi, You forgot to set a LayoutManager to your RecyclerView, without it nothing would work 😉 The adapter can't work if the recyclerView does know how its children should be displayed.

From the sample app: BaseExampleActivity.kt recyclerView = findViewById(R.id.recycler) recyclerView.layoutManager = LinearLayoutManager(this)

NoahReyson commented 5 years ago

Thank you so much.

I'll change my clickbait title ^^