Get visible items in RecyclerView

前端 未结 9 1950
礼貌的吻别
礼貌的吻别 2020-11-22 07:17

I need to know which elements are currently displayed in my RecyclerView. There is no equivalent to the OnScrollListener.onScroll(...) method on ListViews. I tried to work w

相关标签:
9条回答
  • 2020-11-22 07:56

    Addendum:

    The proposed functions findLast...Position() do not work correctly in a scenario with a collapsing toolbar while the toolbar is expanded.

    It seems that the recycler view has a fixed height, and while the toolbar is expanded, the recycler is moved down, partially out of the screen. As a consequence the results of the proposed functions are too high. Example: The last visible item is told to be #9, but in fact item #7 is the last one that is on screen.

    This behaviour is also the reason why my view often failed to scroll to the correct position, i.e. scrollToPosition() did not work correctly (I finally collapsed the toolbar programmatically).

    0 讨论(0)
  • 2020-11-22 07:58

    You can use recyclerView.getChildAt() to get each visible child, and setting some tag convertview.setTag(index) on these view in adapter code will help you to relate it with adapter data.

    0 讨论(0)
  • 2020-11-22 07:59

    for those who have a logic to be implemented inside the RecyclerView adapter you can still use @ernesto approach combined with an on scrollListener to get what you want as the RecyclerView is consulted. Inside the adapter you will have something like this:

    @Override
        public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
            RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
            if(manager instanceof LinearLayoutManager && getItemCount() > 0) {
                LinearLayoutManager llm = (LinearLayoutManager) manager;
                recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                    }
    
                    @Override
                    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                            int visiblePosition = llm.findFirstCompletelyVisibleItemPosition();
                            if(visiblePosition > -1) {
                                View v = llm.findViewByPosition(visiblePosition);
                                //do something
                                v.setBackgroundColor(Color.parseColor("#777777"));
                            }
                    }
                });
            }
        }
    
    0 讨论(0)
  • 2020-11-22 08:04

    Finally, I found a solution to know if the current item is visible, from the onBindViewHolder event in the adapter.

    The key is the method isViewPartiallyVisible from LayoutManager.

    In your adapter, you can get the LayoutManager from the RecyclerView, which you get as parameter from the onAttachedToRecyclerView event.

    0 讨论(0)
  • 2020-11-22 08:09

    Every answer above is correct and I would like to add also a snapshot from my working codes.

    recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
               //some code when initially scrollState changes
            }
    
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //Some code while the list is scrolling
                LinearLayoutManager lManager = (LinearLayoutManager) recycler.getLayoutManager();
                int firstElementPosition = lManager.findFirstVisibleItemPosition();
                
            }
        });
    
    0 讨论(0)
  • 2020-11-22 08:10

    For StaggeredGridLayoutManager do this:

    RecyclerView rv = findViewById(...);
    StaggeredGridLayoutManager lm = new StaggeredGridLayoutManager(...);
    rv.setLayoutManager(lm);
    

    And to get visible item views:

    int[] viewsIds = lm.findFirstCompletelyVisibleItemPositions(null);
    ViewHolder firstViewHolder = rvPlantios.findViewHolderForLayoutPosition(viewsIds[0]);
    View itemView = viewHolder.itemView;
    

    Remember to check if it is empty.

    0 讨论(0)
提交回复
热议问题