Skip to content

Commit

Permalink
Refactor PokemonAdapter for moving setOnClickListener to onCreateView…
Browse files Browse the repository at this point in the history
…Holder
  • Loading branch information
skydoves committed Sep 1, 2020
1 parent 15ffd46 commit 5040270
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions app/src/main/java/com/skydoves/pokedex/ui/adapter/PokemonAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.NO_POSITION
import com.skydoves.pokedex.R
import com.skydoves.pokedex.databinding.ItemPokemonBinding
import com.skydoves.pokedex.model.Pokemon
Expand All @@ -34,7 +35,17 @@ class PokemonAdapter : RecyclerView.Adapter<PokemonAdapter.PokemonViewHolder>()
val inflater = LayoutInflater.from(parent.context)
val binding =
DataBindingUtil.inflate<ItemPokemonBinding>(inflater, R.layout.item_pokemon, parent, false)
return PokemonViewHolder(binding)
return PokemonViewHolder(binding).apply {
binding.root.setOnClickListener {
val position = adapterPosition.takeIf { it != NO_POSITION }
?: return@setOnClickListener
val currentTime = System.currentTimeMillis()
if (currentTime - onClickedTime > binding.transformationLayout.duration) {
DetailActivity.startActivity(binding.transformationLayout, items[position])
onClickedTime = currentTime
}
}
}
}

fun addPokemonList(pokemonList: List<Pokemon>) {
Expand All @@ -43,17 +54,9 @@ class PokemonAdapter : RecyclerView.Adapter<PokemonAdapter.PokemonViewHolder>()
}

override fun onBindViewHolder(holder: PokemonViewHolder, position: Int) {
val item = items[position]
holder.binding.apply {
pokemon = item
pokemon = items[position]
executePendingBindings()
root.setOnClickListener {
val currentTime = System.currentTimeMillis()
if (currentTime - onClickedTime > transformationLayout.duration) {
onClickedTime = currentTime
DetailActivity.startActivity(transformationLayout, item)
}
}
}
}

Expand Down

0 comments on commit 5040270

Please # to comment.