mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-11-17 17:39:19 +01:00
Catalogue with only one recycler
This commit is contained in:
parent
71ab6d38e4
commit
f86c3c81bf
@ -8,7 +8,7 @@ import eu.davidea.flexibleadapter4.FlexibleAdapter
|
|||||||
import eu.kanade.tachiyomi.R
|
import eu.kanade.tachiyomi.R
|
||||||
import eu.kanade.tachiyomi.data.database.models.Manga
|
import eu.kanade.tachiyomi.data.database.models.Manga
|
||||||
import eu.kanade.tachiyomi.util.inflate
|
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 kotlinx.android.synthetic.main.item_catalogue_grid.view.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
@ -102,6 +102,6 @@ class CatalogueAdapter(val fragment: CatalogueFragment) : FlexibleAdapter<Catalo
|
|||||||
* Property to return the height for the covers based on the width to keep an aspect ratio.
|
* Property to return the height for the covers based on the width to keep an aspect ratio.
|
||||||
*/
|
*/
|
||||||
val coverHeight: Int
|
val coverHeight: Int
|
||||||
get() = fragment.catalogue_grid.itemWidth / 3 * 4
|
get() = (fragment.recycler as AutofitRecyclerView).itemWidth / 3 * 4
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ import android.support.design.widget.Snackbar
|
|||||||
import android.support.v4.widget.DrawerLayout
|
import android.support.v4.widget.DrawerLayout
|
||||||
import android.support.v7.widget.*
|
import android.support.v7.widget.*
|
||||||
import android.view.*
|
import android.view.*
|
||||||
import android.view.animation.AnimationUtils
|
|
||||||
import android.widget.ArrayAdapter
|
import android.widget.ArrayAdapter
|
||||||
import android.widget.ProgressBar
|
import android.widget.ProgressBar
|
||||||
import android.widget.Spinner
|
import android.widget.Spinner
|
||||||
@ -24,6 +23,7 @@ import eu.kanade.tachiyomi.util.connectivityManager
|
|||||||
import eu.kanade.tachiyomi.util.inflate
|
import eu.kanade.tachiyomi.util.inflate
|
||||||
import eu.kanade.tachiyomi.util.snack
|
import eu.kanade.tachiyomi.util.snack
|
||||||
import eu.kanade.tachiyomi.util.toast
|
import eu.kanade.tachiyomi.util.toast
|
||||||
|
import eu.kanade.tachiyomi.widget.AutofitRecyclerView
|
||||||
import eu.kanade.tachiyomi.widget.EndlessScrollListener
|
import eu.kanade.tachiyomi.widget.EndlessScrollListener
|
||||||
import eu.kanade.tachiyomi.widget.IgnoreFirstSpinnerListener
|
import eu.kanade.tachiyomi.widget.IgnoreFirstSpinnerListener
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
@ -53,14 +53,9 @@ open class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleVie
|
|||||||
private lateinit var adapter: CatalogueAdapter
|
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
|
private var scrollListener: EndlessScrollListener? = null
|
||||||
|
|
||||||
/**
|
|
||||||
* Scroll listener for list mode. It loads next pages when the end of the list is reached.
|
|
||||||
*/
|
|
||||||
private lateinit var listScrollListener: EndlessScrollListener
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Query of the search box.
|
* Query of the search box.
|
||||||
@ -133,6 +128,8 @@ open class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleVie
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lateinit var recycler: RecyclerView
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
/**
|
/**
|
||||||
* Creates a new instance of this fragment.
|
* Creates a new instance of this fragment.
|
||||||
@ -164,32 +161,7 @@ open class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleVie
|
|||||||
|
|
||||||
// Initialize adapter, scroll listener and recycler views
|
// Initialize adapter, scroll listener and recycler views
|
||||||
adapter = CatalogueAdapter(this)
|
adapter = CatalogueAdapter(this)
|
||||||
|
setupRecycler()
|
||||||
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)
|
|
||||||
|
|
||||||
// Create toolbar spinner
|
// Create toolbar spinner
|
||||||
val themedContext = activity.supportActionBar?.themedContext ?: activity
|
val themedContext = activity.supportActionBar?.themedContext ?: activity
|
||||||
@ -253,6 +225,46 @@ open class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleVie
|
|||||||
showProgressBar()
|
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) {
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
||||||
inflater.inflate(R.menu.catalogue_list, menu)
|
inflater.inflate(R.menu.catalogue_list, menu)
|
||||||
|
|
||||||
@ -384,8 +396,7 @@ open class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleVie
|
|||||||
hideProgressBar()
|
hideProgressBar()
|
||||||
if (page == 1) {
|
if (page == 1) {
|
||||||
adapter.clear()
|
adapter.clear()
|
||||||
gridScrollListener.resetScroll()
|
scrollListener?.resetScroll()
|
||||||
listScrollListener.resetScroll()
|
|
||||||
}
|
}
|
||||||
adapter.addItems(mangas)
|
adapter.addItems(mangas)
|
||||||
}
|
}
|
||||||
@ -425,7 +436,7 @@ open class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleVie
|
|||||||
presenter.swapDisplayMode()
|
presenter.swapDisplayMode()
|
||||||
val isListMode = presenter.isListMode
|
val isListMode = presenter.isListMode
|
||||||
activity.invalidateOptionsMenu()
|
activity.invalidateOptionsMenu()
|
||||||
switcher.showNext()
|
setupRecycler()
|
||||||
if (!isListMode || !context.connectivityManager.isActiveNetworkMetered) {
|
if (!isListMode || !context.connectivityManager.isActiveNetworkMetered) {
|
||||||
// Initialize mangas if going to grid view or if over wifi when going to list view
|
// Initialize mangas if going to grid view or if over wifi when going to list view
|
||||||
presenter.initializeMangas(adapter.items)
|
presenter.initializeMangas(adapter.items)
|
||||||
@ -451,9 +462,7 @@ open class CatalogueFragment : BaseRxFragment<CataloguePresenter>(), FlexibleVie
|
|||||||
* @return the holder of the manga or null if it's not bound.
|
* @return the holder of the manga or null if it's not bound.
|
||||||
*/
|
*/
|
||||||
private fun getHolder(manga: Manga): CatalogueHolder? {
|
private fun getHolder(manga: Manga): CatalogueHolder? {
|
||||||
return (catalogue_grid.findViewHolderForItemId(manga.id!!) ?:
|
return recycler.findViewHolderForItemId(manga.id!!) as? CatalogueHolder
|
||||||
catalogue_list.findViewHolderForItemId(manga.id!!))
|
|
||||||
as? CatalogueHolder
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,27 +21,6 @@
|
|||||||
android:layout_gravity="center_vertical|center_horizontal"
|
android:layout_gravity="center_vertical|center_horizontal"
|
||||||
android:visibility="gone"/>
|
android:visibility="gone"/>
|
||||||
|
|
||||||
|
|
||||||
<ViewSwitcher
|
|
||||||
android:id="@+id/switcher"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="0dp"
|
|
||||||
android:layout_weight="1">
|
|
||||||
<eu.kanade.tachiyomi.widget.AutofitRecyclerView
|
|
||||||
android:id="@+id/catalogue_grid"
|
|
||||||
style="@style/Theme.Widget.GridView"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"
|
|
||||||
android:columnWidth="140dp"
|
|
||||||
tools:listitem="@layout/item_catalogue_grid"/>
|
|
||||||
|
|
||||||
<android.support.v7.widget.RecyclerView
|
|
||||||
android:id="@+id/catalogue_list"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="match_parent"/>
|
|
||||||
|
|
||||||
</ViewSwitcher>
|
|
||||||
|
|
||||||
<ProgressBar
|
<ProgressBar
|
||||||
android:id="@+id/progress_grid"
|
android:id="@+id/progress_grid"
|
||||||
style="?android:attr/progressBarStyle"
|
style="?android:attr/progressBarStyle"
|
||||||
|
10
app/src/main/res/layout/recycler_autofit.xml
Normal file
10
app/src/main/res/layout/recycler_autofit.xml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<eu.kanade.tachiyomi.widget.AutofitRecyclerView
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:id="@+id/catalogue_grid"
|
||||||
|
style="@style/Theme.Widget.GridView"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:columnWidth="140dp"
|
||||||
|
tools:listitem="@layout/item_catalogue_grid" />
|
Loading…
Reference in New Issue
Block a user