pull/43/head
kunfei 5 years ago
parent 418cf7983e
commit 350354ce86
  1. 44
      app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt

@ -1,6 +1,8 @@
package io.legado.app.ui.explore package io.legado.app.ui.explore
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -10,7 +12,9 @@ import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.ui.book.info.BookInfoActivity import io.legado.app.ui.book.info.BookInfoActivity
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import io.legado.app.utils.gone
import kotlinx.android.synthetic.main.activity_explore_show.* import kotlinx.android.synthetic.main.activity_explore_show.*
import kotlinx.android.synthetic.main.view_load_more.view.*
import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivity
class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activity_explore_show), class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activity_explore_show),
@ -19,11 +23,11 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activi
get() = getViewModel(ExploreShowViewModel::class.java) get() = getViewModel(ExploreShowViewModel::class.java)
private lateinit var adapter: ExploreShowAdapter private lateinit var adapter: ExploreShowAdapter
private lateinit var loadMoreView: View
private var hasMore = true
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
intent.getStringExtra("exploreName")?.let { title_bar.title = intent.getStringExtra("exploreName")
title_bar.title = it
}
initRecyclerView() initRecyclerView()
viewModel.booksData.observe(this, Observer { upData(it) }) viewModel.booksData.observe(this, Observer { upData(it) })
viewModel.initData(intent) viewModel.initData(intent)
@ -34,11 +38,45 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activi
recycler_view.layoutManager = LinearLayoutManager(this) recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.addItemDecoration(DividerItemDecoration(this, RecyclerView.VERTICAL)) recycler_view.addItemDecoration(DividerItemDecoration(this, RecyclerView.VERTICAL))
recycler_view.adapter = adapter recycler_view.adapter = adapter
loadMoreView =
LayoutInflater.from(this).inflate(R.layout.view_load_more, recycler_view, false)
adapter.addFooterView(loadMoreView)
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 (it.getActualItemCount() > 0 && hasMore) {
loadMoreView.rotate_loading.show()
viewModel.explore()
}
}
} }
private fun upData(books: List<SearchBook>) { private fun upData(books: List<SearchBook>) {
if (books.isEmpty() && adapter.isEmpty()) {
hasMore = false
loadMoreView.rotate_loading.gone()
loadMoreView.tv_text.text = ""
} else if (books.isEmpty()) {
hasMore = false
loadMoreView.rotate_loading.gone()
loadMoreView.tv_text.text = "我是有底线的"
} else if (adapter.getItems().contains(books.first()) && adapter.getItems().contains(books.last())) {
hasMore = false
loadMoreView.rotate_loading.gone()
loadMoreView.tv_text.text = "我是有底线的"
} else {
adapter.addItems(books) adapter.addItems(books)
} }
}
override fun showBookInfo(bookUrl: String) { override fun showBookInfo(bookUrl: String) {
startActivity<BookInfoActivity>(Pair("searchBookUrl", bookUrl)) startActivity<BookInfoActivity>(Pair("searchBookUrl", bookUrl))

Loading…
Cancel
Save