简体   繁体   English

Android Kotlin recyclerview onScrollListener 不起作用

[英]Android Kotlin recyclerview onScrollListener not work

I want to implement recyclerview onScrollListener.我想实现 recyclerview onScrollListener。 Here is my code.这是我的代码。

MainActivity.kt:主活动.kt:

class MainActivity : AppCompatActivity() {

lateinit var restModel : RestModel
var apiResponseList : MutableList<ApiResponse> = arrayListOf()
lateinit var itemAdapter: ItemAdapter
var handler: Handler = Handler()
lateinit var layoutManager : LinearLayoutManager



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
    mainPresenter = MainPresenter(this)
    val currentDate = mainPresenter.convertDate()
    mainPresenter.makeACall(currentDate)
}

override fun assignResponseToRecyclerview(apiResponse: ApiResponse?) {
    var _layoutManager = LinearLayoutManager(this)
    rv_api_response.apply {
        layoutManager = _layoutManager
        apiResponseList.add(apiResponse!!)
        itemAdapter = ItemAdapter(apiResponseList)
        adapter = itemAdapter

        addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                Log.e("z","z");
            }
        })
    }
}
}

main_activity.xml main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.MainActivity">

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_api_response"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"/>

</androidx.core.widget.NestedScrollView>

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

Why isn't my recyclerview's onScrollListener called (I assume that because my Log text not shows up)?为什么我的 recyclerview 的 onScrollListener 没有被调用(我假设是因为我的日志文本没有显示)?

I probably need to add that my List items are not fullscreen, it's just one element, but I want to call on him this onScrollListener.我可能需要补充一点,我的列表项不是全屏的,它只是一个元素,但我想调用他这个 onScrollListener。

I skipped MVP architectural pattern complexity between classes, just essential code.我跳过了类之间的 MVP 架构模式复杂性,只是基本代码。 But if you need something more just ask.但如果你需要更多的东西,就问吧。

Thank you in advance!先感谢您!

Weird!!奇怪的!! It should have worked.它应该有效。 I just tried having a simple recycler view with similar code and everything worked for me.我只是尝试使用类似的代码创建一个简单的回收器视图,一切都对我有用。

One minor possible thing, I hope you are looking at the error part of logcat for your log.一件可能的小事,我希望您正在查看 logcat 日志的错误部分。 Since you have used Log.e .由于您使用过Log.e

Another possible thought could be to try adding the listener from onCreate provided your callback and listener attachment are not related.另一个可能的想法是尝试从onCreate添加侦听器,前提是您的回调和侦听器附件不相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM