From 0ad4439b0f91ae8d22f26892f4bdeaf1b9bb227f Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 28 Oct 2019 10:02:28 +0800 Subject: [PATCH] up --- .../app/ui/explore/ExploreShowActivity.kt | 27 +++-------- .../io/legado/app/ui/widget/LoadMoreView.kt | 46 +++++++++++++++++++ .../app/ui/widget/anima/RotateLoading.kt | 6 +-- 3 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/widget/LoadMoreView.kt 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 d2cef29e1..e9d7ef69d 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 @@ -1,8 +1,6 @@ package io.legado.app.ui.explore import android.os.Bundle -import android.view.LayoutInflater -import android.view.View import androidx.lifecycle.Observer import androidx.recyclerview.widget.DividerItemDecoration 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.help.IntentDataHelp 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.visible import kotlinx.android.synthetic.main.activity_explore_show.* import kotlinx.android.synthetic.main.view_load_more.view.* import org.jetbrains.anko.startActivity @@ -25,8 +23,7 @@ class ExploreShowActivity : VMBaseActivity(R.layout.activi get() = getViewModel(ExploreShowViewModel::class.java) private lateinit var adapter: ExploreShowAdapter - private lateinit var loadMoreView: View - private var hasMore = true + private lateinit var loadMoreView: LoadMoreView private var isLoading = true override fun onActivityCreated(savedInstanceState: Bundle?) { @@ -41,8 +38,7 @@ class ExploreShowActivity : VMBaseActivity(R.layout.activi recycler_view.layoutManager = LinearLayoutManager(this) recycler_view.addItemDecoration(DividerItemDecoration(this, RecyclerView.VERTICAL)) recycler_view.adapter = adapter - loadMoreView = - LayoutInflater.from(this).inflate(R.layout.view_load_more, recycler_view, false) + loadMoreView = LoadMoreView(this) adapter.addFooterView(loadMoreView) loadMoreView.rotate_loading.show() recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { @@ -57,7 +53,7 @@ class ExploreShowActivity : VMBaseActivity(R.layout.activi private fun scrollToBottom() { adapter.let { - if (hasMore && !isLoading) { + if (loadMoreView.hasMore && !isLoading) { viewModel.explore() } } @@ -66,20 +62,11 @@ class ExploreShowActivity : VMBaseActivity(R.layout.activi private fun upData(books: List) { isLoading = false if (books.isEmpty() && adapter.isEmpty()) { - hasMore = false - loadMoreView.rotate_loading.hide(View.INVISIBLE) - loadMoreView.tv_text.setText(R.string.empty) - loadMoreView.tv_text.visible() + loadMoreView.noMore(getString(R.string.empty)) } else if (books.isEmpty()) { - hasMore = false - loadMoreView.rotate_loading.hide(View.INVISIBLE) - loadMoreView.tv_text.setText(R.string.bottom_line) - loadMoreView.tv_text.visible() + loadMoreView.noMore() } else if (adapter.getItems().contains(books.first()) && adapter.getItems().contains(books.last())) { - hasMore = false - loadMoreView.rotate_loading.hide(View.INVISIBLE) - loadMoreView.tv_text.setText(R.string.bottom_line) - loadMoreView.tv_text.visible() + loadMoreView.noMore() } else { adapter.addItems(books) } diff --git a/app/src/main/java/io/legado/app/ui/widget/LoadMoreView.kt b/app/src/main/java/io/legado/app/ui/widget/LoadMoreView.kt new file mode 100644 index 000000000..ee17ee735 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/LoadMoreView.kt @@ -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() + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/anima/RotateLoading.kt b/app/src/main/java/io/legado/app/ui/widget/anima/RotateLoading.kt index 96b94c66b..f98726b8e 100644 --- a/app/src/main/java/io/legado/app/ui/widget/anima/RotateLoading.kt +++ b/app/src/main/java/io/legado/app/ui/widget/anima/RotateLoading.kt @@ -176,7 +176,7 @@ class RotateLoading : View { post(shown) } - fun hide(visibility: Int = GONE) { + fun hide(visibility: Int = INVISIBLE) { removeCallbacks(shown) removeCallbacks(hidden) stopInternal(visibility) @@ -188,7 +188,7 @@ class RotateLoading : View { invalidate() } - private fun stopInternal(visibility: Int = GONE) { + private fun stopInternal(visibility: Int = INVISIBLE) { stopAnimator(visibility) invalidate() } @@ -205,7 +205,7 @@ class RotateLoading : View { .start() } - private fun stopAnimator(visibility: Int = GONE) { + private fun stopAnimator(visibility: Int = INVISIBLE) { animate().cancel() isStarted = false this.visibility = visibility