SwipeToLoadHelper.kt 5.19 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9 10 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 55 56 57 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
package com.yidianling.tests.home.widget

import android.support.v7.widget.RecyclerView
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.GridLayoutManager
import android.widget.AbsListView.OnScrollListener.SCROLL_STATE_IDLE
import com.yidianling.tests.home.adapter.AdapterWrapper
import com.yidianling.tests.home.config.ITestHomeConfig


/**
 * @author yuanwai
 * @描述:上拉加载更多帮助类
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/7/26
 */
class SwipeToLoadHelper(recyclerView: RecyclerView, private val mAdapterWrapper: AdapterWrapper) : RecyclerView.OnScrollListener() ,AdapterWrapper.FooterCallBack{

    private val mRecyclerView: RecyclerView? = null
    private val mLayoutManager: RecyclerView.LayoutManager
    private var mListener: LoadMoreListener? = null
    /** 是否正在加载中  */
    private var mLoading = false
    /** 上拉刷新功能是否开启  */
    private var mIsSwipeToLoadEnabled = true

    init {
        mLayoutManager = recyclerView.layoutManager

        if (mLayoutManager is GridLayoutManager) {
            mAdapterWrapper.setAdapterType(AdapterWrapper.ADAPTER_TYPE_GRID)
            mAdapterWrapper.setSpanCount(mLayoutManager.spanCount)
        } else if (mLayoutManager is LinearLayoutManager) {
            mAdapterWrapper.setAdapterType(AdapterWrapper.ADAPTER_TYPE_LINEAR)
        }
        mAdapterWrapper.setFooterCallBack(this)
        // 将OnScrollListener设置RecyclerView
        recyclerView.addOnScrollListener(this)
    }

    override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
        if (mIsSwipeToLoadEnabled && SCROLL_STATE_IDLE === newState && !mLoading) {
            if (mLayoutManager is GridLayoutManager) {
                val gridLayoutManager = mLayoutManager
                gridLayoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                    override fun getSpanSize(position: Int): Int {
                        return if (mIsSwipeToLoadEnabled) {
                            // 功能开启, 根据位置判断, 最后一个item时返回整个宽度, 其他位置返回1
                            // AdapterWrapper会保证最后一个item会从新的一行开始
                            if (position == mLayoutManager.getItemCount() - 1) {
                                gridLayoutManager.spanCount
                            } else {
                                1
                            }
                        } else {
                            1
                        }
                    }
                }
            }

            if (mLayoutManager is LinearLayoutManager) {
                val linearLayoutManager = mLayoutManager
                val lastCompletePosition = linearLayoutManager.findLastCompletelyVisibleItemPosition()
                // only when the complete visible item is second last
                if (lastCompletePosition == mLayoutManager.getItemCount() - 2) {
                    val firstCompletePosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition()
                    val child = linearLayoutManager.findViewByPosition(lastCompletePosition)
                            ?: return
                    val deltaY = recyclerView!!.bottom - recyclerView.paddingBottom - child.bottom
                    if (deltaY > 0 && firstCompletePosition != 0) {
                        recyclerView.smoothScrollBy(0, -deltaY)
                    }
                } else if (lastCompletePosition == mLayoutManager.getItemCount() - 1) {
                    // 最后一项完全显示, 触发操作, 执行加载更多操作 禁用回弹判断
                    mLoading = true
                    mAdapterWrapper.setLoadItemState(ITestHomeConfig.STATUS_LOADING)
                    if (mListener != null) {
                        mListener!!.onLoad()
                    }
                }
            }
        }
    }

    override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
        super.onScrolled(recyclerView, dx, dy)
    }

    /** 设置下拉刷新功能是否开启  */
    fun setSwipeToLoadEnabled(isSwipeToLoadEnabled: Boolean) {
        if (mIsSwipeToLoadEnabled != isSwipeToLoadEnabled) {
            mIsSwipeToLoadEnabled = isSwipeToLoadEnabled
            mAdapterWrapper.setLoadItemVisibility(isSwipeToLoadEnabled)
        }
    }

    /** 设置LoadMore Item为加载完成状态, 上拉加载更多完成时调用  */
    fun setLoadMoreFinish() {
        mLoading = false
        mAdapterWrapper.setLoadItemState(ITestHomeConfig.STATUS_UP_LOAD)
    }

    /**
     * 设置LoadMore Item为没有更多数据状态 显示查看全部测评
     */
    fun setNoMoreData(){
        mLoading = false
        mAdapterWrapper.setLoadItemState(ITestHomeConfig.STATUS_SELECT_ALL)
    }

    override fun selectAll() {
        mListener!!.selectAll()
    }

    /** 上拉操作触发时调用的接口  */
    fun setLoadMoreListener(loadMoreListener: LoadMoreListener) {
        mListener = loadMoreListener
    }

    interface LoadMoreListener {
        /**
         * 加载更多
         */
        fun onLoad()
        /**
         * 查看全部测评
         */
        fun selectAll()
    }
}