Skip to content

Instantly share code, notes, and snippets.

@tfcporciuncula
tfcporciuncula / DaggerComponentProvider.kt
Created December 15, 2018 15:28
Fragment injector extension
val Fragment.injector
get() = (requireActivity().application as DaggerComponentProvider).component
@tfcporciuncula
tfcporciuncula / ImageViewExtensions.kt
Created December 20, 2018 08:07
Picasso load extension
fun ImageView.load(resource: Int) {
Picasso.with(context)
.load(resource)
.into(this)
}
@tfcporciuncula
tfcporciuncula / YourModule.kt
Created December 20, 2018 14:22
Module example
@Module
object YourModule {
@Provides @JvmStatic
fun provideThatDependency() = ThatDependency()
}
@tfcporciuncula
tfcporciuncula / test.xml
Created December 30, 2018 23:47
Autosizing TextView issue
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoSizeMaxTextSize="100sp"
android:autoSizeMinTextSize="1sp"
@tfcporciuncula
tfcporciuncula / BooksAdapter.kt
Created January 6, 2019 12:46
BooksAdapter adding click listener
override fun onBindViewHolder(holder: DataBindingViewHolder<Listable>, position: Int) {
super.onBindViewHolder(holder, position) // make sure you keep this
holder.itemView.setOnClickListener {
// whatever you want!
// you could add a Presenter/ViewModel to the Adapter constructor and call a method, for instance:
// viewModel.onItemClicked()
}
}
@tfcporciuncula
tfcporciuncula / DataBindingViewHolder.kt
Created January 14, 2019 14:27
DataBindingViewHolder
class DataBindingViewHolder<T>(private val binding: ViewDataBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: T) {
binding.setVariable(BR.item, item)
binding.executePendingBindings()
}
}
@tfcporciuncula
tfcporciuncula / DataBindingAdapter.kt
Created January 14, 2019 14:30
DataBindingAdapter
abstract class DataBindingAdapter<T>(diffCallback: DiffUtil.ItemCallback<T>) :
ListAdapter<T, DataBindingViewHolder<T>>(diffCallback) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataBindingViewHolder<T> {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = DataBindingUtil.inflate<ViewDataBinding>(layoutInflater, viewType, parent, false)
return DataBindingViewHolder(binding)
}
override fun onBindViewHolder(holder: DataBindingViewHolder<T>, position: Int) = holder.bind(getItem(position))
@tfcporciuncula
tfcporciuncula / BooksAdapter.kt
Created January 14, 2019 14:31
BooksAdapter
class BooksAdapter : DataBindingAdapter<Book>(DiffCallback()) {
class DiffCallback : DiffUtil.ItemCallback<Book>() {
// your DiffCallback implementation
}
override fun getItemViewType(position: Int) = R.layout.item_book
}
@tfcporciuncula
tfcporciuncula / item_book.xml
Created January 14, 2019 14:31
item_book.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="item" type="com.example.model.Book" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
@tfcporciuncula
tfcporciuncula / BooksAdapter.kt
Created January 14, 2019 14:32
BooksAdapter with multiple item types
class BooksAdapter : DataBindingAdapter<Listable>(DiffCallback()) {
class DiffCallback : DiffUtil.ItemCallback<Listable>() {
// your DiffCallback implementation
}
override fun getItemViewType(position: Int) = if (getItem(position) is Book) {
R.layout.item_book
} else {
R.layout.item_section