android kotlin只需再加载一个元素即可解决recyclerview问题

px9o7tmv  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(279)

我只想用一个滚动手势加载一个元素。现在它就像一个滚动手势加载1个或几个新元素(取决于滚动手势的时间)。作为一个解决方案,我可以在短于500毫秒的时间内做这个手势,或者使这个postdayed的延迟更长,但我想有更好的解决方案。你知道怎么做吗?
这个应用程序是以mvp模式编写的。这是我的密码:
currencyfragmentlist.kt

private fun addScrollerListener() {
    rvItem.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(_rvItem: RecyclerView, newState: Int) {
            super.onScrollStateChanged(_rvItem, newState)
            Log.e("scroll", isLoading.toString())
            if (!isLoading) {
                if (!_rvItem.canScrollVertically(1)) {

                    loadMore()
                    isLoading = true
                }
            }
        }
    })
}

private fun loadMore() {
    showProgressDialog()
    var numberOfDays = mainPresenter.getNumberOfMinusDays()
    numberOfDays++
    mainPresenter.saveNumberOfMinusDaysIntoSp(numberOfDays)
    var dateMinusXDays = mainPresenter.currentDateMinusXDaysToStr(numberOfDays)

    val nextLimit = listSize + 1
    for (i in listSize until nextLimit) {

        if (mainPresenter.checkIfSuchDateExistsinSp(dateMinusXDays)) {
            Log.d("such date already exists in shared prefs", dateMinusXDays)
            handler.postDelayed({
                mainPresenter.processDateWithoutMakingACall(dateMinusXDays)
            }, 500)
        } else {     
            mainPresenter.makeACall(dateMinusXDays)
            Log.d("retrofit call made", dateMinusXDays)
        }
    }
    itemAdapter.notifyDataSetChanged()
}

override fun hideProgressDialog() {
    if (apiResponseList.size > 1) {
        apiResponseList.removeAt(apiResponseList.size - 1)
        listSize = apiResponseList.size
        itemAdapter.notifyItemRemoved(listSize)
    } else progress_bar.visibility = View.GONE

    isLoading = false
}

override fun assignResponseToRecyclerview(apiResponse: ApiResponse?) {
    rvItem.apply {
        layoutManager = _layoutManager
        apiResponseList.add(apiResponse!!)
        itemAdapter = activity?.let { ItemAdapter(apiResponseList, it) }!!
        adapter = itemAdapter
    }
    Log.e("assign", isLoading.toString())
}

mainpresenter.kt

override fun makeACall(date: String?) {
    //view.showProgressDialog()
    date?.let { restModel.fetchApiResponse(this, it) }
}

fun processDateWithoutMakingACall(date: String) {
    val apiResponse = processRawJson(sp.getString(date, "").toString())
    passResponseToView(apiResponse)
}

override fun processRawJson(rawJson: String): ApiResponse {
    val parser = JsonParser()
    val rootObj = parser.parse(rawJson).asJsonObject
    var ratesObj = JsonObject()
    var ratesKeys: Set<String> = HashSet()
    val ratesArrayList: ArrayList<Currency> = ArrayList()
    val rootKeys = rootObj.keySet();
    var baseValue = ""
    var dateValue = ""
    for (key in rootKeys) {
        if (key == "base")
            baseValue = rootObj.get(key).asString
        if (key == "date")
            dateValue = rootObj.get(key).asString
        if (key == "rates") {
            ratesObj = rootObj.get(key).asJsonObject
            ratesKeys = ratesObj.keySet()

        }
    }
    for (key2 in ratesKeys) {
        Log.e("ratesKey", key2)
        Log.e("ratesValue", ratesObj.get(key2).asFloat.toString())
        ratesArrayList.add(Currency(key2, ratesObj.get(key2).asFloat))
    }
    saveRawJsonIntoSp(rawJson, dateValue)
    return ApiResponse(baseValue, dateValue, ratesArrayList, false)
}

override fun passResponseToView(apiResponse: ApiResponse?) {
    view.hideProgressDialog()
    view.assignResponseToRecyclerview(apiResponse)
}

restmodel.kt

override fun fetchApiResponse(presenter: MainPresenter, date: String) {
    job = CoroutineScope(Dispatchers.IO).launch {
        val response = userService.getCurrenciesForDate(date)
        withContext(Dispatchers.Main) {
            if (response.isSuccessful) {
                val rawJson = response.body()
                val apiResponse = presenter.processRawJson(rawJson)
                presenter.passResponseToView(apiResponse)
           }
        }
    }
}

任何帮助都将不胜感激。提前谢谢你!

2q5ifsrm

2q5ifsrm1#

尝试使用snaphelper,它可能会减慢布局管理器进行更多回调并停止重载

相关问题