From f3882b3c5f66b5edaccf5a9d05bb50d8ab7986ee Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 18 Oct 2019 16:36:21 +0800 Subject: [PATCH] up --- .../app/ui/rss/article/RssArticlesActivity.kt | 80 +++++-------------- .../ui/rss/article/RssArticlesViewModel.kt | 4 + .../ui/widget/recycler/RefreshRecyclerView.kt | 77 ++++++++++++++++++ .../main/res/layout/activity_rss_artivles.xml | 12 +-- app/src/main/res/layout/view_load_more.xml | 5 +- .../main/res/layout/view_refresh_recycler.xml | 16 ++++ 6 files changed, 122 insertions(+), 72 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/widget/recycler/RefreshRecyclerView.kt create mode 100644 app/src/main/res/layout/view_refresh_recycler.xml diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt index 20353f4c8..e2016e11b 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesActivity.kt @@ -1,9 +1,10 @@ package io.legado.app.ui.rss.article -import android.annotation.SuppressLint import android.os.Bundle -import android.util.Log -import android.view.* +import android.view.LayoutInflater +import android.view.Menu +import android.view.MenuItem +import android.view.View import androidx.core.content.ContextCompat import androidx.lifecycle.LiveData import androidx.lifecycle.Observer @@ -19,6 +20,8 @@ import io.legado.app.ui.rss.read.ReadRssActivity import io.legado.app.ui.rss.source.edit.RssSourceEditActivity import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_rss_artivles.* +import kotlinx.android.synthetic.main.view_load_more.view.* +import kotlinx.android.synthetic.main.view_refresh_recycler.* import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult @@ -32,9 +35,7 @@ class RssArticlesActivity : VMBaseActivity(R.layout.activi private var adapter: RssArticlesAdapter? = null private var rssArticlesData: LiveData>? = null private var url: String? = null - - private var durTouchX = -1000000f - private var durTouchY = -1000000f + private lateinit var loadMoreView: View override fun onActivityCreated(savedInstanceState: Bundle?) { initView() @@ -44,10 +45,8 @@ class RssArticlesActivity : VMBaseActivity(R.layout.activi url = intent.getStringExtra("url") url?.let { initData(it) - viewModel.loadContent(it) { - refresh_progress_bar.isAutoLoading = false - } } + refresh_recycler_view.startLoading() } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { @@ -72,7 +71,6 @@ class RssArticlesActivity : VMBaseActivity(R.layout.activi return super.onCompatOptionsItemSelected(item) } - @SuppressLint("InflateParams") private fun initView() { ATH.applyEdgeEffectColor(recycler_view) recycler_view.layoutManager = LinearLayoutManager(this) @@ -84,59 +82,17 @@ class RssArticlesActivity : VMBaseActivity(R.layout.activi }) adapter = RssArticlesAdapter(this, this) recycler_view.adapter = adapter - val loadMoreView = LayoutInflater.from(this).inflate(R.layout.view_load_more, null) + loadMoreView = + LayoutInflater.from(this).inflate(R.layout.view_load_more, recycler_view, false) adapter?.addFooterView(loadMoreView) refresh_progress_bar.isAutoLoading = true - recycler_view.setOnTouchListener(object : View.OnTouchListener { - @SuppressLint("ClickableViewAccessibility") - override fun onTouch(v: View?, event: MotionEvent?): Boolean { - when (event?.action) { - MotionEvent.ACTION_DOWN -> { - durTouchX = event.x - durTouchY = event.y - } - MotionEvent.ACTION_MOVE -> { - if (durTouchX == -1000000f) { - durTouchX = event.x - } - if (durTouchY == -1000000f) - durTouchY = event.y - - val dY = event.y - durTouchY //>0下拉 - durTouchY = event.y - if (!refresh_progress_bar.isAutoLoading && refresh_progress_bar.getSecondDurProgress() == refresh_progress_bar.secondFinalProgress) { - recycler_view.adapter?.let { - if (it.itemCount > 0) { - if (0 == (recycler_view.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()) { - refresh_progress_bar.setSecondDurProgress((refresh_progress_bar.getSecondDurProgress() + dY / 2).toInt()) - } - } else { - refresh_progress_bar.setSecondDurProgress((refresh_progress_bar.getSecondDurProgress() + dY / 2).toInt()) - } - } - return refresh_progress_bar.getSecondDurProgress() > 0 - } - } - MotionEvent.ACTION_UP -> { - if (!refresh_progress_bar.isAutoLoading && refresh_progress_bar.secondMaxProgress > 0 && refresh_progress_bar.getSecondDurProgress() > 0) { - if (refresh_progress_bar.getSecondDurProgress() >= refresh_progress_bar.secondMaxProgress) { - refresh_progress_bar.isAutoLoading = true - url?.let { - viewModel.loadContent(it) { - refresh_progress_bar.isAutoLoading = false - } - } - } else { - refresh_progress_bar.setSecondDurProgressWithAnim(0) - } - } - durTouchX = -1000000f - durTouchY = -1000000f - } + refresh_recycler_view.onRefreshStart = { + url?.let { + viewModel.loadContent(it) { + refresh_progress_bar.isAutoLoading = false } - return false } - }) + } recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { super.onScrolled(recyclerView, dx, dy) @@ -156,7 +112,11 @@ class RssArticlesActivity : VMBaseActivity(R.layout.activi } private fun scrollToBottom() { - Log.d("xxxxxx", "scrollToBottom") + adapter?.let { + if (it.getActualItemCount() > 0) { + loadMoreView.rotate_loading.show() + } + } } override fun readRss(rssArticle: RssArticle) { diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt index 1f16efee2..603a24148 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt @@ -50,4 +50,8 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application loadContent(url, onFinally) } } + + fun loadMore() { + + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/recycler/RefreshRecyclerView.kt b/app/src/main/java/io/legado/app/ui/widget/recycler/RefreshRecyclerView.kt new file mode 100644 index 000000000..f3bb3feef --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/recycler/RefreshRecyclerView.kt @@ -0,0 +1,77 @@ +package io.legado.app.ui.widget.recycler + +import android.annotation.SuppressLint +import android.content.Context +import android.util.AttributeSet +import android.view.LayoutInflater +import android.view.MotionEvent +import android.view.View +import android.widget.LinearLayout +import androidx.recyclerview.widget.LinearLayoutManager +import io.legado.app.R +import kotlinx.android.synthetic.main.view_refresh_recycler.view.* + + +class RefreshRecyclerView(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) { + + private var durTouchX = -1000000f + private var durTouchY = -1000000f + + var onRefreshStart: (() -> Unit)? = null + + init { + LayoutInflater.from(context).inflate(R.layout.view_refresh_recycler, this, true) + recycler_view.setOnTouchListener(object : View.OnTouchListener { + @SuppressLint("ClickableViewAccessibility") + override fun onTouch(v: View?, event: MotionEvent?): Boolean { + when (event?.action) { + MotionEvent.ACTION_DOWN -> { + durTouchX = event.x + durTouchY = event.y + } + MotionEvent.ACTION_MOVE -> { + if (durTouchX == -1000000f) { + durTouchX = event.x + } + if (durTouchY == -1000000f) + durTouchY = event.y + + val dY = event.y - durTouchY //>0下拉 + durTouchY = event.y + if (!refresh_progress_bar.isAutoLoading && refresh_progress_bar.getSecondDurProgress() == refresh_progress_bar.secondFinalProgress) { + recycler_view.adapter?.let { + if (it.itemCount > 0) { + if (0 == (recycler_view.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()) { + refresh_progress_bar.setSecondDurProgress((refresh_progress_bar.getSecondDurProgress() + dY / 2).toInt()) + } + } else { + refresh_progress_bar.setSecondDurProgress((refresh_progress_bar.getSecondDurProgress() + dY / 2).toInt()) + } + } + return refresh_progress_bar.getSecondDurProgress() > 0 + } + } + MotionEvent.ACTION_UP -> { + if (!refresh_progress_bar.isAutoLoading && refresh_progress_bar.secondMaxProgress > 0 && refresh_progress_bar.getSecondDurProgress() > 0) { + if (refresh_progress_bar.getSecondDurProgress() >= refresh_progress_bar.secondMaxProgress) { + refresh_progress_bar.isAutoLoading = true + onRefreshStart?.invoke() + } else { + refresh_progress_bar.setSecondDurProgressWithAnim(0) + } + } + durTouchX = -1000000f + durTouchY = -1000000f + } + } + return false + } + }) + } + + fun startLoading() { + refresh_progress_bar.isAutoLoading = true + onRefreshStart?.invoke() + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_rss_artivles.xml b/app/src/main/res/layout/activity_rss_artivles.xml index 5eceec843..39678114e 100644 --- a/app/src/main/res/layout/activity_rss_artivles.xml +++ b/app/src/main/res/layout/activity_rss_artivles.xml @@ -11,21 +11,15 @@ android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" /> - - + app:layout_constraintTop_toBottomOf="@id/title_bar"> - diff --git a/app/src/main/res/layout/view_load_more.xml b/app/src/main/res/layout/view_load_more.xml index b8b37f64a..a55372d30 100644 --- a/app/src/main/res/layout/view_load_more.xml +++ b/app/src/main/res/layout/view_load_more.xml @@ -2,7 +2,6 @@ @@ -11,16 +10,16 @@ android:layout_width="36dp" android:layout_height="36dp" android:layout_margin="6dp" - android:visibility="gone" + android:layout_gravity="center" app:loading_width="2dp" /> + + + + + + + \ No newline at end of file