0

I'm working on Xamarin Android and I'm calling a method that is returning some result of a search (num of pages, Total count of items, etc, tested with a mock and works correctly).

I don't know how to implement an endless scrolling view in Xamarin. I saw some Java implementations but i don't know how to "translate" that into Xamarin.

Could you help me or give me some example? thanks in advance!

user3535054
  • 67
  • 1
  • 9

1 Answers1

2

If you are still after the solution of your question, it might help you to sort it out In your activity where you have implemented RecyclerView, you can add following lines of codes

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = base.OnCreateView(inflater, container, savedInstanceState);

        var recyclerView = view.FindViewById<RecyclerView>(Resource.Id.my_recycler_view);
        if (recyclerView != null)
        {
            recyclerView.HasFixedSize = true;

            var layoutManager = new LinearLayoutManager(Activity);

            var onScrollListener = new XamarinRecyclerViewOnScrollListener (layoutManager);
            onScrollListener.LoadMoreEvent += (object sender, EventArgs e) => {
                //Load more stuff here
            };

            recyclerView.AddOnScrollListener (onScrollListener);

            recyclerView.SetLayoutManager(layoutManager);
        }
        return view;
    }

and in XamarinRecyclerViewOnScrollListener.cs class implement the following lines of code

public class  XamarinRecyclerViewOnScrollListener : RecyclerView.OnScrollListener
{
    public delegate void LoadMoreEventHandler(object sender, EventArgs e);
    public event LoadMoreEventHandler LoadMoreEvent;

    private LinearLayoutManager LayoutManager;

    public XamarinRecyclerViewOnScrollListener (LinearLayoutManager layoutManager)
    {
        LayoutManager = layoutManager;
    }

    public override void OnScrolled (RecyclerView recyclerView, int dx, int dy)
    {
        base.OnScrolled (recyclerView, dx, dy);

        var visibleItemCount = recyclerView.ChildCount;
        var totalItemCount = recyclerView.GetAdapter().ItemCount;
        var pastVisiblesItems = LayoutManager.FindFirstVisibleItemPosition();

        if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
            LoadMoreEvent (this, null);
        }
    }
}

You can find the link click here

Good Luck.

Ishwor Khanal
  • 1,039
  • 14
  • 26