Fix catalog covers' flickering when adding a page

This commit is contained in:
len 2016-05-06 14:28:39 +02:00
parent 9de3da33aa
commit e73eed4a9b
6 changed files with 54 additions and 168 deletions

View file

@ -26,7 +26,7 @@ class CatalogueAdapter(val fragment: CatalogueFragment) : FlexibleAdapter<Catalo
get() = mItems
init {
mItems = ArrayList<Manga>()
mItems = ArrayList()
setHasStableIds(true)
}
@ -36,8 +36,9 @@ class CatalogueAdapter(val fragment: CatalogueFragment) : FlexibleAdapter<Catalo
* @param list the list to add.
*/
fun addItems(list: List<Manga>) {
val sizeBeforeAdding = mItems.size
mItems.addAll(list)
notifyDataSetChanged()
notifyItemRangeInserted(sizeBeforeAdding, list.size)
}
/**

View file

@ -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<CataloguePresenter>(), 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<CataloguePresenter>(), 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

View file

@ -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;
}
}
}

View file

@ -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;
}
}
}

View file

@ -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 its 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 isnt 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
}
}

View file

@ -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
}
}
}