pull/43/head
kunfei 5 years ago
parent 5b827ca9b0
commit 98b3d25377
  1. 6
      app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt
  2. 18
      app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt
  3. 22
      app/src/main/java/io/legado/app/ui/explore/ExploreShowViewModel.kt

@ -167,7 +167,11 @@ abstract class CommonRecyclerAdapter<ITEM>(protected val context: Context) : Rec
synchronized(lock) { synchronized(lock) {
val oldSize = getActualItemCount() val oldSize = getActualItemCount()
if (this.items.addAll(newItems)) { if (this.items.addAll(newItems)) {
notifyItemRangeInserted(oldSize + getHeaderCount(), newItems.size) if (oldSize == 0 && getHeaderCount() == 0) {
notifyDataSetChanged()
} else {
notifyItemRangeInserted(oldSize + getHeaderCount(), newItems.size)
}
} }
} }
} }

@ -24,6 +24,7 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activi
private lateinit var adapter: ExploreShowAdapter private lateinit var adapter: ExploreShowAdapter
private lateinit var loadMoreView: View private lateinit var loadMoreView: View
private var hasMore = true private var hasMore = true
private var isLoading = true
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
title_bar.title = intent.getStringExtra("exploreName") title_bar.title = intent.getStringExtra("exploreName")
@ -41,9 +42,26 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activi
LayoutInflater.from(this).inflate(R.layout.view_load_more, recycler_view, false) LayoutInflater.from(this).inflate(R.layout.view_load_more, recycler_view, false)
adapter.addFooterView(loadMoreView) adapter.addFooterView(loadMoreView)
loadMoreView.rotate_loading.show() 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<SearchBook>) { private fun upData(books: List<SearchBook>) {
isLoading = false
if (books.isEmpty() && adapter.isEmpty()) { if (books.isEmpty() && adapter.isEmpty()) {
hasMore = false hasMore = false
loadMoreView.rotate_loading.hide(View.INVISIBLE) loadMoreView.rotate_loading.hide(View.INVISIBLE)

@ -29,18 +29,18 @@ class ExploreShowViewModel(application: Application) : BaseViewModel(application
} }
fun explore() { fun explore() {
bookSource?.let { source -> val source = bookSource
exploreUrl?.let { url -> val url = exploreUrl
WebBook(source).exploreBook(url, page, this) if (source != null && url != null) {
.timeout(30000L) WebBook(source).exploreBook(url, page, this)
.onSuccess(IO) { searchBooks -> .timeout(30000L)
searchBooks?.let { .onSuccess(IO) { searchBooks ->
booksData.postValue(searchBooks) searchBooks?.let {
App.db.searchBookDao().insert(*searchBooks.toTypedArray()) booksData.postValue(searchBooks)
page++ App.db.searchBookDao().insert(*searchBooks.toTypedArray())
} page++
} }
} }
} }
} }

Loading…
Cancel
Save