-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: recycler generator with one view holder
refactor: removed image decoder for coroutines
- Loading branch information
CraZyLegenD
committed
Dec 19, 2020
1 parent
e9e71ba
commit 5951676
Showing
6 changed files
with
124 additions
and
51 deletions.
There are no files selected for viewing
33 changes: 0 additions & 33 deletions
33
coroutines/src/main/java/com/crazylegend/coroutines/ImageDecoder.kt
This file was deleted.
Oops, something went wrong.
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
55 changes: 55 additions & 0 deletions
55
recyclerview/src/main/java/com/crazylegend/recyclerview/AbstractViewBindingHolderAdapter.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.crazylegend.recyclerview | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.ListAdapter | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.viewbinding.ViewBinding | ||
import com.crazylegend.recyclerview.clickListeners.forItemClickListener | ||
|
||
|
||
/** | ||
* Created by crazy on 4/5/20 to long live and prosper ! | ||
* Takes leverage of not providing that damn layout res id | ||
* | ||
* USAGE: | ||
* class TestViewBindingAdapter : AbstractViewBindingAdapter<TestModel, TestViewHolderShimmer, CustomizableCardViewBinding>( | ||
::TestViewHolderShimmer, CustomizableCardViewBinding::inflate | ||
) | ||
* | ||
*/ | ||
abstract class AbstractViewBindingHolderAdapter<T, VB : ViewBinding>( | ||
private val bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB, | ||
areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null }, | ||
areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null } | ||
) : | ||
ListAdapter<T, AbstractViewBindingHolderAdapter.AbstractViewHolder<VB>>(GenericDiffUtil(areItemsTheSameCallback, areContentsTheSameCallback)) { | ||
|
||
var forItemClickListener: forItemClickListener<T>? = null | ||
var onLongClickListener: forItemClickListener<T>? = null | ||
|
||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AbstractViewHolder<VB> { | ||
val binding = bindingInflater.invoke(LayoutInflater.from(parent.context), parent, false) | ||
val holder = AbstractViewHolder(binding) | ||
|
||
holder.itemView.setOnClickListenerCooldown { | ||
if (holder.adapterPosition != RecyclerView.NO_POSITION) | ||
forItemClickListener?.forItem(holder.adapterPosition, getItem(holder.adapterPosition), it) | ||
} | ||
holder.itemView.setOnLongClickListener { | ||
if (holder.adapterPosition != RecyclerView.NO_POSITION) | ||
onLongClickListener?.forItem(holder.adapterPosition, getItem(holder.adapterPosition), it) | ||
true | ||
} | ||
return holder | ||
} | ||
|
||
override fun onBindViewHolder(holder: AbstractViewHolder<VB>, position: Int) { | ||
val item = getItem(holder.adapterPosition) | ||
bindItems(item, position, itemCount, holder.binding) | ||
} | ||
|
||
abstract fun bindItems(item: T, position: Int, itemCount: Int, binding: VB) | ||
|
||
class AbstractViewHolder<VB : ViewBinding>(val binding: VB) : RecyclerView.ViewHolder(binding.root) | ||
} |
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
51 changes: 51 additions & 0 deletions
51
recyclerview/src/main/java/com/crazylegend/recyclerview/RecyclerViewWithHolderGenerator.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.crazylegend.recyclerview | ||
|
||
import android.view.LayoutInflater | ||
import android.view.ViewGroup | ||
import androidx.recyclerview.widget.RecyclerView | ||
import androidx.viewbinding.ViewBinding | ||
|
||
|
||
/** | ||
* Created by crazy on 3/9/20 to long live and prosper ! | ||
*/ | ||
|
||
|
||
inline fun <reified T, VB : ViewBinding> generateRecyclerWithHolder( | ||
noinline bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB, | ||
noinline areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null }, | ||
noinline areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null }, | ||
crossinline binder: (item: T, position: Int, itemCount: Int, binding: VB) -> Unit): AbstractViewBindingHolderAdapter<T, VB> { | ||
|
||
return object : AbstractViewBindingHolderAdapter<T, VB>(bindingInflater, areItemsTheSameCallback, areContentsTheSameCallback) { | ||
override fun bindItems(item: T, position: Int, itemCount: Int, binding: VB) { | ||
binder(item, position, itemCount, binding) | ||
} | ||
} | ||
} | ||
|
||
inline fun <reified T, VB : ViewBinding> RecyclerView.generateVerticalAdapterWithHolder( | ||
noinline bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB, | ||
noinline areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null }, | ||
noinline areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null }, | ||
crossinline binder: (item: T, position: Int, itemCount: Int, binding: VB) -> Unit, | ||
hasFixedSize: Boolean = false, reverseLayout: Boolean = false): AbstractViewBindingHolderAdapter<T, VB> { | ||
|
||
val adapter = generateRecyclerWithHolder(bindingInflater, areItemsTheSameCallback, areContentsTheSameCallback, binder) | ||
initRecyclerViewAdapter(adapter, RecyclerView.VERTICAL, hasFixedSize, reverseLayout) | ||
return adapter | ||
} | ||
|
||
inline fun <reified T, VB : ViewBinding> RecyclerView.generateHorizontalAdapterWithHolder( | ||
noinline bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> VB, | ||
noinline areItemsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null }, | ||
noinline areContentsTheSameCallback: (old: T, new: T) -> Boolean? = { _, _ -> null }, | ||
crossinline binder: (item: T, position: Int, itemCount: Int, binding: VB) -> Unit, | ||
hasFixedSize: Boolean = false, reverseLayout: Boolean = false): AbstractViewBindingHolderAdapter<T, VB> { | ||
|
||
val adapter = generateRecyclerWithHolder(bindingInflater, areItemsTheSameCallback, areContentsTheSameCallback, binder) | ||
initRecyclerViewAdapter(adapter, RecyclerView.HORIZONTAL, hasFixedSize, reverseLayout) | ||
return 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