CommonRecyclerAdapter.kt 6.49 KB
Newer Older
1
package com.ydl.ydlcommon.adapter
konghaorui committed
2

YKai committed
3 4 5 6
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
konghaorui committed
7 8 9
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
10
import com.ydl.ydlcommon.view.BaseViewHolder
konghaorui committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54


/**
 * 支持添加head 和 foot的adapter
 *
 * 子类需复写bindData() 设置显示数据
 *
 * 设置加载更多回调监听
 *
 * Created by harvie on 2017/6/27 0027.
 */
class CommonRecyclerAdapter<T>(recyclerView: RecyclerView?, resLayout: Int) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var datas: ArrayList<T> = ArrayList()

    var layout : Int = resLayout

    var headView: View ?= null

    var footView: View ?=null

    var recyView : RecyclerView? = recyclerView

    private var visibleItemCount : Int = 0
    private var totalItemCount :Int = 0
    private var lastVisibleItem : Int =0
    /**
     * 当前滑动的状态
     */
    private var currentScrollState = 0

    private var layoutManagerType : LayoutManagerType =
        LayoutManagerType.LinearLayout


    //绑定数据的回调接口
    var callback : BindDataCallback<T>?=null

    //加载更多回调接口
    var loadmoreListener : OnLoadMoreLIstener? = null

    private fun setOnScrollListener(){

        recyView?.addOnScrollListener(object : RecyclerView.OnScrollListener(){
YKai committed
55
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
konghaorui committed
56
                super.onScrolled(recyclerView, dx, dy)
YKai committed
57
                val layoutManager = recyclerView.layoutManager
konghaorui committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

                if (layoutManager is LinearLayoutManager) {
                    layoutManagerType =
                        LayoutManagerType.LinearLayout
                } else if (layoutManager is GridLayoutManager) {
                    layoutManagerType =
                        LayoutManagerType.GridLayout
                } else if (layoutManager is StaggeredGridLayoutManager) {
                    layoutManagerType =
                        LayoutManagerType.StaggeredGridLayout
                } else {
                    throw RuntimeException(
                            "Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager")
                }

                when (layoutManagerType) {
                    LayoutManagerType.LinearLayout -> lastVisibleItem = (layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
                    LayoutManagerType.GridLayout -> lastVisibleItem = (layoutManager as GridLayoutManager).findLastVisibleItemPosition()
                    else -> {
                    }
                }
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                visibleItemCount = recyclerView.childCount
                if (recyclerView.layoutManager is LinearLayoutManager){
                    val laymanager : LinearLayoutManager = recyclerView.layoutManager as LinearLayoutManager

                    totalItemCount = laymanager.itemCount
                    lastVisibleItem = laymanager.findLastVisibleItemPosition()

                    currentScrollState = newState
                    if ((visibleItemCount > 0 && currentScrollState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem >= totalItemCount - 1)) {
                        //加载更多
                        loadmoreListener?.onLoadMore()
                    }
                }
            }
        })
    }

    //设置加载更多监听
    fun setOnLoadMoreListener(loadmoreListener : OnLoadMoreLIstener) : CommonRecyclerAdapter<T> {
        this.loadmoreListener = loadmoreListener
        //设置滑动监听
        setOnScrollListener()
        return this
    }

    //update data
    fun update(datas1: List<T>?){
        if (datas1!=null){
            this.datas.clear()
            this.datas.addAll(datas1)
            notifyDataSetChanged()
        }
    }

    //追加数据
    fun addDatas(datas1: List<T>){
        this.datas.addAll(datas1)
        notifyDataSetChanged()
    }

    //设置headView
    fun addHeadView(headView: View?) {
        this.headView = headView
    }

    //添加item到列表最后
    fun addItem(t :T){
        if (t==null)return
        datas.add(t)
        notifyDataSetChanged()
    }

    //添加item到制定位置
    fun addItem(index : Int,t:T){
        if (t==null)return
        datas.add(index,t)
        notifyDataSetChanged()
    }

    //设置footView
    fun addFootView(footView: View?) {
        this.footView = footView
    }

    //设置绑定数据回调接口
    fun setBindDataCallback(callback: BindDataCallback<T>) : CommonRecyclerAdapter<T> {
        this.callback = callback
        return this
    }

    val headViewCount: Int
        get() = if (headView == null) 0 else 1

    val footViewCount: Int
        get() = if (footView == null) 0 else 1

    override fun getItemCount(): Int {
        return datas.size + headViewCount + footViewCount
    }

    override fun getItemViewType(position: Int): Int {
        if (position < headViewCount) return HEAD
        if (position >= datas.size + headViewCount) return FOOT
        return NORMAL
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
170
        if (viewType == HEAD) return BaseViewHolder(
konghaorui committed
171 172
            headView
        )
173
        if (viewType == FOOT) return BaseViewHolder(
konghaorui committed
174 175 176
            footView
        )
        val view = LayoutInflater.from(viewGroup.context).inflate(layout, null)
177
        return BaseViewHolder(view)
konghaorui committed
178 179 180 181 182 183
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (getItemViewType(position) == HEAD) return
        if (getItemViewType(position) == FOOT) return
        //绑定数据
184
        callback?.bindDatabindData(holder as BaseViewHolder,datas[position-headViewCount],position-headViewCount)
konghaorui committed
185 186 187 188 189 190 191 192 193 194
    }

    companion object {
        val HEAD = 1
        val NORMAL = 2
        val FOOT = 3
    }

    interface BindDataCallback<T>{
        //绑定数据回调  需要子类设置此回调
195
        fun bindDatabindData(holder : BaseViewHolder, t : T, position: Int)
konghaorui committed
196 197 198 199 200 201 202 203 204 205 206 207
    }
    //加载更多监听
    interface OnLoadMoreLIstener{
        fun onLoadMore()
    }

    enum class LayoutManagerType {
        LinearLayout,
        StaggeredGridLayout,
        GridLayout
    }
}