我正在尝试在 Xamarin 中创建一个无限表作为 Instagram 客户端。但是,当我滚动到屏幕底部时,它会加载回相同的第二页图像。它正常加载第一页,然后正常加载第二页,但是当我尝试加载第三页时,它再次加载第二页。我将如何解决这个问题?这就是我尝试加载它的方式:
Account instagram = enumerable.First ();
var instagramAccessToken = instagram.Properties ["access_token"].ToString ();
var request = new RestRequest { RootElement = "data", Resource = "/users/self/feed" };
request.AddParameter ("access_token", instagramAccessToken);
var client = new RestClient ("https://api.instagram.com/v1");
client.ExecuteAsync (request, response => {
var rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);
string nextURL = rootObject.pagination.next_url;
//// Create Next Client
var requestBack = new RestRequest ();
var clientBack = new RestClient (nextURL);
// GET Next Response
clientBack.ExecuteAsync (requestBack, responseBack => {
var rootObjectBack = JsonConvert.DeserializeObject<RootObject> (responseBack.Content);
table.InvokeOnMainThread (() => {
// Create list that contains newly attained JSON
List<Datum> instagramData = rootObjectBack.data;
// Create dummy list for other values
List<Datum> sloppy = null;
((TableSource<Datum>)table.Source).instaData.AddRange (instagramData);
table.ReloadData ();
});
});
});
这是我检测到用户在 TableView 底部的地方
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
NetworkCheck netCheck = new NetworkCheck ();
netCheck.runCheck ();
bool log = netCheck.anyLoggedIn ();
if (tableView.ContentSize.Height - UIScreen.MainScreen.Bounds.Height <= tableView.ContentOffset.Y) {
// this is the last row in the last section
Console.WriteLine ("~~~Bottom~~~");
FLDTRequest fldt = new FLDTRequest ();
fldt.getPastInstagramFeed (tableView);
}
var cell = tableView.DequeueReusableCell(InstagramCell.Key) as InstagramCell;
if (cell == null)
{
cell = new InstagramCell();
var views = NSBundle.MainBundle.LoadNib("InstagramCell", cell, null);
cell = Runtime.GetNSObject(views.ValueAt(0)) as InstagramCell;
}
Datum h = instaData [indexPath.Row];
cell.BindData(h);
return cell;
}
最佳答案
将回调放在一个单独的递归函数中,只要有 nexturl,它就会不断调用自己。不知道 instagram API,但这样的东西应该可以工作。
在 viewdidload 或其他东西中运行它(不是每次到达底部,而是一次)
nextUrl = null;
Account instagram = enumerable.First ();
var instagramAccessToken = instagram.Properties ["access_token"].ToString ();
var request = new RestRequest { RootElement = "data", Resource = "/users/self/feed" };
request.AddParameter ("access_token", instagramAccessToken);
var client = new RestClient ("https://api.instagram.com/v1");
client.ExecuteAsync (request, resp =>
{
PageRetrievedCallback(resp );
});
回调函数
将 nextUrl 存储为类值
string nextUrl = null;
public void PageRetrievedCallback(RestResponse response)
{
var rootObject = JsonConvert.DeserializeObject<RootObject> (response.Content);
nextURL = rootObject.pagination.next_url;
table.InvokeOnMainThread (() => {
// Create list that contains newly attained JSON
List<Datum> instagramData = rootObject.data;
// Create dummy list for other values
List<Datum> sloppy = null;
((TableSource<Datum>)table.Source).instaData.AddRange (instagramData);
table.ReloadData ();
});
}
当您检测到底部加载下一页时运行此命令:
if ( ! string.IsNullOrEmpty (nextURL) )
{
var requestBack = new RestRequest ();
var clientBack = new RestClient (nextURL);
clientBack.ExecuteAsync (requestBack, resp =>
{
PageRetrievedCallback(resp );
});
}