GithubHelp home page GithubHelp logo

Comments (2)

ve3344 avatar ve3344 commented on June 16, 2024

普通列表空布局

普通空布局其实很简单,就是监听数据为0就显示,不为0就隐藏。

fun <V : ViewBinding> RecyclerView.Adapter<*>.createEmptyStateAdapter(
    creator: LayoutCreator<V>
): BindingAdapter<Unit, V> {
    val emptyLayoutAdapter = SingleViewBindingAdapter(creator) {
    }
    registerAdapterDataObserver(object :RecyclerView.AdapterDataObserver(){
        override fun onChanged() {
            emptyLayoutAdapter.isVisible = itemCount == 0
        }

        override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
            onChanged()
        }

        override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
            onChanged()
        }
    })
    return emptyLayoutAdapter
}

使用的时候也是一样拼接上就行

dataAdapter.createEmptyStateAdapter(XxxBinding::inflate)+dataAdapter

之所以必须要分页,其实是分页处理反而复杂,所以就加上了分页状态的监听做的示例。不用分页也行的。
其他特殊场景也是一样的道理,比如2个列表的数量加起来是奇数才显示的布局(当然实际中可能很少遇到)
这个后续再补上文档吧~

是否必须要协程

这个问题分2种情况,

  1. 如果app不介意引入协程的情况,但是代码已经有原来的框架了,只是不想切换协程写法。
    利用LifecycleOwner 产生 LifecycleScope 可以很好进行转换,而suspend方法也可以通过类似以下方式转换成callback。
class LoadMoreDataCallbackFetcher<T, P : LoadMoreProgress>(private val onLoad: (progress: P, callback: LoadCallback<T>) -> Unit) :
    LoadMoreDataFetcher<T, P> {

    interface LoadCallback<T> {
        fun setCancelListener(cancelListener: OnCancelListener)
        fun onData(data: Collection<T>?)
        fun onError(error: Throwable)
    }

    override suspend fun fetch(progress: P) = suspendCancellableCoroutine { cont ->
        val callback = object : LoadCallback<T> {
            var cancelListener: OnCancelListener? = null
            override fun setCancelListener(cancelListener: OnCancelListener) {
                this.cancelListener = cancelListener
            }

            override fun onData(data: Collection<T>?) {
                cont.resume(data)
            }

            override fun onError(error: Throwable) {
                cont.resumeWithException(error)
            }
        }
        cont.invokeOnCancellation {
            callback.cancelListener?.onCancel()
        }
        onLoad(progress, callback)
    }
}
  1. 如果完全不想引入和使用协程,比如项目中已经有RxJava 或其他异步框架。
    可以按照loadmore 模块的方式复制一份进行对应类的替换。如suspend替换成Observable 对象

from bindingadapter.

stars-one avatar stars-one commented on June 16, 2024

尝试写你提供的recyclerview的扩展方法,但是LayoutCreator这个类是提示找不到的,这个类定为内部类

from bindingadapter.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.