From e73eed4a9bf7dbd12e5655a72e38eb434593fb58 Mon Sep 17 00:00:00 2001 From: len Date: Fri, 6 May 2016 14:28:39 +0200 Subject: [PATCH] Fix catalog covers' flickering when adding a page --- .../ui/catalogue/CatalogueAdapter.kt | 5 +- .../ui/catalogue/CatalogueFragment.kt | 12 ++-- .../widget/EndlessGridScrollListener.java | 49 --------------- .../widget/EndlessListScrollListener.java | 49 --------------- .../widget/EndlessScrollListener.java | 61 ------------------- .../tachiyomi/widget/EndlessScrollListener.kt | 46 ++++++++++++++ 6 files changed, 54 insertions(+), 168 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/EndlessGridScrollListener.java delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/EndlessListScrollListener.java delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/EndlessScrollListener.java create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/EndlessScrollListener.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueAdapter.kt index 38a7c1c2c..3cb42d434 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueAdapter.kt @@ -26,7 +26,7 @@ class CatalogueAdapter(val fragment: CatalogueFragment) : FlexibleAdapter() + mItems = ArrayList() setHasStableIds(true) } @@ -36,8 +36,9 @@ class CatalogueAdapter(val fragment: CatalogueFragment) : FlexibleAdapter) { + val sizeBeforeAdding = mItems.size mItems.addAll(list) - notifyDataSetChanged() + notifyItemRangeInserted(sizeBeforeAdding, list.size) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt index 174dbc46a..84402e35e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueFragment.kt @@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.ui.catalogue import android.content.res.Configuration import android.os.Bundle -import android.support.v7.app.AppCompatActivity import android.support.v7.widget.GridLayoutManager import android.support.v7.widget.SearchView import android.support.v7.widget.Toolbar @@ -24,8 +23,7 @@ import eu.kanade.tachiyomi.ui.manga.MangaActivity import eu.kanade.tachiyomi.util.getResourceDrawable import eu.kanade.tachiyomi.util.snack import eu.kanade.tachiyomi.util.toast -import eu.kanade.tachiyomi.widget.EndlessGridScrollListener -import eu.kanade.tachiyomi.widget.EndlessListScrollListener +import eu.kanade.tachiyomi.widget.EndlessScrollListener import eu.kanade.tachiyomi.widget.NpaLinearLayoutManager import kotlinx.android.synthetic.main.fragment_catalogue.* import kotlinx.android.synthetic.main.toolbar.* @@ -56,12 +54,12 @@ class CatalogueFragment : BaseRxFragment(), FlexibleViewHold /** * Scroll listener for grid mode. It loads next pages when the end of the list is reached. */ - private lateinit var gridScrollListener: EndlessGridScrollListener + private lateinit var gridScrollListener: EndlessScrollListener /** * Scroll listener for list mode. It loads next pages when the end of the list is reached. */ - private lateinit var listScrollListener: EndlessListScrollListener + private lateinit var listScrollListener: EndlessScrollListener /** * Query of the search box. @@ -135,13 +133,13 @@ class CatalogueFragment : BaseRxFragment(), FlexibleViewHold adapter = CatalogueAdapter(this) val glm = catalogue_grid.layoutManager as GridLayoutManager - gridScrollListener = EndlessGridScrollListener(glm, { requestNextPage() }) + gridScrollListener = EndlessScrollListener(glm, { requestNextPage() }) catalogue_grid.setHasFixedSize(true) catalogue_grid.adapter = adapter catalogue_grid.addOnScrollListener(gridScrollListener) val llm = NpaLinearLayoutManager(activity) - listScrollListener = EndlessListScrollListener(llm, { requestNextPage() }) + listScrollListener = EndlessScrollListener(llm, { requestNextPage() }) catalogue_list.setHasFixedSize(true) catalogue_list.adapter = adapter catalogue_list.layoutManager = llm diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessGridScrollListener.java b/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessGridScrollListener.java deleted file mode 100644 index e49430d97..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessGridScrollListener.java +++ /dev/null @@ -1,49 +0,0 @@ -package eu.kanade.tachiyomi.widget; - -import android.support.v7.widget.GridLayoutManager; -import android.support.v7.widget.RecyclerView; - -import rx.functions.Action0; - -public class EndlessGridScrollListener extends RecyclerView.OnScrollListener { - - private int previousTotal = 0; // The total number of items in the dataset after the last load - private boolean loading = true; // True if we are still waiting for the last set of data to load. - private static final int VISIBLE_THRESHOLD = 5; // The minimum amount of items to have below your current scroll position before loading more. - private int firstVisibleItem, visibleItemCount, totalItemCount; - - private final GridLayoutManager layoutManager; - - private final Action0 requestNext; - - public EndlessGridScrollListener(GridLayoutManager layoutManager, Action0 requestNext) { - this.layoutManager = layoutManager; - this.requestNext = requestNext; - } - - public void resetScroll() { - previousTotal = 0; - loading = true; - } - - @Override - public void onScrolled(RecyclerView recyclerView, int dx, int dy) { - super.onScrolled(recyclerView, dx, dy); - - visibleItemCount = recyclerView.getChildCount(); - totalItemCount = layoutManager.getItemCount(); - firstVisibleItem = layoutManager.findFirstVisibleItemPosition(); - - if (loading && (totalItemCount > previousTotal)) { - loading = false; - previousTotal = totalItemCount; - } - if (!loading && (totalItemCount - visibleItemCount) - <= (firstVisibleItem + VISIBLE_THRESHOLD)) { - // End has been reached - requestNext.call(); - loading = true; - } - } - -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessListScrollListener.java b/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessListScrollListener.java deleted file mode 100644 index 768f6bd28..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessListScrollListener.java +++ /dev/null @@ -1,49 +0,0 @@ -package eu.kanade.tachiyomi.widget; - -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; - -import rx.functions.Action0; - -public class EndlessListScrollListener extends RecyclerView.OnScrollListener { - - private int previousTotal = 0; // The total number of items in the dataset after the last load - private boolean loading = true; // True if we are still waiting for the last set of data to load. - private static final int VISIBLE_THRESHOLD = 5; // The minimum amount of items to have below your current scroll position before loading more. - private int firstVisibleItem, visibleItemCount, totalItemCount; - - private LinearLayoutManager layoutManager; - - private final Action0 requestNext; - - public EndlessListScrollListener(LinearLayoutManager layoutManager, Action0 requestNext) { - this.layoutManager = layoutManager; - this.requestNext = requestNext; - } - - public void resetScroll() { - previousTotal = 0; - loading = true; - } - - @Override - public void onScrolled(RecyclerView recyclerView, int dx, int dy) { - super.onScrolled(recyclerView, dx, dy); - - visibleItemCount = recyclerView.getChildCount(); - totalItemCount = layoutManager.getItemCount(); - firstVisibleItem = layoutManager.findFirstVisibleItemPosition(); - - if (loading && (totalItemCount > previousTotal)) { - loading = false; - previousTotal = totalItemCount; - } - if (!loading && (totalItemCount - visibleItemCount) - <= (firstVisibleItem + VISIBLE_THRESHOLD)) { - // End has been reached - requestNext.call(); - loading = true; - } - } - -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessScrollListener.java b/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessScrollListener.java deleted file mode 100644 index 98e20aa08..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessScrollListener.java +++ /dev/null @@ -1,61 +0,0 @@ -package eu.kanade.tachiyomi.widget; - -import android.widget.AbsListView; - -import rx.functions.Action0; - -public class EndlessScrollListener implements AbsListView.OnScrollListener { - // The minimum amount of items to have below your current scroll position - // before loading more. - private static final int VISIBLE_THRESHOLD = 5; - // The total number of items in the dataset after the last load - private int previousTotalItemCount = 0; - // True if we are still waiting for the last set of data to load. - private boolean loading = true; - - private final Action0 requestNext; - - public EndlessScrollListener(Action0 requestNext) { - this.requestNext = requestNext; - } - - public void resetScroll() { - this.previousTotalItemCount = 0; - this.loading = true; - } - - // This happens many times a second during a scroll, so be wary of the code you place here. - // We are given a few useful parameters to help us work out if we need to load some more data, - // but first we check if we are waiting for the previous load to finish. - @Override - public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) - { - // If the total item count is zero and the previous isn't, assume the - // list is invalidated and should be reset back to initial state - if (totalItemCount < previousTotalItemCount) { - this.previousTotalItemCount = totalItemCount; - if (totalItemCount == 0) { this.loading = true; } - } - // If it’s still loading, we check to see if the dataset count has - // changed, if so we conclude it has finished loading and update the current page - // number and total item count. - if (loading && (totalItemCount > previousTotalItemCount)) { - loading = false; - previousTotalItemCount = totalItemCount; - } - - // If it isn’t currently loading, we check to see if we have breached - // the visibleThreshold and need to reload more data. - // If we do need to reload some more data, we execute onLoadMore to fetch the data. - if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + VISIBLE_THRESHOLD)) { - requestNext.call(); - loading = true; - } - } - - @Override - public void onScrollStateChanged(AbsListView view, int scrollState) { - // Don't take any action on changed - } - -} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessScrollListener.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessScrollListener.kt new file mode 100644 index 000000000..55e81c6d3 --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/EndlessScrollListener.kt @@ -0,0 +1,46 @@ +package eu.kanade.tachiyomi.widget + +import android.support.v7.widget.LinearLayoutManager +import android.support.v7.widget.RecyclerView + +class EndlessScrollListener( + private val layoutManager: LinearLayoutManager, + private val requestNext: () -> Unit) +: RecyclerView.OnScrollListener() { + + companion object { + // The minimum amount of items to have below your current scroll position before loading + // more. + private val VISIBLE_THRESHOLD = 5 + } + + private var previousTotal = 0 // The total number of items in the dataset after the last load + private var loading = true // True if we are still waiting for the last set of data to load. + private var firstVisibleItem = 0 + private var visibleItemCount = 0 + private var totalItemCount = 0 + + fun resetScroll() { + previousTotal = 0 + loading = true + } + + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + super.onScrolled(recyclerView, dx, dy) + + visibleItemCount = recyclerView.childCount + totalItemCount = layoutManager.itemCount + firstVisibleItem = layoutManager.findFirstVisibleItemPosition() + + if (loading && totalItemCount > previousTotal) { + loading = false + previousTotal = totalItemCount + } + if (!loading && totalItemCount - visibleItemCount <= firstVisibleItem + VISIBLE_THRESHOLD) { + // End has been reached + requestNext() + loading = true + } + } + +} \ No newline at end of file