how to load more items in RecyclerView in .net android?

mc 5,426 Reputation points
2024-02-01T09:59:27.8466667+00:00

in .net android(not maui). how to use RecyclerView to load more items when I scroll down to bottom? the Layout is GridLayoutManager and there may be 5 columns in the view.

Developer technologies .NET .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-02-02T06:33:04.11+00:00

    Hello,

    You could implement this feature by listening to scrolling.

    For RecyclerView, DotNet Android is no different from Android native, so you need to refer to the Android native documentation.

    In .NET, you could implement a listener at the bottom of the scroll using the following code.

    recyclerView.AddOnScrollListener(new MyOnScrollingListener());
    
    public class MyOnScrollingListener : RecyclerView.OnScrollListener
    {
        public override void OnScrollStateChanged(RecyclerView recyclerView, int newState)
        {
            base.OnScrollStateChanged(recyclerView, newState);
            GridLayoutManager gm = (GridLayoutManager)recyclerView.GetLayoutManager();
            int totalItemCount = recyclerView.GetAdapter().ItemCount;
            int lastVisibleItemPosition = gm.FindLastVisibleItemPosition();  //Get the coordinates of the bottommost element
            int visibleItemCount = recyclerView.ChildCount;
    
            if (newState == RecyclerView.ScrollStateIdle
                    && lastVisibleItemPosition == totalItemCount - 1
                    && visibleItemCount > 0)
            {
                // load more
            }
        }
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.