package com.xty.base.refresh

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.xty.base.adapter.BaseAdapter
import com.xty.common.R

class ListRefresh(var context: Context) {
      var recyclerView:RecyclerView?=null


    fun setListener( view: SmartRefreshLayout,recycle:RecyclerView,refresh:()->Unit){
        recyclerView=recycle
        //刷新
        view.setOnRefreshListener {
            view.finishRefresh(1000)
            refresh.invoke()
        }
        view.setEnableLoadMore(false)
        //加载更多
       /* view.setOnLoadMoreListener {
            view.finishLoadMore(1000)
        }*/
    }


    fun <T> setData(list:MutableList<T>,adapter:BaseAdapter<T>){
        var noneView=getNoneView(recyclerView!!)
        if(list.isNullOrEmpty()){
            adapter.setNewInstance(null)
            adapter.setEmptyView(noneView)
        }else{
            adapter.setNewInstance(list)
        }
    }


    private fun getNoneView(view:ViewGroup):View{
     return LayoutInflater.from(context).inflate(R.layout.view_none_data,view,false)

    }
}