Last active
March 18, 2024 22:13
-
-
Save martijn00/a45a238c5452a273e602 to your computer and use it in GitHub Desktop.
Load more / endless scroll for Xamarin RecyclerView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Thanks for your simple and efficient solution. Be blessed !
Hello thanks for your solution.
But i have not OnCreateView Method.
I want implement in MainActivity.
how to do this?
Works fine, but unfortunately it fires multiple times so you should adjust it so it only fires ones until the loading of data is done again.
this is more accurate,
public class EndlessRecyclerOnScrollListener : RecyclerView.OnScrollListener
{
public delegate void LoadMoreEventHandler(object sender, EventArgs e);
public event LoadMoreEventHandler LoadMoreEvent;
private LinearLayoutManager LayoutManager;
private int previousTotal = 0; // The total number of items in the dataset after the last load
private bool loading = true; // True if we are still waiting for the last set of data to load.
private bool animiFlag = true;
private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;
private int current_page = 1;
public EndlessRecyclerOnScrollListener(LinearLayoutManager layoutManager)
{
LayoutManager = layoutManager;
}
public override void OnScrolled(RecyclerView recyclerView, int dx, int dy)
{
base.OnScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.ChildCount;
totalItemCount = recyclerView.GetAdapter().ItemCount;
firstVisibleItem = LayoutManager.FindFirstVisibleItemPosition();
if (loading)
{
if (totalItemCount > previousTotal)
{
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold))
{
current_page++;
LoadMoreEvent(this, null);
loading = true;
}
/*if ((visibleItemCount + pastVisiblesItems) >= totalItemCount)
{
LoadMoreEvent(this, null);
}*/
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks it works you saved my life