简体   繁体   English

"RecyclerView findLastCompletelyVisibleItemPosition 总是返回最后一个元素"

[英]RecyclerView findLastCompletelyVisibleItemPosition always returns last element

first question on StackOverflow! StackOverflow 上的第一个问题!

I have an endlessly scrolling<\/strong> RecyclerView, preceded by several other views, all within a NestedScrollView.我有一个无限滚动<\/strong>的 RecyclerView,前面有几个其他视图,都在 NestedScrollView 中。

Whenever I call findLastCompletelyVisibleItemPosition<\/code> on the RecyclerView's layout manager, I am given the position of the last element loaded in the RecyclerView...even if only the first one is visible on screen.每当我在 RecyclerView 的布局管理器上调用findLastCompletelyVisibleItemPosition<\/code>时,我都会得到 RecyclerView 中加载的最后一个元素的位置......即使屏幕上只有第一个元素可见。 I believe this is due to the fact that the RecyclerView is within a NestedScrollView -- the item is being displayed, however I'd have to scroll down to view it -- but I could definitely be wrong.我相信这是因为 RecyclerView 位于 NestedScrollView 内——该项目正在显示,但我必须向下滚动才能查看它——但我肯定是错的。

Initialization of RecyclerView in Fragment OnViewCreated : Fragment OnViewCreated 中 RecyclerView 的初始化:

    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    //... irrelevant code ... //

   photosRecyclerView = view.findViewById(R.id.profile_recyclerview_photos);
   // Add LayoutManager to RecyclerView.
   GridLayoutManager manager = new GridLayoutManager(getContext(), 2);
   photosRecyclerView.setLayoutManager(manager);
   // Smoothes scrolling from NestedScrollView.
   photosRecyclerView.setNestedScrollingEnabled(false);
   // Add adapter to RecyclerView.
   profileRecyclerViewAdapter = new ProfileRecyclerViewAdapter(getContext(), photoList);
   photosRecyclerView.setAdapter(profileRecyclerViewAdapter);

After some research -- with a lot of help coming from this answer , I realized that this behaviour is due to the fact that the RecyclerView height is set to wrap content.经过一些研究——从这个答案中得到了很多帮助,我意识到这种行为是由于 RecyclerView 高度设置为包装内容。 This causes it to load all of the items immediately and thus the last "completely visible" item is the last item in the RecyclerView.这会导致它立即加载所有项目,因此最后一个“完全可见”的项目是 RecyclerView 中的最后一个项目。

You can try using constraint layout and change the recyclerview height to 0dp .您可以尝试使用约束布局并将 recyclerview 高度更改为 0dp 。

It will solve your problem它会解决你的问题

I recently faced an issue with recycler view, tried every possible solution but still failed.我最近遇到了回收站视图的问题,尝试了所有可能的解决方案,但仍然失败。 So I came up with this lightweight solution.所以我想出了这个轻量级的解决方案。

binding.currencies.apply {
        adapter = currencyPagingAdapter
    }.addOnScrollListener(object : RecyclerView.OnScrollListener() {
        override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
            super.onScrollStateChanged(recyclerView, newState)

            val lm = recyclerView.layoutManager as LinearLayoutManager
            val lv = lm.getChildAt(lm.itemCount - 1)

            val scrollBounds = Rect()
            recyclerView.getHitRect(scrollBounds)

            if (lv != null) {
                if (lv.getLocalVisibleRect(scrollBounds)) {
                    Log.d("TAG", "onScrollStateChanged: visible")
                } else {
                    Log.d("TAG", "onScrollStateChanged: not visible")
                }

            }

        }
    })

Had the same problem, I fixed my problem by detecting when nestedScrollView is at the end!有同样的问题,我通过检测 nestedScrollView 何时结束来解决我的问题!

        nestedScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            if (nestedScrollView != null) {
                if (nestedScrollView.getChildAt(0).getBottom() <= (nestedScrollView.getHeight() + nestedScrollView.getScrollY())) {
                    //scroll view is at bottom
                    if (isAnythingToLoad && rowsArrayList.size() > 8) {
                            //bottom of list!
                            loadMore();
                    }
                }
            }
        }
    });

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

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