diff --git a/.gitignore b/.gitignore index f06cd1c..5dedaf2 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ gradle.xml .idea/markdown-navigator-enh.xml .idea/markdown-navigator.xml .idea/runConfigurations.xml +/.idea/kotlinScripting.xml diff --git a/rekukler/src/main/java/com/rerekt/rekukler/MultibindingsAdapter.kt b/rekukler/src/main/java/com/rerekt/rekukler/MultibindingsAdapter.kt index 4bdbc58..baeafa5 100644 --- a/rekukler/src/main/java/com/rerekt/rekukler/MultibindingsAdapter.kt +++ b/rekukler/src/main/java/com/rerekt/rekukler/MultibindingsAdapter.kt @@ -5,10 +5,11 @@ import androidx.recyclerview.widget.RecyclerView import androidx.viewbinding.ViewBinding open class MultiBindingAdapter( - val bindersSet: List> + vararg binders: ViewBinder<*, *> ): RecyclerView.Adapter() { var items: List = listOf() + internal val bindersSet = binders.toList() as List> override fun onCreateViewHolder( parent: ViewGroup, diff --git a/rekukler/src/main/java/com/rerekt/rekukler/RecyclerViewDSL.kt b/rekukler/src/main/java/com/rerekt/rekukler/RecyclerViewDSL.kt index 4a1d59b..b5cf802 100644 --- a/rekukler/src/main/java/com/rerekt/rekukler/RecyclerViewDSL.kt +++ b/rekukler/src/main/java/com/rerekt/rekukler/RecyclerViewDSL.kt @@ -3,17 +3,18 @@ package com.rerekt.rekukler import android.content.Context import android.graphics.Bitmap import androidx.core.graphics.drawable.toDrawable +import androidx.fragment.app.Fragment import androidx.recyclerview.widget.* import androidx.viewbinding.ViewBinding fun RecyclerView.configure( - customAdapter: ((List>) -> MultiBindingAdapter)? = null, + adapter: MultiBindingAdapter, block: RecyclerViewConfig.() -> Unit ) { RecyclerViewConfig(context).also { block(it) layoutManager = it.layoutManager - adapter = customAdapter?.invoke(it.bindersSet) ?: MultiBindingAdapter(it.bindersSet) + this.adapter = adapter it.itemDecorations.forEach { addItemDecoration(it) } } } @@ -40,7 +41,6 @@ class RecyclerViewConfig( private val context: Context ) { - internal var bindersSet = listOf>() internal var itemDecorations: MutableList = mutableListOf() internal var layoutManager: RecyclerView.LayoutManager = @@ -82,8 +82,4 @@ class RecyclerViewConfig( itemDecorations.add(decoration) } - fun viewBinders(vararg viewBinder: ViewBinder<*, *>) { - bindersSet = viewBinder.toList() as List> - } - } \ No newline at end of file diff --git a/sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt b/sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt index 76571b8..a96d0d4 100644 --- a/sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt +++ b/sample/src/main/java/com/rerekt/sample/ui/ListFragment.kt @@ -6,6 +6,7 @@ import android.os.Looper import android.view.View import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager +import com.rerekt.rekukler.MultiBindingAdapter import com.rerekt.rekukler.configure import com.rerekt.rekukler.updateList import com.rerekt.sample.R @@ -17,6 +18,13 @@ class ListFragment: Fragment(R.layout.fragment_main) { lateinit var binding: FragmentMainBinding + private val adapter by lazy { + MultiBindingAdapter( + articlesBinder { println("Click from Article item") }, + loadingBinder() + ) + } + @ExperimentalStdlibApi override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) @@ -47,15 +55,11 @@ class ListFragment: Fragment(R.layout.fragment_main) { } private fun initRecycler() { - binding.rvArticles.configure { + binding.rvArticles.configure(adapter) { linearLayout { reverseLayout = false orientation = LinearLayoutManager.VERTICAL } - viewBinders( - articlesBinder { println("Click from Article item") }, - loadingBinder() - ) dividerItemDecoration( size = 2.dip(resources).toInt() )