我有一个要求,即当达到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)
{
}
}
这是我的RecyclerViewOnScrollListenerCY类:
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);
}
}
}
有人可以指出我在代码中的错误吗?请帮助
答案 0 :(得分:0)
根据您的描述,您想滚动而不是将位置重置到顶部,以便每次都可以进行recycleview。
最初,当数据更改时,我正在创建一个新的适配器。适配器基类将自动清除调用.Clear()的数据的内容。这会导致滚动视图每次都转到顶部。
为避免使用.Clear(),您无需创建适配器的新实例,而只需调用Adapter.NotifyDataSetChanged(),并且每次数据刷新时滚动视图都不会刷新到顶部。
https://developer.xamarin.com/api/member/Android.Widget.BaseAdapter.NotifyDataSetChanged/