Xamarin表单:无需点击按钮即可加载更多项目

时间:2017-12-05 05:46:45

标签: xamarin.forms

逗人,

我正在实现在没有按钮的情况下向listview加载更多项目。

我的代码:

 public partial class DashBoardPage : ContentPage
  {
ObservableCollection<string> Items;
bool isLoading;

public DashBoardPage()
{
    InitializeComponent();

    Items = new ObservableCollection<string>();
    var listview = new ListView();

    listview.ItemsSource = Items;
    listview.ItemAppearing += (sender, e) =>
    {
        if (isLoading || Items.Count == 0)
            return;

        //hit bottom!
        if (e.Item.ToString() == Items[Items.Count - 1])
        {
            LoadItems();
        }
    };
    LoadItems();
}

public async void LoadItems()
{
    isLoading = true;
    HttpClient client = new HttpClient();
    var response = await client.GetAsync("My Url");
    string tweetJson = await response.Content.ReadAsStringAsync();

    UserTweetResponse userTweetResponse = new UserTweetResponse();
    if (tweetJson != "")
    {
        userTweetResponse = JsonConvert.DeserializeObject<UserTweetResponse>(tweetJson.ToString());
        foreach (var tweet in userTweetResponse.userTweetsList)
                    {
                        Items.Add(tweet.ToString());
                    }
    }
    ListView1.ItemsSource = userTweetResponse.userTweetsList;
    isLoading = false;
}
}

我引用以下链接并与LoadItems()中的for循环混淆: https://montemagno.com/load-more-items-at-end-of-listview-in/

for (int i = 0; i < 20; i++) 
 {
  Items.Add (string.Format("Item {0}", Items.Count));
 }

在我的情况下,我使用itemSource属性将响应绑定到列表视图。

我的模特课:

 public class UserTweetResponse
   {
      public List<UserTweetsList> userTweetsList { get; set; }
    }
public class UserTweetsList
{
    public int tweetId { get; set; }
    public string tweetData { get; set; }
    public string createdTime { get; set; }
    public int commentCount { get; set; }
    public int likeCount { get; set; }
    public int flagCount { get; set; }
    public string mediaUrl { get; set; }
    public string tweetUser { get; set; }
    public bool userLiked { get; set; }
    public bool userFlagged { get; set; }
    public bool isMediaUrlNull { get { return string.IsNullOrEmpty(mediaUrl); } }
}

我该如何解决这个问题? 提前致谢

2 个答案:

答案 0 :(得分:2)

您需要将新推文添加到Items集合

<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
    <execution>
        <id>auto-clean</id>
        <phase>initialize</phase>
        <goals>
            <goal>clean</goal>
        </goals>
    </execution>
</executions>

因为Observable集合是ListView项目源,所以新项目将出现在列表中。

答案 1 :(得分:-1)

Pasete整体:

using System;
using System.Diagnostics;
using System.Net.Http;
using Newtonsoft.Json;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Collections.ObjectModel;
using System.Threading.Tasks;

 namespace SmartTweet
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DashBoardPage : ContentPage
{
    int i = 1;
    bool loadMore;
    ObservableCollection<UserTweetsList> Items =new ObservableCollection<SmartTweet.UserTweetsList>();
    bool isLoading;
    UserTweetResponse userTweetResponse;
    public DashBoardPage()
    {
        InitializeComponent();



        ListView1.ItemAppearing += (sender, e) =>
        {
            if (isLoading || Items!=null &&  Items.Count == 0)
                return;

            //hit bottom
            if (userTweetResponse.userTweetsList!=null && userTweetResponse.userTweetsList.Count>0)
            {
                UserTweetsList item = userTweetResponse.userTweetsList[userTweetResponse.userTweetsList.Count - 1];
                if ((e.Item as UserTweetsList).tweetId.ToString() == item.tweetId.ToString())
                {
                    Debug.WriteLine("Enter bottom");
                    loadMore = true;
                    UserTweetsList();

                }
            }


        };

    }
    protected async override void OnAppearing()
    {
        await UserTweetsList();


        base.OnAppearing();
    }
    async void ButtonClicked(Object sender, EventArgs e)
    {
        loadMore = true;
       // await UserTweetsList();
    }

    public async Task UserTweetsList()
    {
        isLoading = true;
        if (loadMore)
        {
            i = i + 1;
        }
        int countInInt = i * 3;
        try
        {
            string applicationId = "1";
            string siteId = "5";
            string userId = "225";
            string ownedBy = "me";
            string period = "before";
            string count = countInInt.ToString();

            DateTime dt = DateTime.Now.ToLocalTime();
            string date = dt.Year.ToString() + "-" + dt.Month.ToString() + "-" + dt.Day.ToString() + " " + dt.Hour.ToString() + ":" + dt.Minute.ToString() + ":" + dt.Second.ToString() + "." + dt.Millisecond.ToString();

            HttpClient client = new HttpClient();
            var response = await client.GetAsync("web service url");
            string tweetJson = await response.Content.ReadAsStringAsync();
            Debug.WriteLine("tweetList:>" + tweetJson);

             userTweetResponse = new UserTweetResponse();
            if (tweetJson != "")
            {
                userTweetResponse = JsonConvert.DeserializeObject<UserTweetResponse>(tweetJson.ToString());
                foreach (var tweet in userTweetResponse.userTweetsList)
                {
                    if (!Items.Contains(tweet))
                    {
                        Items.Add(tweet);
                    }

                    //Items.Add(tweet.ToString());
                }
            }
            ListView1.ItemsSource = userTweetResponse.userTweetsList;
            isLoading = false;
            if (userTweetResponse.userTweetsList.Count == 0)
            {
                //loadMoreButton.IsVisible = false;
                blue_holo_circle.IsVisible = false;
                blueLine.IsVisible = false;
            }
            else
            {
                //loadMoreButton.IsVisible = true;
                blue_holo_circle.IsVisible = true;
                blueLine.IsVisible = true;
            }

        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
        }
    }
}
 }