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. 主DNS配置

    一,安装BIND [root@localhost ~]# yum install bind bind-chroot bind-utils Loaded plugins: product-id, sub ...

  2. 正则表达式匹配完整img标签php实现

    处理html富文本的时候,碰到批量处理img标签,要把img标签格式化,并且去除不用的代码,class,各种data-等,首先想到使用正则匹配,然后处理匹配到的img标签和参数,经过一番尝试终于搞定了 ...

  3. com.alibaba.fastjson.JSONObject学习

    JSONObject json = new JSONObject(); //设置json属性,可以是对象,数值 json.put("key",value); //获取json的普通 ...

  4. VMware12 安装 CentOS 6.5 64位

    前言:本人在配置Hadoop的过程中,需要搭建Cent OS 64 环境,借此,顺便将Cent OS 64 的安装在此记录,方便自己,也方便大家学习.本次是在VM12虚拟机中实现Cent OS 64 ...

  5. SWAP空间不足,如何进行添加

    交换分区值过小,可以有两种方法来增加swap分区,一种是将新的分区来作为swap,另一种是在磁盘中创建一个大的文件来作swap. 我们以文件扩展swap [root@xuegod63 ~]# cd / ...

  6. [SmartFoxServer入门]服务器安装

    安装SFS2X: SFS2X平台安装操作和步骤都很简单.我们建议先查看对系统的要求,然后根据你选择的操作系统按照指定的安装向导进行安装. 系统要求: SFS2X是一款支持所有主流操作系统,运行在JVM ...

  7. 【09_242】Valid Anagram

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

  8. MVC项目创建与项目结构介绍

    一.创建MVC项目 打开VS202,点击:文件—>新建—>项目—>Web—>Asp.Net MVC 4 Web应用程序 填好项目名称.解决方案名称和文件存放位置,然后点击确定, ...

  9. 6.6 Android 编译机制的变迁

    我们使用Java开发android,在编译打包APK文件时,会经过以下流程 Java编译器将应用中所有Java文件编译为class文件(JVM运行的是.class文件,而DVM是.dex文件) dx工 ...

  10. Zabbix_server.conf 的性能调优

    Zabbix安装完成后,模板里面有一个Template App Zabbix Server,添加到zabbix服务器里. 过个一两天,查看以下的图表(在Graphs里面). Zabbix cache ...