diff --git a/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt b/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt index a87416469..97073009f 100644 --- a/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt +++ b/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt @@ -167,7 +167,11 @@ abstract class CommonRecyclerAdapter(protected val context: Context) : Rec synchronized(lock) { val oldSize = getActualItemCount() if (this.items.addAll(newItems)) { - notifyItemRangeInserted(oldSize + getHeaderCount(), newItems.size) + if (oldSize == 0 && getHeaderCount() == 0) { + notifyDataSetChanged() + } else { + notifyItemRangeInserted(oldSize + getHeaderCount(), newItems.size) + } } } } diff --git a/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt b/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt index 59e4b0502..3535b8e4b 100644 --- a/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt +++ b/app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt @@ -24,6 +24,7 @@ class ExploreShowActivity : VMBaseActivity(R.layout.activi private lateinit var adapter: ExploreShowAdapter private lateinit var loadMoreView: View private var hasMore = true + private var isLoading = true override fun onActivityCreated(savedInstanceState: Bundle?) { title_bar.title = intent.getStringExtra("exploreName") @@ -41,9 +42,26 @@ class ExploreShowActivity : VMBaseActivity(R.layout.activi LayoutInflater.from(this).inflate(R.layout.view_load_more, recycler_view, false) adapter.addFooterView(loadMoreView) loadMoreView.rotate_loading.show() + recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + super.onScrolled(recyclerView, dx, dy) + if (!recyclerView.canScrollVertically(1)) { + scrollToBottom() + } + } + }) + } + + private fun scrollToBottom() { + adapter.let { + if (hasMore && !isLoading) { + viewModel.explore() + } + } } private fun upData(books: List) { + isLoading = false if (books.isEmpty() && adapter.isEmpty()) { hasMore = false loadMoreView.rotate_loading.hide(View.INVISIBLE) diff --git a/app/src/main/java/io/legado/app/ui/explore/ExploreShowViewModel.kt b/app/src/main/java/io/legado/app/ui/explore/ExploreShowViewModel.kt index a9514c8e4..cf7ea9ebc 100644 --- a/app/src/main/java/io/legado/app/ui/explore/ExploreShowViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/explore/ExploreShowViewModel.kt @@ -29,18 +29,18 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application } fun explore() { - bookSource?.let { source -> - exploreUrl?.let { url -> - WebBook(source).exploreBook(url, page, this) - .timeout(30000L) - .onSuccess(IO) { searchBooks -> - searchBooks?.let { - booksData.postValue(searchBooks) - App.db.searchBookDao().insert(*searchBooks.toTypedArray()) - page++ - } + val source = bookSource + val url = exploreUrl + if (source != null && url != null) { + WebBook(source).exploreBook(url, page, this) + .timeout(30000L) + .onSuccess(IO) { searchBooks -> + searchBooks?.let { + booksData.postValue(searchBooks) + App.db.searchBookDao().insert(*searchBooks.toTypedArray()) + page++ } - } + } } }