SmartRefreshLayout下拉加载上一页定位

/**
 * @author azu
 * @data 2022/11/04
 * @describe 加载上一页数据定位显示工具类
 * */
object ShowPrePageUtil {
    private val mHandler = Handler(Looper.getMainLooper())

    /**
     * @param recyclerView
     * @param itemHeight 条目高度
     * @param newItemSize 上一页新加条目个数
     * @param itemSize 条目总数
     * @param refreshHeaderHeight 加载header高度
     * */
    fun scrollLastHeader(
        recyclerView: RecyclerView,
        itemHeight: Int,
        newItemSize: Int,
        itemSize: Int,
        refreshHeaderHeight: Int
    ) {
        val recyclerViewHeight = recyclerView.height
        val fullyVisibleItemSize = recyclerViewHeight / itemHeight
        val lastItemVisibleHeight = recyclerViewHeight % itemHeight
        val slideDownHeight = if (lastItemVisibleHeight < itemHeight - refreshHeaderHeight) {
            itemHeight - lastItemVisibleHeight
        } else {
            lastItemVisibleHeight - (itemHeight - refreshHeaderHeight)
        }
        if (itemSize < newItemSize + fullyVisibleItemSize) {
            mHandler.post {
                recyclerView.scrollToPosition(itemSize - 1)
            }
        } else {
            recyclerView.scrollToPosition(newItemSize + fullyVisibleItemSize)
            mHandler.post {
                recyclerView.layoutManager?.offsetChildrenVertical(slideDownHeight)
            }
        }
    }
}

你可能感兴趣的:(SmartRefreshLayout下拉加载上一页定位)