pull/43/head
kunfei 5 years ago
parent 3e224749ca
commit 0ad4439b0f
  1. 27
      app/src/main/java/io/legado/app/ui/explore/ExploreShowActivity.kt
  2. 46
      app/src/main/java/io/legado/app/ui/widget/LoadMoreView.kt
  3. 6
      app/src/main/java/io/legado/app/ui/widget/anima/RotateLoading.kt

@ -1,8 +1,6 @@
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
@ -13,8 +11,8 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.IntentDataHelp import io.legado.app.help.IntentDataHelp
import io.legado.app.ui.book.info.BookInfoActivity import io.legado.app.ui.book.info.BookInfoActivity
import io.legado.app.ui.widget.LoadMoreView
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import io.legado.app.utils.visible
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 kotlinx.android.synthetic.main.view_load_more.view.*
import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivity
@ -25,8 +23,7 @@ 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 lateinit var loadMoreView: LoadMoreView
private var hasMore = true
private var isLoading = true private var isLoading = true
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
@ -41,8 +38,7 @@ 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 = loadMoreView = LoadMoreView(this)
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() { recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
@ -57,7 +53,7 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activi
private fun scrollToBottom() { private fun scrollToBottom() {
adapter.let { adapter.let {
if (hasMore && !isLoading) { if (loadMoreView.hasMore && !isLoading) {
viewModel.explore() viewModel.explore()
} }
} }
@ -66,20 +62,11 @@ class ExploreShowActivity : VMBaseActivity<ExploreShowViewModel>(R.layout.activi
private fun upData(books: List<SearchBook>) { private fun upData(books: List<SearchBook>) {
isLoading = false isLoading = false
if (books.isEmpty() && adapter.isEmpty()) { if (books.isEmpty() && adapter.isEmpty()) {
hasMore = false loadMoreView.noMore(getString(R.string.empty))
loadMoreView.rotate_loading.hide(View.INVISIBLE)
loadMoreView.tv_text.setText(R.string.empty)
loadMoreView.tv_text.visible()
} else if (books.isEmpty()) { } else if (books.isEmpty()) {
hasMore = false loadMoreView.noMore()
loadMoreView.rotate_loading.hide(View.INVISIBLE)
loadMoreView.tv_text.setText(R.string.bottom_line)
loadMoreView.tv_text.visible()
} else if (adapter.getItems().contains(books.first()) && adapter.getItems().contains(books.last())) { } else if (adapter.getItems().contains(books.first()) && adapter.getItems().contains(books.last())) {
hasMore = false loadMoreView.noMore()
loadMoreView.rotate_loading.hide(View.INVISIBLE)
loadMoreView.tv_text.setText(R.string.bottom_line)
loadMoreView.tv_text.visible()
} else { } else {
adapter.addItems(books) adapter.addItems(books)
} }

@ -0,0 +1,46 @@
package io.legado.app.ui.widget
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.FrameLayout
import io.legado.app.R
import io.legado.app.utils.invisible
import io.legado.app.utils.visible
import kotlinx.android.synthetic.main.view_load_more.view.*
class LoadMoreView(context: Context) : FrameLayout(context) {
var hasMore = true
private set
init {
LayoutInflater.from(context).inflate(R.layout.view_load_more, this, true)
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
}
fun startLoad() {
tv_text.invisible()
rotate_loading.show()
}
fun stopLoad() {
rotate_loading.hide()
}
fun noMore(msg: String? = null) {
hasMore = false
rotate_loading.hide()
if (msg != null) {
tv_text.text = msg
} else {
tv_text.setText(R.string.bottom_line)
}
tv_text.visible()
}
}

@ -176,7 +176,7 @@ class RotateLoading : View {
post(shown) post(shown)
} }
fun hide(visibility: Int = GONE) { fun hide(visibility: Int = INVISIBLE) {
removeCallbacks(shown) removeCallbacks(shown)
removeCallbacks(hidden) removeCallbacks(hidden)
stopInternal(visibility) stopInternal(visibility)
@ -188,7 +188,7 @@ class RotateLoading : View {
invalidate() invalidate()
} }
private fun stopInternal(visibility: Int = GONE) { private fun stopInternal(visibility: Int = INVISIBLE) {
stopAnimator(visibility) stopAnimator(visibility)
invalidate() invalidate()
} }
@ -205,7 +205,7 @@ class RotateLoading : View {
.start() .start()
} }
private fun stopAnimator(visibility: Int = GONE) { private fun stopAnimator(visibility: Int = INVISIBLE) {
animate().cancel() animate().cancel()
isStarted = false isStarted = false
this.visibility = visibility this.visibility = visibility

Loading…
Cancel
Save