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 73fe6b2b09..ff9332fd47 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 @@ -8,7 +8,7 @@ import eu.davidea.flexibleadapter4.FlexibleAdapter import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.util.inflate -import kotlinx.android.synthetic.main.fragment_catalogue.* +import eu.kanade.tachiyomi.widget.AutofitRecyclerView import kotlinx.android.synthetic.main.item_catalogue_grid.view.* import java.util.* @@ -102,6 +102,6 @@ class CatalogueAdapter(val fragment: CatalogueFragment) : FlexibleAdapter(), FlexibleVie private lateinit var adapter: CatalogueAdapter /** - * Scroll listener for grid mode. It loads next pages when the end of the list is reached. + * Scroll listener. It loads next pages when the end of the list is reached. */ - 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: EndlessScrollListener + private var scrollListener: EndlessScrollListener? = null /** * Query of the search box. @@ -133,6 +128,8 @@ open class CatalogueFragment : BaseRxFragment(), FlexibleVie } } + lateinit var recycler: RecyclerView + companion object { /** * Creates a new instance of this fragment. @@ -164,32 +161,7 @@ open class CatalogueFragment : BaseRxFragment(), FlexibleVie // Initialize adapter, scroll listener and recycler views adapter = CatalogueAdapter(this) - - val glm = catalogue_grid.layoutManager as GridLayoutManager - gridScrollListener = EndlessScrollListener(glm, { requestNextPage() }) - catalogue_grid.setHasFixedSize(true) - catalogue_grid.adapter = adapter - catalogue_grid.addOnScrollListener(gridScrollListener) - - val llm = LinearLayoutManager(activity) - listScrollListener = EndlessScrollListener(llm, { requestNextPage() }) - catalogue_list.setHasFixedSize(true) - catalogue_list.adapter = adapter - catalogue_list.layoutManager = llm - catalogue_list.addOnScrollListener(listScrollListener) - catalogue_list.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL)) - if (presenter.isListMode) { - switcher.showNext() - } - - numColumnsSubscription = getColumnsPreferenceForCurrentOrientation().asObservable() - .doOnNext { catalogue_grid.spanCount = it } - .skip(1) - // Set again the adapter to recalculate the covers height - .subscribe { catalogue_grid.adapter = adapter } - - switcher.inAnimation = AnimationUtils.loadAnimation(activity, android.R.anim.fade_in) - switcher.outAnimation = AnimationUtils.loadAnimation(activity, android.R.anim.fade_out) + setupRecycler() // Create toolbar spinner val themedContext = activity.supportActionBar?.themedContext ?: activity @@ -253,6 +225,46 @@ open class CatalogueFragment : BaseRxFragment(), FlexibleVie showProgressBar() } + private fun setupRecycler() { + if (!isAdded) return + + numColumnsSubscription?.unsubscribe() + + val oldRecycler = catalogue_view.getChildAt(1) + var oldPosition = RecyclerView.NO_POSITION + if (oldRecycler is RecyclerView) { + oldPosition = (oldRecycler.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() + oldRecycler.adapter = null + + catalogue_view.removeView(oldRecycler) + } + + recycler = if (presenter.isListMode) { + RecyclerView(context).apply { + layoutManager = LinearLayoutManager(context) + addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL)) + } + } else { + (catalogue_view.inflate(R.layout.recycler_autofit) as AutofitRecyclerView).apply { + numColumnsSubscription = getColumnsPreferenceForCurrentOrientation().asObservable() + .doOnNext { spanCount = it } + .skip(1) + // Set again the adapter to recalculate the covers height + .subscribe { adapter = this@CatalogueFragment.adapter } + } + } + scrollListener = EndlessScrollListener(recycler.layoutManager as LinearLayoutManager, { requestNextPage() }) + recycler.setHasFixedSize(true) + recycler.addOnScrollListener(scrollListener) + recycler.adapter = adapter + + catalogue_view.addView(recycler, 1) + + if (oldPosition != RecyclerView.NO_POSITION) { + recycler.layoutManager.scrollToPosition(oldPosition) + } + } + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { inflater.inflate(R.menu.catalogue_list, menu) @@ -384,8 +396,7 @@ open class CatalogueFragment : BaseRxFragment(), FlexibleVie hideProgressBar() if (page == 1) { adapter.clear() - gridScrollListener.resetScroll() - listScrollListener.resetScroll() + scrollListener?.resetScroll() } adapter.addItems(mangas) } @@ -425,7 +436,7 @@ open class CatalogueFragment : BaseRxFragment(), FlexibleVie presenter.swapDisplayMode() val isListMode = presenter.isListMode activity.invalidateOptionsMenu() - switcher.showNext() + setupRecycler() if (!isListMode || !context.connectivityManager.isActiveNetworkMetered) { // Initialize mangas if going to grid view or if over wifi when going to list view presenter.initializeMangas(adapter.items) @@ -451,9 +462,7 @@ open class CatalogueFragment : BaseRxFragment(), FlexibleVie * @return the holder of the manga or null if it's not bound. */ private fun getHolder(manga: Manga): CatalogueHolder? { - return (catalogue_grid.findViewHolderForItemId(manga.id!!) ?: - catalogue_list.findViewHolderForItemId(manga.id!!)) - as? CatalogueHolder + return recycler.findViewHolderForItemId(manga.id!!) as? CatalogueHolder } /** diff --git a/app/src/main/res/layout/fragment_catalogue.xml b/app/src/main/res/layout/fragment_catalogue.xml index d862a45b72..0eb4fafaf4 100644 --- a/app/src/main/res/layout/fragment_catalogue.xml +++ b/app/src/main/res/layout/fragment_catalogue.xml @@ -21,27 +21,6 @@ android:layout_gravity="center_vertical|center_horizontal" android:visibility="gone"/> - - - - - - - - + \ No newline at end of file