Skip to content

Commit

Permalink
Merge pull request #13 from intive-FDV/troubleshooting/paging_adapter…
Browse files Browse the repository at this point in the history
…_scrolling_bug

[Fix] scroll bug + column width
  • Loading branch information
andres-seoane-intive authored Sep 21, 2021
2 parents c624ca2 + ffeca9a commit ce9da4f
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.intive.tmdbandroid.common

import androidx.recyclerview.widget.DiffUtil
import com.intive.tmdbandroid.model.TVShow

class TVShowAsyncPagingDataDiffCallback : DiffUtil.ItemCallback<TVShow>() {
override fun areItemsTheSame(oldItem: TVShow, newItem: TVShow): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(oldItem: TVShow, newItem: TVShow): Boolean {
return oldItem == newItem
}
}
17 changes: 9 additions & 8 deletions app/src/main/java/com/intive/tmdbandroid/home/ui/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import kotlin.math.floor
class HomeFragment : Fragment() {
private val viewModel: HomeViewModel by viewModels()

private lateinit var tvShowPageAdapter: TVShowPageAdapter
private val clickListener = { tvShow: TVShow ->
val action = HomeFragmentDirections.actionHomeFragmentDestToTVShowDetail(tvShow.id)
findNavController().navigate(action)
}
private val tvShowPageAdapter = TVShowPageAdapter(clickListener)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -122,17 +126,12 @@ class HomeFragment : Fragment() {
private fun initViews(binding: FragmentHomeBinding) {
val rvTopTVShows = binding.rvPopularTVShows

val clickListener = { tvShow: TVShow ->
val action = HomeFragmentDirections.actionHomeFragmentDestToTVShowDetail(tvShow.id)
findNavController().navigate(action)
}
tvShowPageAdapter = TVShowPageAdapter(clickListener)

rvTopTVShows.apply {
val displayMetrics = context.resources.displayMetrics
val dpWidth = displayMetrics.widthPixels / displayMetrics.density
Timber.i("MAS - dpWidth: $dpWidth")

val scaling = 200
val scaling = resources.getInteger(R.integer.screening_width)
val columnCount = floor(dpWidth / scaling).toInt()
Timber.i("MAS - columnCount: $columnCount")

Expand All @@ -146,6 +145,8 @@ class HomeFragment : Fragment() {
}
}

tvShowPageAdapter.widthSize = ((floor(dpWidth / columnCount) - resources.getInteger(R.integer.short_padding)) * displayMetrics.density).toInt()

layoutManager = manager
adapter = tvShowPageAdapter
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,83 @@ package com.intive.tmdbandroid.home.ui.adapters
import android.content.res.Resources
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.paging.AsyncPagingDataDiffer
import androidx.paging.PagingData
import androidx.recyclerview.widget.*
import com.intive.tmdbandroid.R
import com.intive.tmdbandroid.common.TVShowAsyncPagingDataDiffCallback
import com.intive.tmdbandroid.databinding.ItemHorizontalListBinding
import com.intive.tmdbandroid.databinding.ItemScreeningBinding
import com.intive.tmdbandroid.databinding.ItemTitleBinding
import com.intive.tmdbandroid.model.TVShow

class TVShowPageAdapter(private val clickListener: ((TVShow) -> Unit)) : PagingDataAdapter<TVShow, RecyclerView.ViewHolder>(COMPARATOR) {
class TVShowPageAdapter(private val clickListener: ((TVShow) -> Unit)) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
private val COMPARATOR = object : DiffUtil.ItemCallback<TVShow>() {
override fun areItemsTheSame(oldItem: TVShow, newItem: TVShow): Boolean = (oldItem == newItem)
override fun areContentsTheSame(oldItem: TVShow, newItem: TVShow): Boolean = (oldItem == newItem)
}

private const val HEADER = 0
private const val WATCHLIST = 1
private const val POPULAR = 2
}

var widthSize: Int = 0
val adapterCallback = AdapterListUpdateCallback(this)

private val differ = AsyncPagingDataDiffer(
TVShowAsyncPagingDataDiffCallback(),
object : ListUpdateCallback {
override fun onInserted(position: Int, count: Int) {
adapterCallback.onInserted(position + 1, count)
}

override fun onRemoved(position: Int, count: Int) {
adapterCallback.onRemoved(position + 1, count)
}

override fun onMoved(fromPosition: Int, toPosition: Int) {
adapterCallback.onMoved(fromPosition + 1, toPosition + 1)
}

override fun onChanged(position: Int, count: Int, payload: Any?) {
adapterCallback.onChanged(position + 1, count, payload)
}
}
)

suspend fun submitData(tvShowPagingData: PagingData<TVShow>) {
differ.submitData(tvShowPagingData)
}

private val watchlistAdapter = WatchlistAdapter(clickListener)

override fun getItemCount(): Int {
return differ.itemCount + 3
}

fun refreshWatchlistAdapter(list: List<TVShow>) {
watchlistAdapter.submitList(list)
}

override fun getItemViewType(position: Int): Int {
return when (position) {
0,2 -> HEADER
0, 2 -> HEADER
1 -> WATCHLIST
else -> POPULAR
}
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

when (holder) {
is HeaderHolder -> holder.bind(position)
is WatchlistHolder -> holder.bind()
is TVShowHolder -> getItem(position - 3)?.let { holder.bind(it) }
is TVShowHolder -> differ.getItem(position - 3)?.let { holder.bind(it) }
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
HEADER -> HeaderHolder(ItemTitleBinding.inflate(LayoutInflater.from(parent.context), parent, false))
WATCHLIST -> WatchlistHolder(ItemHorizontalListBinding.inflate(LayoutInflater.from(parent.context), parent, false))
POPULAR -> TVShowHolder(ItemScreeningBinding.inflate(LayoutInflater.from(parent.context), parent, false), clickListener )
POPULAR -> TVShowHolder(ItemScreeningBinding.inflate(LayoutInflater.from(parent.context), parent, false), clickListener)
else -> throw Exception("Illegal ViewType")
}
}
Expand All @@ -78,6 +106,7 @@ class TVShowPageAdapter(private val clickListener: ((TVShow) -> Unit)) : PagingD
fun bind() {
rvWatchlist.apply {
layoutManager = LinearLayoutManager(itemView.context, LinearLayoutManager.HORIZONTAL, false)
watchlistAdapter.widthSize = widthSize
adapter = watchlistAdapter
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.intive.tmdbandroid.databinding.ItemScreeningBinding
import com.intive.tmdbandroid.model.TVShow
import timber.log.Timber

class WatchlistAdapter(private val clickListener: ((TVShow) -> Unit)) : ListAdapter<TVShow, TVShowHolder>(COMPARATOR) {
var widthSize: Int = 0

companion object {
private val COMPARATOR = object : DiffUtil.ItemCallback<TVShow>() {
override fun areItemsTheSame(oldItem: TVShow, newItem: TVShow): Boolean = (oldItem == newItem)
Expand All @@ -19,8 +22,14 @@ class WatchlistAdapter(private val clickListener: ((TVShow) -> Unit)) : ListAdap
holder.bind(getItem(position))
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TVShowHolder = TVShowHolder(
ItemScreeningBinding.inflate(LayoutInflater.from(parent.context), parent, false),
clickListener
)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TVShowHolder {
val binding = ItemScreeningBinding.inflate(LayoutInflater.from(parent.context), parent, false)
Timber.i("MAS - width: $widthSize")
binding.screeningContainer.layoutParams.width = widthSize

return TVShowHolder(
binding,
clickListener
)
}
}
Binary file added app/src/main/res/drawable/poster_placeholder.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 7 additions & 9 deletions app/src/main/res/layout/item_screening.xml
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>

<androidx.cardview.widget.CardView
android:id="@+id/screening_container"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="16dp"
android:layout_margin="8dp"
android:paddingBottom="8dp"
app:cardCornerRadius="5dp"
android:elevation="5dp">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/screening_poster"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:minWidth="192dp"
app:layout_constraintDimensionRatio="1:1.5"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@drawable/ic_launcher_foreground" />
tools:src="@drawable/poster_placeholder" />

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/screening_title"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-w200dp/integer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="screening_width">100</integer>
<integer name="short_padding">8</integer>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values-w300dp/integer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="screening_width">150</integer>
<integer name="short_padding">8</integer>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values-w400dp/integer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="screening_width">200</integer>
<integer name="short_padding">8</integer>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/integer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="screening_width">250</integer>
<integer name="short_padding">8</integer>
</resources>

0 comments on commit ce9da4f

Please sign in to comment.