Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在片段中使用RecyclerView实现无止境列表_Android_Kotlin - Fatal编程技术网

Android 如何在片段中使用RecyclerView实现无止境列表

Android 如何在片段中使用RecyclerView实现无止境列表,android,kotlin,Android,Kotlin,当用户滚动到底部附近时,如何将更多内容加载到我的RecyclerView列表中 我已经看过了堆栈溢出页面,但是我仍然在努力将Kotlin答案中的代码实现到我的代码中 为了让大家更好地了解上下文id,这里展示了我是如何在android studio中创建片段的(通过单击片段(列表)并选择一个空片段完成的) 在onCreateView中,我有一段代码 view.list.addOnScrollListener(object : RecyclerView.OnScrollListener() {

当用户滚动到底部附近时,如何将更多内容加载到我的RecyclerView列表中

我已经看过了堆栈溢出页面,但是我仍然在努力将Kotlin答案中的代码实现到我的代码中

为了让大家更好地了解上下文id,这里展示了我是如何在android studio中创建片段的(通过单击片段(列表)并选择一个空片段完成的)

在onCreateView中,我有一段代码

view.list.addOnScrollListener(object : RecyclerView.OnScrollListener() {


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

                val contentHeight = view.list.height - (view.list.paddingTop + view.list.paddingBottom)

                val distanceToBottom = view.list.computeVerticalScrollRange() - view.list.computeVerticalScrollOffset() - contentHeight

                val metrics = DisplayMetrics()

                activity!!.windowManager.getDefaultDisplay().getMetrics(metrics)
                //val Measuredwidth = metrics.widthPixels
                val Measuredheight = metrics.heightPixels

                // everything looks like its in pixels. It is important that everything is one unit type to ensure accurate calculations
                // Once this is all finished i will ask a question in stack overflow on how to ensure its one unit type


                // using the screen height and timesing it by 2 to know when to load more data
                if (Measuredheight * 2 > distanceToBottom) {

                    Toast.makeText(view.list.context, "Scrolling $distanceToBottom, this is the screen height excluding the nav bar in pixels " + Measuredheight + " more content needs to be loaded", Toast.LENGTH_SHORT).show()

                    // Load more function to be placed here
                }


            }


        })
                adapter = UserAdapter()

                val itemViewModel = ViewModelProviders.of(view.list.context)
                    .get(UserViewModel::class.java)

                itemViewModel.userPagedList.observe(view.list.context, Observer {
                    adapter.submitList(it)
                })

                list.adapter = adapter
--------------------------编辑------------------

我一直在研究分页库,我遇到了一些问题,但是我仍然无法将代码放入片段中

我想我应该做的是在onCreateView中,我必须放置这段代码

view.list.addOnScrollListener(object : RecyclerView.OnScrollListener() {


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

                val contentHeight = view.list.height - (view.list.paddingTop + view.list.paddingBottom)

                val distanceToBottom = view.list.computeVerticalScrollRange() - view.list.computeVerticalScrollOffset() - contentHeight

                val metrics = DisplayMetrics()

                activity!!.windowManager.getDefaultDisplay().getMetrics(metrics)
                //val Measuredwidth = metrics.widthPixels
                val Measuredheight = metrics.heightPixels

                // everything looks like its in pixels. It is important that everything is one unit type to ensure accurate calculations
                // Once this is all finished i will ask a question in stack overflow on how to ensure its one unit type


                // using the screen height and timesing it by 2 to know when to load more data
                if (Measuredheight * 2 > distanceToBottom) {

                    Toast.makeText(view.list.context, "Scrolling $distanceToBottom, this is the screen height excluding the nav bar in pixels " + Measuredheight + " more content needs to be loaded", Toast.LENGTH_SHORT).show()

                    // Load more function to be placed here
                }


            }


        })
                adapter = UserAdapter()

                val itemViewModel = ViewModelProviders.of(view.list.context)
                    .get(UserViewModel::class.java)

                itemViewModel.userPagedList.observe(view.list.context, Observer {
                    adapter.submitList(it)
                })

                list.adapter = adapter

但是,我遇到了一些错误,有些代码似乎已被弃用。请有人给我看一下我需要使用的代码,以便在片段中有一个RecyclerView,这样我就可以接受他们的答案。

你要找的是一个名为“它是Android架构组件的一部分”的库


下面是第三页,请注意,第三页已退出

我建议对此进行分页。在这里,看看这个来实现无限列表。我一直在研究分页,发现了一个带有一些源代码的教程,但是,我似乎不知道如何将它放在一个片段中。分页3是alpha格式的?这是什么意思?我应该使用那个版本吗?如果你不习惯在应用程序中放入
alpha
,那么你可以同时使用Paging 2。我一直在研究Paging,发现了一个包含一些源代码的教程。然而,我似乎不知道如何将它放入片段中。