Skip to content

Commit

Permalink
日常提交:2022-11-11.
Browse files Browse the repository at this point in the history
Signed-off-by: 王朋飞 <walgr1010>
  • Loading branch information
王朋飞 committed Nov 11, 2022
1 parent 26a26c6 commit e007a23
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,58 @@ open class QuickRefreshRecyclerView @JvmOverloads constructor(
@JvmField
var mRequestData = RequestData(0)

@JvmField
val refreshCallback = object : CallbackList<QuickItemData> {
override fun callback(data: List<QuickItemData>?) {
getQuickAdapter().mDataList?.clear()
getQuickAdapter().appendList(data)
mRequestData.loadDataSize(data?.size ?: 0)
if (mDataChangeListener?.refreshFinish() != true) {
adapter.notifyDataSetChanged()
}
}
}

@JvmField
val loadMoreCallback = object : CallbackList<QuickItemData> {
override fun callback(data: List<QuickItemData>?) {
appendList(data)
mRequestData.loadDataSize(data?.size ?: 0)
if (mDataChangeListener?.loadMoreFinish() != true) {
adapter.notifyItemRangeInserted(
size() - (data?.size ?: 0), (data?.size ?: 0)
)
}
}
}

private var mDataChangeListener: Request2ListWithView<out RequestData, out QuickItemData, QuickRefreshRecyclerView>? =
null

fun <Request : RequestData, Data : QuickItemData> setDataChangeListener(dataChangeListener: Request2ListWithView<Request, Data, QuickRefreshRecyclerView>) {
mDataChangeListener = dataChangeListener
(mDataChangeListener as? Request2ListWithView<RequestData, QuickItemData, QuickRefreshRecyclerView>)?.let {
it.requestData = mRequestData
it.refreshCallback = refreshCallback
it.loadMoreCallback = loadMoreCallback
}
}

override fun onRefresh() {
mRequestData.refresh()
(mDataChangeListener as? Request2ListWithView<RequestData, QuickItemData, QuickRefreshRecyclerView>)
?.requestAndCallback(this, mRequestData, object : CallbackList<QuickItemData> {
override fun callback(data: List<QuickItemData>?) {
setNewData(data)
mRequestData.loadDataSize(data?.size ?: 0)
adapter.notifyDataSetChanged()
}

})
mDataChangeListener?.refreshFinish()
refreshView?.onRefresh() ?: let {
mRequestData.refresh()
(mDataChangeListener as? Request2ListWithView<RequestData, QuickItemData, QuickRefreshRecyclerView>)
?.requestAndCallback(this, mRequestData, refreshCallback)
mDataChangeListener?.refreshFinish()
}
}

override fun onLoadMore() {
mRequestData.loadMore()
(mDataChangeListener as? Request2ListWithView<RequestData, QuickItemData, QuickRefreshRecyclerView>)
?.requestAndCallback(this, mRequestData, object : CallbackList<QuickItemData> {
override fun callback(data: List<QuickItemData>?) {
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<RequestData, QuickItemData, QuickRefreshRecyclerView>)
?.requestAndCallback(this, mRequestData, loadMoreCallback)
mDataChangeListener?.loadMoreFinish()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,43 @@ object Request2RefreshView :
RequestData()
}
val realData = data as Request2ListWithView<RequestData, QuickItemData, RefreshView>
realData.view = view
realData.requestData = requestData
val refreshCallback = object : CallbackList<QuickItemData> {
override fun callback(data: List<QuickItemData>?) {
quickAdapter.mDataList?.clear()
quickAdapter.appendList(data)
requestData.loadDataSize(data?.size ?: 0)
if (!realData.refreshFinish()) {
quickAdapter.notifyDataSetChanged()
}
}
}
realData.refreshCallback = refreshCallback
val loadMoreCallback = object : CallbackList<QuickItemData> {
override fun callback(data: List<QuickItemData>?) {
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<QuickItemData> {
override fun callback(data: List<QuickItemData>?) {
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<QuickItemData> {
override fun callback(data: List<QuickItemData>?) {
quickAdapter.appendList(data)
requestData.loadDataSize(data?.size ?: 0)
if (!realData.loadMoreFinish()) {
quickAdapter.notifyDataSetChanged()
}
}
})
realData.requestAndCallback(view, requestData, loadMoreCallback)
realData.loadMoreFinish()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -9,70 +10,169 @@ import com.wpf.app.quickutil.CallbackList
*
*/

interface Request2List<Request : RequestData, Data : QuickItemData>: Request2ListWithView<Request, Data, Any> {
interface Request2List<Request : RequestData, Data : QuickItemData> : Request2ListWithView<Request, Data, Any> {

/**
* 接口请求
*/
fun requestAndCallback(requestData: Request, callback: CallbackList<Data>)
@CallSuper
fun requestAndCallback(requestData: Request, callback: CallbackList<Data>) {
this.requestData = requestData
if (requestData.isRefresh) {
this.refreshCallback = callback
} else {
this.loadMoreCallback = callback
}
}

override fun requestAndCallback(view: Any, requestData: Request, callback: CallbackList<Data>) {
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<Request : RequestData, Data : QuickItemData, View> {

var request2List: Request2ListWithView<Request, Data, View>?
var view: View?
var requestData: Request?
var refreshCallback: CallbackList<Data>?
var loadMoreCallback: CallbackList<Data>?

/**
* 接口请求
*/
fun requestAndCallback(view: View, requestData: Request, callback: CallbackList<Data>)
@CallSuper
fun requestAndCallback(view: View, requestData: Request, callback: CallbackList<Data>) {
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 <Request : RequestData, Data : QuickItemData> requestData2List(
callbackF: (requestData: Request, callback: CallbackList<Data>) -> Unit
) = object : Request2List<Request, Data> {
override var request2List: Request2ListWithView<Request, Data, Any>? = this
override var view: Any? = null
override var requestData: Request? = null
override var refreshCallback: CallbackList<Data>? = null
override var loadMoreCallback: CallbackList<Data>? = null

override fun requestAndCallback(requestData: Request, callback: CallbackList<Data>) {
super.requestAndCallback(requestData, callback)
callbackF.invoke(requestData, callback)
}
}

fun <Request : RequestData, Data : QuickItemData, View> requestData2ListWithView(
callbackF: (view: View, requestData: Request, callback: CallbackList<Data>) -> Unit
) = object : Request2ListWithView<Request, Data, View> {
override var request2List: Request2ListWithView<Request, Data, View>? = this
override var view: View? = null
override var requestData: Request? = null
override var refreshCallback: CallbackList<Data>? = null
override var loadMoreCallback: CallbackList<Data>? = null

override fun requestAndCallback(view: View, requestData: Request, callback: CallbackList<Data>) {
super.requestAndCallback(view, requestData, callback)
callbackF.invoke(view, requestData, callback)
}
}

fun <Data : QuickItemData> request2List(
callbackF: (requestData: RequestData, callback: CallbackList<Data>) -> Unit
) = object : Request2List<RequestData, Data> {
override var request2List: Request2ListWithView<RequestData, Data, Any>? = this
override var view: Any? = null
override var requestData: RequestData? = null
override var refreshCallback: CallbackList<Data>? = null
override var loadMoreCallback: CallbackList<Data>? = null

override fun requestAndCallback(requestData: RequestData, callback: CallbackList<Data>) {
super.requestAndCallback(requestData, callback)
callbackF.invoke(requestData, callback)
}
}

fun <Data : QuickItemData, View> request2ListWithView(
callbackF: (view: View, requestData: RequestData, callback: CallbackList<Data>) -> Unit
) = object : Request2ListWithView<RequestData, Data, View> {
override var request2List: Request2ListWithView<RequestData, Data, View>? = this
override var view: View? = null
override var requestData: RequestData? = null
override var refreshCallback: CallbackList<Data>? = null
override var loadMoreCallback: CallbackList<Data>? = null

override fun requestAndCallback(view: View, requestData: RequestData, callback: CallbackList<Data>) {
super.requestAndCallback(view, requestData, callback)
callbackF.invoke(view, requestData, callback)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down

0 comments on commit e007a23

Please sign in to comment.