Last active
November 12, 2022 16:37
-
-
Save addeeandra/b67aa9c1193d4b37abf5c1f34ef58387 to your computer and use it in GitHub Desktop.
Android - Kotlin Easy Adapter
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
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.recyclerview.widget.RecyclerView | |
abstract class AnyAdapter<T>(val items: MutableList<T>) : RecyclerView.Adapter<AnyViewHolder<T>>() { | |
abstract fun getLayoutId(): Int | |
abstract fun onCreateViewHolder(view: View): AnyViewHolder<T> | |
abstract fun onBindedViewHolder(holder: AnyViewHolder<T>, data: T) | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnyViewHolder<T> = onCreateViewHolder( | |
LayoutInflater.from(parent.context).inflate(getLayoutId(), parent, false) | |
) | |
override fun getItemCount(): Int = items.size | |
override fun onBindViewHolder(holder: AnyViewHolder<T>, position: Int) { | |
holder.bind(items[position]) | |
onBindedViewHolder(holder, items[position]) | |
} | |
} |
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
import android.view.View | |
import androidx.recyclerview.widget.RecyclerView | |
abstract class AnyViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) { | |
abstract fun bind(data: T) | |
} |
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
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.databinding.DataBindingUtil | |
import androidx.databinding.ViewDataBinding | |
import androidx.recyclerview.widget.RecyclerView | |
abstract class AnyViewModelAdapter<T, V : ViewDataBinding>( | |
val items: MutableList<T> | |
) : RecyclerView.Adapter<AnyViewModelHolder<T, V>>() { | |
constructor(layoutId: Int, clickHandler: ((data: MODEL) -> Unit)?) : this(layoutId, mutableListOf(), clickHandler) | |
abstract fun getLayoutId(): Int | |
abstract fun onCreateViewHolder(view: View): AnyViewModelHolder<T, V> | |
abstract fun onBindedViewHolder(holder: AnyViewModelHolder<T, V>, data: T) | |
protected lateinit var binding: V | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnyViewModelHolder<T, V> { | |
return AnyViewModelHolder( | |
DataBindingUtil.inflate( | |
LayoutInflater.from(parent.context), | |
getLayoutId(), | |
parent, | |
false | |
) | |
) | |
} | |
override fun getItemCount(): Int = items.size | |
override fun onBindViewHolder(holder: AnyViewModelHolder<T, V>, position: Int) { | |
holder.bind(items[position]) | |
onBindedViewHolder(holder, items[position]) | |
} | |
} |
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
import androidx.databinding.ViewDataBinding | |
import androidx.databinding.library.baseAdapters.BR | |
import androidx.recyclerview.widget.RecyclerView | |
open class AnyViewModelHolder<T, V : ViewDataBinding>( | |
private val binding: V | |
) : RecyclerView.ViewHolder(binding.root) { | |
fun bind(data: T) { | |
binding.setVariable(BR.viewmodel, data) | |
binding.executePendingBindings() | |
} | |
} |
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 OutletListAdapter(items: MutableList<ExampleData>) : AnyAdapter<ExampleData>(items) { | |
override fun getLayoutId(): Int = R.layout.item_example | |
override fun onCreateViewHolder(view: View): AnyViewHolder<OutletListData> { | |
return OutletListViewHolder(view) | |
// ALTERNATIVE you may use dataBinding instead like this, | |
// return OutletListViewHolder(ItemExampleBinding.bind(view)) | |
} | |
override fun onBindedViewHolder(holder: AnyViewHolder<ExampleData>, data: ExampleData) { | |
// TODO something here, like adding listener? | |
// holder.itemView.setOnClickListener(someListenerHere) | |
} | |
} |
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 ExampleData( | |
val id: Long, | |
val name: String, | |
val phone: String, | |
val description: String | |
) |
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
/** | |
* Example ViewHolder with databinding | |
*/ | |
class ExampleViewHolder(private val mBinding: ItemOutletBinding) : AnyViewHolder<ExampleData>(mBinding.root) { | |
override fun bind(data: OutletListData) { | |
mBinding.setVariable(BR.data, data) | |
mBinding.executePendingBindings() | |
} | |
} | |
/** | |
* Without data binding | |
*/ | |
class ExampleViewHolder(view: View) : AnyViewHolder<ExampleData>(view) { | |
override fun bind(data: ExampleData) { | |
// do some binding like | |
itemView.tvName.text = data.name | |
iteMView.tvPhone.text = data.phone | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment