From 885f07091a621d66d60529e986a381951c5b77e7 Mon Sep 17 00:00:00 2001 From: limuyang Date: Mon, 30 Dec 2019 14:32:48 +0800 Subject: [PATCH] fix #3009 --- .../library/adapter/base/BaseQuickAdapter.kt | 12 +++- .../adapter/base/diff/BrvahAsyncDiffer.kt | 4 +- .../adapter/base/loadmore/BaseLoadMoreView.kt | 57 ++++++++++++++----- .../adapter/base/module/LoadMoreModule.kt | 39 +++++++------ 4 files changed, 78 insertions(+), 34 deletions(-) diff --git a/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.kt b/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.kt index 445f584ae..3351f9cda 100644 --- a/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.kt +++ b/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.kt @@ -330,7 +330,11 @@ abstract class BaseQuickAdapter //Do not move position, need to change before LoadMoreView binding loadMoreModule?.autoLoadMore(position) when (holder.itemViewType) { - LOAD_MORE_VIEW -> loadMoreModule?.loadMoreView?.convert(holder, position) + LOAD_MORE_VIEW -> { + loadMoreModule?.let { + it.loadMoreView.convert(holder, position, it.loadMoreStatus) + } + } HEADER_VIEW, EMPTY_VIEW, FOOTER_VIEW -> return else -> convert(holder, data.getOrNull(position - getHeaderLayoutCount())) } @@ -346,7 +350,11 @@ abstract class BaseQuickAdapter //Do not move position, need to change before LoadMoreView binding loadMoreModule?.autoLoadMore(position) when (holder.itemViewType) { - LOAD_MORE_VIEW -> loadMoreModule?.loadMoreView?.convert(holder, position) + LOAD_MORE_VIEW -> { + loadMoreModule?.let { + it.loadMoreView.convert(holder, position, it.loadMoreStatus) + } + } HEADER_VIEW, EMPTY_VIEW, FOOTER_VIEW -> return else -> convert(holder, data.getOrNull(position - getHeaderLayoutCount()), payloads) } diff --git a/library/src/main/java/com/chad/library/adapter/base/diff/BrvahAsyncDiffer.kt b/library/src/main/java/com/chad/library/adapter/base/diff/BrvahAsyncDiffer.kt index c72c68ec3..d347a4681 100644 --- a/library/src/main/java/com/chad/library/adapter/base/diff/BrvahAsyncDiffer.kt +++ b/library/src/main/java/com/chad/library/adapter/base/diff/BrvahAsyncDiffer.kt @@ -11,7 +11,7 @@ import java.util.concurrent.Executor class BrvahAsyncDiffer(private val adapter: BaseQuickAdapter, private val config: BrvahAsyncDifferConfig) : DifferImp { - private var mUpdateCallback: ListUpdateCallback = BrvahListUpdateCallback(adapter) + private val mUpdateCallback: ListUpdateCallback = BrvahListUpdateCallback(adapter) private var mMainThreadExecutor: Executor private class MainThreadExecutor internal constructor() : Executor { @@ -29,7 +29,7 @@ class BrvahAsyncDiffer(private val adapter: BaseQuickAdapter, private val mListeners: MutableList> = CopyOnWriteArrayList() - var mMaxScheduledGeneration = 0 + private var mMaxScheduledGeneration = 0 @JvmOverloads fun submitList(newList: MutableList?, commitCallback: Runnable? = null) { diff --git a/library/src/main/java/com/chad/library/adapter/base/loadmore/BaseLoadMoreView.kt b/library/src/main/java/com/chad/library/adapter/base/loadmore/BaseLoadMoreView.kt index 141bb1884..99b55059d 100644 --- a/library/src/main/java/com/chad/library/adapter/base/loadmore/BaseLoadMoreView.kt +++ b/library/src/main/java/com/chad/library/adapter/base/loadmore/BaseLoadMoreView.kt @@ -8,49 +8,78 @@ import com.chad.library.adapter.base.viewholder.BaseViewHolder * * @author limuyang */ -abstract class BaseLoadMoreView { - - enum class Status { - Complete, Loading, Fail, End - } - var loadMoreStatus = Status.Complete - internal set +enum class LoadMoreStatus { + Complete, Loading, Fail, End +} - var isLoadEndMoreGone: Boolean = false - internal set +/** + * 继承此类,实行自定义loadMore视图 + */ +abstract class BaseLoadMoreView { + /** + * 根布局 + * @param parent ViewGroup + * @return View + */ abstract fun getRootView(parent: ViewGroup): View + /** + * 布局中的 加载更多视图 + * @param holder BaseViewHolder + * @return View + */ abstract fun getLoadingView(holder: BaseViewHolder): View + /** + * 布局中的 加载完成布局 + * @param holder BaseViewHolder + * @return View + */ abstract fun getLoadComplete(holder: BaseViewHolder): View + /** + * 布局中的 加载结束布局 + * @param holder BaseViewHolder + * @return View + */ abstract fun getLoadEndView(holder: BaseViewHolder): View + /** + * 布局中的 加载失败布局 + * @param holder BaseViewHolder + * @return View + */ abstract fun getLoadFailView(holder: BaseViewHolder): View - open fun convert(holder: BaseViewHolder, position: Int) { + /** + * 可重写此方式,实行自定义逻辑 + * @param holder BaseViewHolder + * @param position Int + * @param loadMoreStatus LoadMoreStatus + */ + open fun convert(holder: BaseViewHolder, position: Int, loadMoreStatus: LoadMoreStatus) { when (loadMoreStatus) { - Status.Complete -> { + LoadMoreStatus.Complete -> { getLoadingView(holder).isVisible(false) getLoadComplete(holder).isVisible(true) getLoadFailView(holder).isVisible(false) getLoadEndView(holder).isVisible(false) } - Status.Loading -> { + LoadMoreStatus.Loading -> { getLoadingView(holder).isVisible(true) getLoadComplete(holder).isVisible(false) getLoadFailView(holder).isVisible(false) getLoadEndView(holder).isVisible(false) } - Status.Fail -> { + LoadMoreStatus.Fail -> { getLoadingView(holder).isVisible(false) getLoadComplete(holder).isVisible(false) getLoadFailView(holder).isVisible(true) getLoadEndView(holder).isVisible(false) } - Status.End -> { + LoadMoreStatus.End -> { getLoadingView(holder).isVisible(false) getLoadComplete(holder).isVisible(false) getLoadFailView(holder).isVisible(false) diff --git a/library/src/main/java/com/chad/library/adapter/base/module/LoadMoreModule.kt b/library/src/main/java/com/chad/library/adapter/base/module/LoadMoreModule.kt index 495c84153..f0aaa72fe 100644 --- a/library/src/main/java/com/chad/library/adapter/base/module/LoadMoreModule.kt +++ b/library/src/main/java/com/chad/library/adapter/base/module/LoadMoreModule.kt @@ -6,6 +6,7 @@ import com.chad.library.adapter.base.BaseQuickAdapter import com.chad.library.adapter.base.listener.LoadMoreListenerImp import com.chad.library.adapter.base.listener.OnLoadMoreListener import com.chad.library.adapter.base.loadmore.BaseLoadMoreView +import com.chad.library.adapter.base.loadmore.LoadMoreStatus import com.chad.library.adapter.base.loadmore.SimpleLoadMoreView import com.chad.library.adapter.base.viewholder.BaseViewHolder @@ -39,6 +40,12 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, /** 不满一屏时,是否可以继续加载的标记位 */ private var mNextLoadEnable = true + var loadMoreStatus = LoadMoreStatus.Complete + private set + + var isLoadEndMoreGone: Boolean = false + private set + /** 设置加载更多布局 */ var loadMoreView = LoadMoreModuleConfig.defLoadMoreView /** 加载完成后是否允许点击 */ @@ -61,7 +68,7 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, */ val isLoading: Boolean get() { - return loadMoreView.loadMoreStatus == BaseLoadMoreView.Status.Loading + return loadMoreStatus == LoadMoreStatus.Loading } /** @@ -94,7 +101,7 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, } } else { if (newHasLoadMore) { - loadMoreView.loadMoreStatus = BaseLoadMoreView.Status.Complete + loadMoreStatus = LoadMoreStatus.Complete baseQuickAdapter.notifyItemInserted(loadMoreViewPosition) } } @@ -103,11 +110,11 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, internal fun setupViewHolder(viewHolder: BaseViewHolder) { viewHolder.itemView.setOnClickListener { - if (loadMoreView.loadMoreStatus == BaseLoadMoreView.Status.Fail) { + if (loadMoreStatus == LoadMoreStatus.Fail) { loadMoreToLoading() - } else if (loadMoreView.loadMoreStatus == BaseLoadMoreView.Status.Complete) { + } else if (loadMoreStatus == LoadMoreStatus.Complete) { loadMoreToLoading() - } else if (enableLoadMoreEndClick && loadMoreView.loadMoreStatus == BaseLoadMoreView.Status.End) { + } else if (enableLoadMoreEndClick && loadMoreStatus == LoadMoreStatus.End) { loadMoreToLoading() } } @@ -117,10 +124,10 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, * The notification starts the callback and loads more */ fun loadMoreToLoading() { - if (loadMoreView.loadMoreStatus == BaseLoadMoreView.Status.Loading) { + if (loadMoreStatus == LoadMoreStatus.Loading) { return } - loadMoreView.loadMoreStatus = BaseLoadMoreView.Status.Loading + loadMoreStatus = LoadMoreStatus.Loading baseQuickAdapter.notifyItemChanged(loadMoreViewPosition) invokeLoadMoreListener() } @@ -130,7 +137,7 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, if (mLoadMoreListener == null || !isEnableLoadMore) { return false } - if (loadMoreView.loadMoreStatus == BaseLoadMoreView.Status.End && loadMoreView.isLoadEndMoreGone) { + if (loadMoreStatus == LoadMoreStatus.End && isLoadEndMoreGone) { return false } return baseQuickAdapter.data.isNotEmpty() @@ -151,10 +158,10 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, if (position < baseQuickAdapter.itemCount - preLoadNumber) { return } - if (loadMoreView.loadMoreStatus != BaseLoadMoreView.Status.Complete) { + if (loadMoreStatus != LoadMoreStatus.Complete) { return } - if (loadMoreView.loadMoreStatus == BaseLoadMoreView.Status.Loading) { + if (loadMoreStatus == LoadMoreStatus.Loading) { return } if (!mNextLoadEnable) { @@ -168,7 +175,7 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, * 触发加载更多监听 */ private fun invokeLoadMoreListener() { - loadMoreView.loadMoreStatus = BaseLoadMoreView.Status.Loading + loadMoreStatus = LoadMoreStatus.Loading baseQuickAdapter.weakRecyclerView.get()?.let { it.post { mLoadMoreListener?.onLoadMore() } } ?: mLoadMoreListener?.onLoadMore() @@ -236,9 +243,9 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, return } // mNextLoadEnable = false - loadMoreView.isLoadEndMoreGone = gone + isLoadEndMoreGone = gone - loadMoreView.loadMoreStatus = BaseLoadMoreView.Status.End + loadMoreStatus = LoadMoreStatus.End if (gone) { baseQuickAdapter.notifyItemRemoved(loadMoreViewPosition) @@ -255,7 +262,7 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, return } // mNextLoadEnable = true - loadMoreView.loadMoreStatus = BaseLoadMoreView.Status.Complete + loadMoreStatus = LoadMoreStatus.Complete baseQuickAdapter.notifyItemChanged(loadMoreViewPosition) @@ -269,7 +276,7 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, if (!hasLoadMoreView()) { return } - loadMoreView.loadMoreStatus = BaseLoadMoreView.Status.Fail + loadMoreStatus = LoadMoreStatus.Fail baseQuickAdapter.notifyItemChanged(loadMoreViewPosition) } @@ -288,7 +295,7 @@ open class BaseLoadMoreModule(private val baseQuickAdapter: BaseQuickAdapter<*, internal fun reset() { if (mLoadMoreListener != null) { isEnableLoadMore = true - loadMoreView.loadMoreStatus = BaseLoadMoreView.Status.Complete + loadMoreStatus = LoadMoreStatus.Complete } } } \ No newline at end of file