Android 对RecycleView设置空视图

前言

有时候我们在使用RecycleView的时候需要在没有数据的时候设置emptyView。这里通过对RecycleView进行拓展来设置emptyView.本质上是通过有数据时候隐藏RecycleView显示emptyView,无数据时候通过隐藏emptyView显示RecycleView来处理的。本例参考腾讯Bugly的代码,但是示例代码有疏漏,所以这里给出原因及解决方式。

使用方式(kotlin)

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_F8F8F8">
			<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <com.custom.BaseRecycleView
                android:id="@+id/make_money_task_mine_rv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginHorizontal="@dimen/dp_15"
                android:visibility="visible"/>
       		</RelativeLayout>
</LinearLayout>

kotlin文件

recycleView.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL,false)
recycleView.emptyView = layoutInflater.inflate(R.layout.layout_empty_view,rootView,false)
recycleView.adapter = adapter

自定义RecycleView(kotlin)

/**
 * 自定义基础的RecycleView
 * 内部实现了空视图
 */
class BaseRecycleView : RecyclerView{
    var emptyView: View ?= null
        set(value) {
            field = value
//            (this.rootView as ViewGroup).addView(value) //加入主界面布局
            val rootViewGroup = this.parent as ViewGroup
            rootViewGroup.addView(value)
        }

    /**
     * 数据监测中心
     */
   private val adapterDataObserver = object :AdapterDataObserver(){
        override fun onChanged() {
            adapter?.run {
                if(itemCount == 0){
                    emptyView?.visibility = View.VISIBLE
                    this@BaseRecycleView.visibility = GONE;
                }else{
                    emptyView?.visibility = View.GONE
                    this@BaseRecycleView.visibility = VISIBLE
                }
            }

        }

        override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
            super.onItemRangeRemoved(positionStart, itemCount)
            onChanged()
        }

        override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
            super.onItemRangeMoved(fromPosition, toPosition, itemCount)
            onChanged()
        }

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

        override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
            super.onItemRangeChanged(positionStart, itemCount)
            onChanged()
        }

        override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
            super.onItemRangeChanged(positionStart, itemCount, payload)
            onChanged()
        }
    }

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun setAdapter(adapter: Adapter<*>?) {
        super.setAdapter(adapter)
        if (null == adapter) return
        adapter.registerAdapterDataObserver(adapterDataObserver);
        adapterDataObserver.onChanged();
    }
}

腾讯bugly中示例代码疏漏地方

在原代码中将emptyView添加进RecycleView的方式是通过如下方式

   (this.rootView as ViewGroup).addView(value) //加入主界面布局

这种方式只有在布局中只有RecycleView一个子控件的时候有效,如果在RecycleView外面还有一层的话,就无法达到这个效果了。因为this.rootView寻找的是布局的根布局,所以在根布局中添加emptyView后,再隐藏RecycleView的话,emptyView还是无法显示。所以需要将代码改为以下写法:

 val rootViewGroup = this.parent as ViewGroup
     rootViewGroup.addView(value)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_F8F8F8">
			<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <com.custom.BaseRecycleView
                android:id="@+id/make_money_task_mine_rv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginHorizontal="@dimen/dp_15"
                android:visibility="visible"/>
       		</RelativeLayout>
</LinearLayout>

引用

腾讯bugly博客园地址:https://www.cnblogs.com/bugly/p/6264751.html
Android之View.getRootView:https://www.cnblogs.com/linux007/p/5783115.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值