from:http://www.codenutz.com/lac09-xamarin-forms-infinite-scrolling-listview/

The last few months have been crazy busy working on ItsMonkie Solutions, whether it be website rework, defining the value proposition or outreach emails, I’ve had very little time and as such Codenutz.com and the Live App Challenge has suffered. I don’t want to bore you by writing about my work schedule here, but I will be producing articles here on Codenutz with greater consistency, but lower frequency.

Anyway I’ve been hitting Xamarin.Forms pretty hard over the last few weeks with some okay results. I have to be totally honest, its been a rocky ride – the documentation isn’t brilliant, there are bugs, the updates often introduce new bugs and the framework itself sometimes feels a little bit lacking, albeit constantly improving.

Xamarin.Forms Extensibility

The really great thing about Xamarin.Forms is that its pretty extensible, and adding features isn’t too complicated by adding custom renderers, and / or extending existing controls.

I thought the best way to demonstrate this was create a really trivial example

Xamarin.Forms Infinite ListView

To demonstrate a really basic example of this I created a simple infinite scrolling listview and demo project on GitHub:

https://github.com/mattwhetton/Codenutz.XF.InfiniteListView

The code is incredibly simple, and the solution contains demo projects for how to use it.

What does it do

One of the first things that I really wanted in Xamarin.Forms was an infinite scrolling ListView, i.e. a ListView control that could theoretically keep going and going, dynamically loading the content as it needed to. It also really had to be MVVM and XAML friendly – the MVVM pattern is incredibly useful for writing cleanly separated user interfaces, but thats for another day!

How does it work

I implemented this by simply sub classing the built in ListView and adding in a few hooks:

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class InfiniteListView : ListView
{
    public static readonly BindableProperty LoadMoreCommandProperty = BindableProperty.Create<InfiniteListView,ICommand>(bp => bp.LoadMoreCommand, default(ICommand));
 
    public ICommand LoadMoreCommand
    {
        get { return (ICommand) GetValue(LoadMoreCommandProperty); }
        set { SetValue(LoadMoreCommandProperty, value); }
    }
 
    public InfiniteListView()
    {
        ItemAppearing += InfiniteListView_ItemAppearing;
    }
 
    void InfiniteListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
    {
        var items = ItemsSource as IList;
 
        if (items != null && e.Item == items[items.Count - 1])
        {
            if(LoadMoreCommand != null && LoadMoreCommand.CanExecute(null))
                LoadMoreCommand.Execute(null);
        }
    }
}

The basic idea behind this control is to expose a bindable command called LoadMoreCommand. This command fires whenever the last item in the listview appears. This was achieved using the readily available ItemAppearing event.

How do you use it

The concept is pretty straight forward, you add an InfiniteListView to your page, bind up the ItemsSource to an ObservableCollection on your ViewModel and also a command that instructs your ViewModel to load more content. Here’s a snippet from the XAML in my sample project:

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:Controls="clr-namespace:Codenutz.XF.Controls;assembly=Codenutz.XF.Controls.InfiniteListView"
        x:Class="Codenutz.XF.InfiniteListView.Shared.View.InfiniteListViewSampleView"
        Title="{Binding Title}">
 
    <Controls:InfiniteListView
        ItemsSource="{Binding MarvelCharacters}"
        SelectedItem="{Binding SelectedCharacter}"
        LoadMoreCommand="{Binding LoadCharactersCommand}">
    </Controls:InfiniteListView>
    
    </ContentPage>

Here I’m simply binding the ItemsSource to a MarvelCharacters observable collection (yeah, its always comics!), and the LoadMoreCommand to a LoadCharacters command. Ignore the SelectedItem part – thats just for fun!

When the view model initially loads it fetches the first 50 characters from the repository (there are thousands hard coded in the sample). When the load more command is triggered the view model simply loads the next 50.

Demo

Here’s a really simple demonstration of what this looks like on an iPhone 6 simulator:

The example is trivial, but nicely represents how easy this kind of thing is to achieve with a little bit of work extending the Xamarin.Forms controls.

Wrapping up

The full source code for this example is available on Github so if you want more detail feel free to dig in.

I hope to bring a lot more on the Xamarin front, and will hopefully get some useful posts out. If you have any questions or suggestions about post topics, I’d love to hear them.

