關於xamarin.forms 中 list 的loadmore

前言


           最近幾天在研究上拉加載啊,下拉刷新啊什麼的。然而坑爹的事情總是那麼多。在xamarin.forms中,list自帶的,並沒有上拉加載的這個屬性(難道當初他們封裝方法時,就不會想到數據多了會咋整嗎?)抱怨歸抱怨,問題總是要解決的。

既然沒有,那就自己寫一個嘍~

思路


我的思路是這樣的,

什麼是上拉刷新,那是不是就是說,在當前頁面,看到最後一個item的時候,我需要加載一些新的數據,那我是不是可以寫一個,只要出現了最後一個item我們就去刷新最新數據呢?

 1 public class InfiniteListView : ListView
 2     {
 3         public static readonly BindableProperty LoadMoreCommandProperty = BindableProperty.Create(nameof(LoadMoreCommand), typeof(ICommand), typeof(InfiniteListView));
 4         public static readonly BindableProperty CanLoadMoreProperty = BindableProperty.Create(nameof(CanLoadMore), typeof(bool), typeof(InfiniteListView), true);
 5 
 6         public ICommand LoadMoreCommand
 7         {
 8             get { return (ICommand)GetValue(LoadMoreCommandProperty); }
 9             set { SetValue(LoadMoreCommandProperty, value); }
10         }
11 
12         public bool CanLoadMore
13         {
14             get
15             {
16                 return (bool)GetValue(CanLoadMoreProperty);
17             }
18             set
19             {
20                 SetValue(CanLoadMoreProperty, value);
21             }
22         }
23 
24         public InfiniteListView()
25         {
26             ItemAppearing += InfiniteListView_ItemAppearing;
27         }
28 
29         private void InfiniteListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
30         {
31             if (!CanLoadMore)
32             {
33                 return;
34             }
35             var items = ItemsSource as IList;
36 
37             if (items != null && e.Item == items[items.Count - 1])
38             {
39                 if (LoadMoreCommand != null && LoadMoreCommand.CanExecute(null))
40                     LoadMoreCommand.Execute(null);
41             }
42         }
43     }

我們來繼承listview,來實現它的ItemAppearing方法,然後再方法裏一次次的判斷有沒有到最後一個item(說實話我不知道這對程序好不好,現在只是想到了實現),只要出現了最後一個item,就去執行綁定方法。

在頁面綁定中,需要

<ContentPage
             xmlns:common="clr-namespace:XXX.Common;assembly=XXX"
            >
  <common:InfiniteListView 
                               CanLoadMore="{Binding IsLoadMore}"
                               LoadMoreCommand="{Binding LoadMoreLogCommand}">

   </common:InfiniteListView>

So。。其實是不是很簡單?

簡單測試了一下,發現了一個問題。就是我的list數據,有分組,一個list裏面可能會有多個list,那最後一個item豈不是永遠都是最外層的數據了?我把方法簡單的改了一下

 1 private void GroupInfiniteListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
 2         {
 3             if (!CanLoadMore)
 4             {
 5                 return;
 6             }
 7             if (ItemsSource == null)
 8             {
 9                 return;
10             }
11             var items = ItemsSource as IList;
12             var itemSourceCouunt = items.Count;
13             var iindex = 0;
14             IList lastData = ItemsSource as IList;
15             do
16             {
17                 iindex++;
18                 var lastItem = (items[items.Count - iindex]) as IList;
19                 if (lastItem.Count > 0)
20                 {
21                     lastData = lastItem;
22                     continue;
23                 }
24             } while (itemSourceCouunt == iindex);
25             //foreach (var item in lastItem)
26             //{
27             //    if (item is IList)
28             //    {
29             //        var data = item as IList;
30             if (lastData.Count <= 0)
31             {
32                 return;
33             }
34             if (lastData != null && e.Item == lastData[lastData.Count - 1])
35             {
36                 if (LoadMoreCommand != null && LoadMoreCommand.CanExecute(null))
37                     LoadMoreCommand.Execute(null);
38                 //    }
39                 //}
40                 //continue;
41             }
42         }

 

爲了實現這個東西,其實費了我不少功夫(其實我真不知道xamarin.forms哪裏省事省時間了)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章