package com.gh.gamecenter.qa.article.detail

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnScrollListener
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.qa.comment.base.BaseCommentAdapter
import com.gh.gamecenter.qa.comment.base.BaseCommentViewModel

class ArticleDetailCommentListAdapter(
    context: Context,
    private val viewModel: BaseCommentViewModel,
    type: AdapterType,
    entrance: String
) : BaseCommentAdapter(context, viewModel, type, entrance) {

    private val onScrollListener = object: OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            val layoutManager = recyclerView.layoutManager
            if (layoutManager is LinearLayoutManager) {// 加载更多
                if (layoutManager.findLastVisibleItemPosition() == itemCount - 1
                    && RecyclerView.SCROLL_STATE_IDLE == newState
                ) {
                    viewModel.load(LoadType.NORMAL)
                }
            }
        }
    }
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        recyclerView.addOnScrollListener(onScrollListener)
    }

    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        recyclerView.removeOnScrollListener(onScrollListener)
    }
}