From e007a2322788b836003ce9d6b4364cc8c0539e8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=9C=8B=E9=A3=9E?= Date: Fri, 11 Nov 2022 10:14:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=B8=B8=E6=8F=90=E4=BA=A4:2022-11-11?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 王朋飞 --- .../QuickRefreshRecyclerView.kt | 65 +++++++---- .../helper/Request2RefreshView.kt | 44 ++++--- .../listeners/Request2List.kt | 110 +++++++++++++++++- .../app/quick/demo/RefreshListTestActivity.kt | 6 + gradle.properties | 2 +- 5 files changed, 179 insertions(+), 48 deletions(-) diff --git a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/QuickRefreshRecyclerView.kt b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/QuickRefreshRecyclerView.kt index ffaef7b5..7030f965 100644 --- a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/QuickRefreshRecyclerView.kt +++ b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/QuickRefreshRecyclerView.kt @@ -26,39 +26,58 @@ open class QuickRefreshRecyclerView @JvmOverloads constructor( @JvmField var mRequestData = RequestData(0) + @JvmField + val refreshCallback = object : CallbackList { + override fun callback(data: List?) { + getQuickAdapter().mDataList?.clear() + getQuickAdapter().appendList(data) + mRequestData.loadDataSize(data?.size ?: 0) + if (mDataChangeListener?.refreshFinish() != true) { + adapter.notifyDataSetChanged() + } + } + } + + @JvmField + val loadMoreCallback = object : CallbackList { + override fun callback(data: List?) { + appendList(data) + mRequestData.loadDataSize(data?.size ?: 0) + if (mDataChangeListener?.loadMoreFinish() != true) { + adapter.notifyItemRangeInserted( + size() - (data?.size ?: 0), (data?.size ?: 0) + ) + } + } + } + private var mDataChangeListener: Request2ListWithView? = null fun setDataChangeListener(dataChangeListener: Request2ListWithView) { mDataChangeListener = dataChangeListener + (mDataChangeListener as? Request2ListWithView)?.let { + it.requestData = mRequestData + it.refreshCallback = refreshCallback + it.loadMoreCallback = loadMoreCallback + } } override fun onRefresh() { - mRequestData.refresh() - (mDataChangeListener as? Request2ListWithView) - ?.requestAndCallback(this, mRequestData, object : CallbackList { - override fun callback(data: List?) { - setNewData(data) - mRequestData.loadDataSize(data?.size ?: 0) - adapter.notifyDataSetChanged() - } - - }) - mDataChangeListener?.refreshFinish() + refreshView?.onRefresh() ?: let { + mRequestData.refresh() + (mDataChangeListener as? Request2ListWithView) + ?.requestAndCallback(this, mRequestData, refreshCallback) + mDataChangeListener?.refreshFinish() + } } override fun onLoadMore() { - mRequestData.loadMore() - (mDataChangeListener as? Request2ListWithView) - ?.requestAndCallback(this, mRequestData, object : CallbackList { - override fun callback(data: List?) { - appendList(data) - mRequestData.loadDataSize(data?.size ?: 0) - adapter.notifyItemRangeInserted( - size() - (data?.size ?: 0), (data?.size ?: 0) - ) - } - }) - mDataChangeListener?.loadMoreFinish() + refreshView?.onLoadMore() ?: let { + mRequestData.loadMore() + (mDataChangeListener as? Request2ListWithView) + ?.requestAndCallback(this, mRequestData, loadMoreCallback) + mDataChangeListener?.loadMoreFinish() + } } } \ No newline at end of file diff --git a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/helper/Request2RefreshView.kt b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/helper/Request2RefreshView.kt index ee64b547..1fa297d9 100644 --- a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/helper/Request2RefreshView.kt +++ b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/helper/Request2RefreshView.kt @@ -28,37 +28,43 @@ object Request2RefreshView : RequestData() } val realData = data as Request2ListWithView + realData.view = view + realData.requestData = requestData + val refreshCallback = object : CallbackList { + override fun callback(data: List?) { + quickAdapter.mDataList?.clear() + quickAdapter.appendList(data) + requestData.loadDataSize(data?.size ?: 0) + if (!realData.refreshFinish()) { + quickAdapter.notifyDataSetChanged() + } + } + } + realData.refreshCallback = refreshCallback + val loadMoreCallback = object : CallbackList { + override fun callback(data: List?) { + quickAdapter.appendList(data) + requestData.loadDataSize(data?.size ?: 0) + if (!realData.loadMoreFinish()) { + quickAdapter.notifyDataSetChanged() + } + } + } + realData.loadMoreCallback = loadMoreCallback view.refreshView = object : RefreshView { override var refreshView: RefreshView? = this override fun onRefresh() { super.onRefresh() requestData.refresh() - realData.requestAndCallback(view, requestData, object : CallbackList { - override fun callback(data: List?) { - quickAdapter.mDataList?.clear() - quickAdapter.appendList(data) - requestData.loadDataSize(data?.size ?: 0) - if (!realData.refreshFinish()) { - quickAdapter.notifyDataSetChanged() - } - } - }) + realData.requestAndCallback(view, requestData, refreshCallback) realData.refreshFinish() } override fun onLoadMore() { super.onLoadMore() requestData.loadMore() - realData.requestAndCallback(view, requestData, object : CallbackList { - override fun callback(data: List?) { - quickAdapter.appendList(data) - requestData.loadDataSize(data?.size ?: 0) - if (!realData.loadMoreFinish()) { - quickAdapter.notifyDataSetChanged() - } - } - }) + realData.requestAndCallback(view, requestData, loadMoreCallback) realData.loadMoreFinish() } } diff --git a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/Request2List.kt b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/Request2List.kt index 90082037..316de54c 100644 --- a/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/Request2List.kt +++ b/QuickRecyclerView/src/main/java/com/wpf/app/quickrecyclerview/listeners/Request2List.kt @@ -1,5 +1,6 @@ package com.wpf.app.quickrecyclerview.listeners +import androidx.annotation.CallSuper import com.wpf.app.quickrecyclerview.data.QuickItemData import com.wpf.app.quickrecyclerview.data.RequestData import com.wpf.app.quickutil.CallbackList @@ -9,43 +10,124 @@ import com.wpf.app.quickutil.CallbackList * */ -interface Request2List: Request2ListWithView { +interface Request2List : Request2ListWithView { /** * 接口请求 */ - fun requestAndCallback(requestData: Request, callback: CallbackList) + @CallSuper + fun requestAndCallback(requestData: Request, callback: CallbackList) { + this.requestData = requestData + if (requestData.isRefresh) { + this.refreshCallback = callback + } else { + this.loadMoreCallback = callback + } + } override fun requestAndCallback(view: Any, requestData: Request, callback: CallbackList) { + super.requestAndCallback(view, requestData, callback) requestAndCallback(requestData, callback) } + + @CallSuper + fun manualRun(requestData: Request) { + if (requestData.isRefresh) { + refreshCallback?.let { + request2List?.requestAndCallback(view!!, requestData, it) + } + } else { + loadMoreCallback?.let { + request2List?.requestAndCallback(view!!, requestData, it) + } + } + } } interface Request2ListWithView { + var request2List: Request2ListWithView? + var view: View? + var requestData: Request? + var refreshCallback: CallbackList? + var loadMoreCallback: CallbackList? + /** * 接口请求 */ - fun requestAndCallback(view: View, requestData: Request, callback: CallbackList) + @CallSuper + fun requestAndCallback(view: View, requestData: Request, callback: CallbackList) { + this.view = view + this.requestData = requestData + if (requestData.isRefresh) { + this.refreshCallback = callback + } else { + this.loadMoreCallback = callback + } + } /** * 刷新结束 * 返回true表示刷新结束后自己刷新adapter */ - fun refreshFinish(): Boolean { return false } + fun refreshFinish(): Boolean { + return false + } /** * 加载结束 * 返回true表示刷新结束后自己刷新adapter */ - fun loadMoreFinish(): Boolean { return false } + fun loadMoreFinish(): Boolean { + return false + } + + /** + * 按照最后的请求手动再次调用1次 + */ + @CallSuper + fun manualRun() { + if (view != null && requestData != null) { + if (requestData!!.isRefresh) { + refreshCallback?.let { + request2List?.requestAndCallback(view!!, requestData!!, it) + } + } else { + loadMoreCallback?.let { + request2List?.requestAndCallback(view!!, requestData!!, it) + } + } + } + } + + /** + * 按照新的请求手动再次调用1次 + */ + @CallSuper + fun manualRun(view: View, requestData: Request) { + if (requestData.isRefresh) { + refreshCallback?.let { + request2List?.requestAndCallback(view, requestData, it) + } + } else { + loadMoreCallback?.let { + request2List?.requestAndCallback(view, requestData, it) + } + } + } } fun requestData2List( callbackF: (requestData: Request, callback: CallbackList) -> Unit ) = object : Request2List { + override var request2List: Request2ListWithView? = this + override var view: Any? = null + override var requestData: Request? = null + override var refreshCallback: CallbackList? = null + override var loadMoreCallback: CallbackList? = null override fun requestAndCallback(requestData: Request, callback: CallbackList) { + super.requestAndCallback(requestData, callback) callbackF.invoke(requestData, callback) } } @@ -53,8 +135,14 @@ fun requestData2List( fun requestData2ListWithView( callbackF: (view: View, requestData: Request, callback: CallbackList) -> Unit ) = object : Request2ListWithView { + override var request2List: Request2ListWithView? = this + override var view: View? = null + override var requestData: Request? = null + override var refreshCallback: CallbackList? = null + override var loadMoreCallback: CallbackList? = null override fun requestAndCallback(view: View, requestData: Request, callback: CallbackList) { + super.requestAndCallback(view, requestData, callback) callbackF.invoke(view, requestData, callback) } } @@ -62,8 +150,14 @@ fun requestData2ListWithView fun request2List( callbackF: (requestData: RequestData, callback: CallbackList) -> Unit ) = object : Request2List { + override var request2List: Request2ListWithView? = this + override var view: Any? = null + override var requestData: RequestData? = null + override var refreshCallback: CallbackList? = null + override var loadMoreCallback: CallbackList? = null override fun requestAndCallback(requestData: RequestData, callback: CallbackList) { + super.requestAndCallback(requestData, callback) callbackF.invoke(requestData, callback) } } @@ -71,8 +165,14 @@ fun request2List( fun request2ListWithView( callbackF: (view: View, requestData: RequestData, callback: CallbackList) -> Unit ) = object : Request2ListWithView { + override var request2List: Request2ListWithView? = this + override var view: View? = null + override var requestData: RequestData? = null + override var refreshCallback: CallbackList? = null + override var loadMoreCallback: CallbackList? = null override fun requestAndCallback(view: View, requestData: RequestData, callback: CallbackList) { + super.requestAndCallback(view, requestData, callback) callbackF.invoke(view, requestData, callback) } } \ No newline at end of file diff --git a/app/src/main/java/com/wpf/app/quick/demo/RefreshListTestActivity.kt b/app/src/main/java/com/wpf/app/quick/demo/RefreshListTestActivity.kt index 0bed8099..6f03c5a1 100644 --- a/app/src/main/java/com/wpf/app/quick/demo/RefreshListTestActivity.kt +++ b/app/src/main/java/com/wpf/app/quick/demo/RefreshListTestActivity.kt @@ -9,6 +9,7 @@ import com.wpf.app.quick.demo.http.request import com.wpf.app.quickrecyclerview.QuickRefreshRecyclerView import com.wpf.app.quickrecyclerview.helper.Request2RefreshView import com.wpf.app.quickrecyclerview.listeners.request2List +import kotlinx.coroutines.delay /** * Created by 王朋飞 on 2022/7/8. @@ -42,5 +43,10 @@ class RefreshListTestActivity : QuickActivity(R.layout.activity_refresh_list, ti mSmartRefreshLayout?.setOnRefreshListener { mRecyclerView?.onRefresh() } mSmartRefreshLayout?.setOnLoadMoreListener { mRecyclerView?.onLoadMore() } mSmartRefreshLayout?.autoRefresh() + + mSmartRefreshLayout?.postDelayed({ + request2List.requestData?.loadMore() + request2List.manualRun() + }, 2000) } } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index a0b48ff9..b5cc69cd 100755 --- a/gradle.properties +++ b/gradle.properties @@ -27,7 +27,7 @@ android.nonTransitiveRClass=true IS_REMOTE=true GROUP=com.wpf.app.quick -VERSION_NAME=0.5.4 +VERSION_NAME=0.5.5 POM_DESCRIPTION=Quick Android.