This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class DataBindingEntry( | |
@LayoutRes val layoutId: Int, | |
val bindings: Map<Int, Any?> | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DataBindingViewHolder(private val viewBinding: ViewDataBinding): RecyclerView.ViewHolder(viewBinding.root) { | |
fun bind(bindingEntry: DataBindingEntry) { | |
viewBinding.apply { | |
bindingEntry.bindings.forEach { setVariable(it.key, it.value) } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DataBindingAdapter(private var bindingEntries: List<DataBindingEntry> = emptyList()) | |
: RecyclerView.Adapter<DataBindingViewHolder>() { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBindingViewHolder { | |
val layoutInflater = LayoutInflater.from(parent.context) | |
val viewBinding = DataBindingUtil.inflate<ViewDataBinding>(layoutInflater, viewType, parent, false) | |
return DataBindingViewHolder(viewBinding) | |
} | |
override fun onBindViewHolder(holder: DataBindingViewHolder, position: Int) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.longv.pokedex.databinding | |
import androidx.databinding.BindingAdapter | |
import androidx.recyclerview.widget.RecyclerView | |
@BindingAdapter(value = ["model"]) | |
fun setupRecyclerView(view: RecyclerView, model: RecyclerViewModel?) { | |
model?.apply { | |
view.adapter = DataBindingAdapter(bindingEntries) | |
view.layoutManager = layoutManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layout | |
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"> | |
<data> | |
<variable | |
name="listModel" | |
type="androidx.lifecycle.LiveData<me.longv.pokedex.databinding.RecyclerViewModel>"/> | |
</data> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun debugSubscribeOn(): Disposable = | |
Observable.just(1, 2, 3) | |
.doOnNext { | |
println(Thread.currentThread().name) // RxCachedThreadScheduler-1 | |
} | |
.subscribeOn(Schedulers.io()) | |
.doOnNext { | |
println(Thread.currentThread().name) // RxCachedThreadScheduler-1 | |
} | |
.subscribe() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun debugSubscribeOn(): Disposable = | |
Observable.just(1, 2, 3) | |
.doOnNext { | |
println(Thread.currentThread().name) // RxCachedThreadScheduler-1 | |
} | |
.subscribeOn(Schedulers.io()) | |
.subscribeOn(Schedulers.computation()) | |
.doOnNext { | |
println(Thread.currentThread().name) // RxCachedThreadScheduler-1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun debugSubscribeOn(): Disposable = | |
Observable.just(1) | |
.doOnNext { | |
println(Thread.currentThread().name) // RxCachedThreadScheduler-1 | |
} | |
.subscribeOn(Schedulers.io()) | |
.flatMap { | |
println(Thread.currentThread().name) // RxCachedThreadScheduler-1 | |
Observable.just(it) | |
.subscribeOn(Schedulers.computation()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun debugSubscribeOn(): Disposable = | |
Observable.just(1) | |
.doOnNext { | |
println(Thread.currentThread().name) // RxCachedThreadScheduler-1 | |
} | |
.subscribeOn(Schedulers.io()) | |
.flatMap { | |
println(Thread.currentThread().name) // RxCachedThreadScheduler-1 | |
Observable.just(it) | |
.doOnNext { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity { | |
// ... | |
private val viewModel by viewModels { viewModelFactory } | |
private lateinit var adapter: DataBindingAdapter | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) |
OlderNewer