A Xamarin.Forms Infinite Scrolling ListView的更多相关文章

  1. Xamarin.Forms中的ListView的ItemTrapped事件与ItemSelected事件的区别

    今天对Xamarin.Forms中的ListView的两个事件(ItemTrapped和ItemSelected)做了小小的研究,发现有以下几点区别: 1.ItemTrapped事件会优先被触发. 2 ...

  2. xamarin.forms之实现ListView列表倒计时

    做商城类APP时经常会遇到抢购倒计时的功能,之前做小区宝iOS的时候也有类似的功能,想着参考iOS做的思路,自定义一个Cell,在Cell中每秒刷新一下控件的文本值,但使用xamarin.forms实 ...

  3. Xamarin.Forms 开发资源集合(复制)

    复制:https://www.cnblogs.com/mschen/p/10199997.html 收集整理了下 Xamarin.Forms 的学习参考资料,分享给大家,稍后会不断补充: UI样式 S ...

  4. Xamarin.Forms 开发资源集合

    收集整理了下 Xamarin.Forms 的学习参考资料,分享给大家,稍后会不断补充: UI样式 Snppts: Xamarin Forms UI Snippets. Prebuilt Templat ...

  5. Xamarin自定义布局系列——ListView的一个自定义实现ItemsControl(横向列表)

    在以前写UWP程序的时候,了解到在ListView或者ListBox这类的列表空间中,有一个叫做ItemsPannel的属性,它是所有列表中子元素实际的容器,如果要让列表进行横向排列,只需要在Xaml ...

  6. Xamarin.Forms: 无限滚动的ListView(懒加载方式)

    说明 在本博客中,学习如何在Xamarin.Forms应用程序中设计一个可扩展的无限滚动的ListView.这个无限滚动函数在默认的Xamarin.Forms不存在,因此我们需要为此添加插件.在这里我 ...

  7. Xamarin.Forms 简介

    An Introduction to Xamarin.Forms 来源:http://developer.xamarin.com/guides/cross-platform/xamarin-forms ...

  8. 老司机学新平台 - Xamarin Forms开发框架之MvvmCross插件精选

    在前两篇老司机学Xamarin系列中,简单介绍了Xamarin开发环境的搭建以及Prism和MvvmCross这两个开发框架.不同的框架,往往不仅仅使用不同的架构风格,同时社区活跃度不同,各种功能模块 ...

  9. Xamarin.Forms入门-使用 Xamarin.Forms 来创建跨平台的用户界面

    Xamarin.Forms 是一个跨平台的.基于原生控件的UI工具包,开发人员可以轻松的创建适用于 Android,iOS 以及 Windows Phone的用户界面.Xamarin.Forms 通过 ...

随机推荐

  1. 【摘】Chrome解决高版本Stable Beta扩展程序强制停用问题

    博客园插件遇到这个问题,下述摘自百度贴吧,原文地址http://tieba.baidu.com/p/3091171066 1]下载组策略模板 chrome.adm 2] Win+R gpedit.ms ...

  2. Browser默认书签加载过程

    Browser配置默认书签——string.xml中<string-array name="bookmarks" translatable="false" ...

  3. 水果项目第3集-asp.net web api开发入门

    app后台开发,可以用asp.net webservice技术. 也有一种重量级一点的叫WCF,也可以用来做app后台开发. 现在可以用asp.net web api来开发app后台. Asp.net ...

  4. 【09_242】Valid Anagram

    Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...

  5. Java写的爬虫的基本程序

    这是一个web搜索的基本程序,从命令行输入搜索条件(起始的URL.处理url的最大数.要搜索的字符串),它就会逐个对Internet上的URL进行实时搜索,查找并输出匹配搜索条件的页面. 这个程序的原 ...

  6. java servlet 代码样例 (demo)

    今天又搞了下jsp +servlet 的代码样例,感觉虽然搭了好多次,可是每次还是不记得那些参数,都要去网上搜索,索性自己把这次的简单demo给记录下来,供下次使用的时候直接复制吧. 这个web逻辑 ...

  7. openSUSE 13.1 Milestone 4 发布

    openSUSE 13.1 发布第四个里程碑版本,下载地址: openSUSE-Factory-KDE-Live-Build0652-x86_64.iso (949MB, MD5, torrent) ...

  8. NuGet v3 feed带来的惊喜

    估计有1个月了,在mac上编译dnx从来没有成功过,因为在安装nuget packages时连接myget.org总是超时. 今天在 ASP.NET 5 Beta5 Now Available 中得知 ...

  9. 降龙十八掌之一:(亢龙有悔)SQL Server Profiler和数据库引擎优化顾问

    简介 说到Sql的[性能工具]真是强大,SQL Server Profiler的中文意思是SQL Server事件探查,这个到底是做什么用的呢?我们都知道探查的意思大多是和监视有关,其实这个SQL S ...

  10. 如何把excel数据导入数据库

    这里介绍2种把excel数据导入oracle数据库的方法. 1. 在excel中生成sql语句. 1)在数据列的右侧,第一行的任何位置输入="insert into table(xx,yyy ...