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) {
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)
}
}
}
}

@ -24,6 +24,7 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(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<ExploreShowViewModel>(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<SearchBook>) {
isLoading = false
if (books.isEmpty() && adapter.isEmpty()) {
hasMore = false
loadMoreView.rotate_loading.hide(View.INVISIBLE)

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

Loading…
Cancel
Save