我有一个要求,用户应该能够在recycleview结束时加载更多数据,例如延迟加载。我可以在link的帮助下实现此功能,但每次我到达底部并加载更多数据,recycleview项目从顶部开始,我必须滚动到底部才能查看项目。这是我的代码:
private async void DisplayCurrentYearData()
{
//current year
try
{
if (oStaticVariables.bIsConnected == true)
{
CurrentYear.StartIndex = 1;
CurrentYear.EndIndex = 20;
var oActCurYrData = await oWebApiService.GetCurrentYearList(oStaticVariables.MembershipID);
//var oActCurYrData = await oWebApiService.GetMembersLastYearData(new Commom.Files.Models.Member() { MembershipID = Convert.ToInt32(oStaticVariables.MembershipID) });
if (oActCurYrData != null && oActCurYrData.Count != 0)
{
this.listItems = new List<DataType>();
foreach (Commom.Files.Models.YearlyData x in oActCurYrData.ToList())
{
LogbookGroupData g1 = new LogbookGroupData(x.EventDescription, x.ActivityDescription);
if (x.Status == "1")
{
LogbookEntryData e11 = new LogbookEntryData(x.FormattedDate, x.Points, "Submitted");
g1.items.Add(e11);
}
else if (x.Status == "2")
{
LogbookEntryData e11 = new LogbookEntryData(x.FormattedDate, x.Points, "Awarded");
g1.items.Add(e11);
}
else if (x.Status == "3")
{
LogbookEntryData e11 = new LogbookEntryData(x.FormattedDate, x.Points, "Declined");
g1.items.Add(e11);
}
else
{
LogbookEntryData e11 = new LogbookEntryData(x.FormattedDate, x.Points, "Forwarded To Vetting Team");
g1.items.Add(e11);
}
listItems.Add(g1);
}
oStaticVariables.MyActivityYearlyData = oActCurYrData;
if (Activity != null)
{
this.recyclerAdapterCY = new LogbookCurrentYearRecycleViewAdaptor(Activity, listItems);
this.recyclerAdapterCY.GroupClickCurrent += OnGroupClickCurrent;
this.mLayoutManager = new LinearLayoutManager(Activity);
var onScrollListener = new RecyclerViewOnScrollListenerCY(this.mLayoutManager);
onScrollListener.LoadMoreEvent += (object sender, EventArgs e) =>
{
//Load more stuff here
LoadMoreCurrentYearData();
};
recyclerViewCY.AddOnScrollListener(onScrollListener);
this.recyclerViewCY.SetLayoutManager(mLayoutManager);
this.recyclerViewCY.SetAdapter(recyclerAdapterCY);
}
}
else {
}
}
}
catch (Exception ex)
{
}
}
private async void LoadMoreCurrentYearData()
{
CurrentYear.IsLoading = true;
CurrentYear.StartIndex = CurrentYear.EndIndex + 1;
CurrentYear.EndIndex = CurrentYear.EndIndex + 5;
//current year
try
{
txtTotalPoints.Visibility = ViewStates.Gone;
if (oStaticVariables.bIsConnected == true)
{
ShowSpinner();
var oActCurYrData = await oWebApiService.GetCurrentYearList(oStaticVariables.MembershipID);
//var oActCurYrData = await oWebApiService.GetMembersLastYearData(new Commom.Files.Models.Member() { MembershipID = Convert.ToInt32(oStaticVariables.MembershipID) });
if (oActCurYrData != null && oActCurYrData.Count != 0)
{
this.listItemsNew = new List<DataType>();
foreach (Commom.Files.Models.YearlyData x in oActCurYrData.ToList())
{
LogbookGroupData g1 = new LogbookGroupData(x.EventDescription, x.ActivityDescription);
if (x.Status == "1")
{
LogbookEntryData e11 = new LogbookEntryData(x.FormattedDate, x.Points, "Submitted");
g1.items.Add(e11);
}
else if (x.Status == "2")
{
LogbookEntryData e11 = new LogbookEntryData(x.FormattedDate, x.Points, "Awarded");
g1.items.Add(e11);
}
else if (x.Status == "3")
{
LogbookEntryData e11 = new LogbookEntryData(x.FormattedDate, x.Points, "Declined");
g1.items.Add(e11);
}
else
{
LogbookEntryData e11 = new LogbookEntryData(x.FormattedDate, x.Points, "Forwarded To Vetting Team");
g1.items.Add(e11);
}
listItemsNew.Add(g1);
}
listItems.AddRange(listItemsNew);
//oStaticVariables.MyActivityYearlyData = oActCurYrData;
if (Activity != null)
{
this.recyclerAdapterCY = new LogbookCurrentYearRecycleViewAdaptor(Activity, listItems);
this.recyclerAdapterCY.GroupClickCurrent += OnGroupClickCurrent;
this.recyclerViewCY.SetAdapter(recyclerAdapterCY);
HideSpinner();
CurrentYear.IsLoading = false;
}
}
else
{
HideSpinner();
CurrentYear.IsLoading = false;
}
}
}
catch (Exception ex)
{
}
}
这是我的RecyclerView OnScrollListener类:
public class RecyclerViewOnScrollListenerCY : RecyclerView.OnScrollListener
{
public delegate void LoadMoreEventHandler(object sender, EventArgs e);
public event LoadMoreEventHandler LoadMoreEvent;
private LinearLayoutManager LayoutManager;
public RecyclerViewOnScrollListenerCY(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 && !CurrentYear.IsLoading)
{
LoadMoreEvent(this, null);
}
}
}
有人可以在代码中指出我的错误。请帮忙
根据您的描述,您希望每次都滚动并不将位置重置到顶部以进行回收。
最初我在数据更改时创建了一个新的适配器。适配器基类将自动清除调用.Clear()数据的内容。这会导致滚动视图每次都返回到顶部。
要避免使用.Clear(),您需要不创建适配器的新实例,只需调用Adapter.NotifyDataSetChanged(),每次数据刷新时滚动视图都不会刷新到顶部。
https://developer.xamarin.com/api/member/Android.Widget.BaseAdapter.NotifyDataSetChanged/