博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A Xamarin.Forms Infinite Scrolling ListView
阅读量:5297 次
发布时间:2019-06-14

本文共 4662 字,大约阅读时间需要 15 分钟。

from:

The last few months have been crazy busy working on , 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  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:

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:

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:

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  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.

转载于:https://www.cnblogs.com/zjoch/p/6248951.html

你可能感兴趣的文章
Spring中事务传播行为
查看>>
HDU - 1042 - N! - JAVA
查看>>
1.4.1python下载网页(每天一更)
查看>>
Excel 2010导数据到SQL SERVER 2008
查看>>
单点登录的实现
查看>>
Java动态代理
查看>>
cisco 2811 Qos
查看>>
mongodb基础
查看>>
怎样打造一个分布式数据库
查看>>
配置流分类
查看>>
5.Windows应急响应:挖矿病毒
查看>>
检测光纤的同心性
查看>>
C#中的虚函数及继承关系
查看>>
[Leetcode 70] 82 Remove Duplicates from Sorted List II
查看>>
hdu1698 Just a Hook 【区间修改】(模板题)
查看>>
Android音频不能播放问题
查看>>
unfinished application
查看>>
设计模式 - Java中单例模式的6种写法及优缺点对比
查看>>
axios的配置项
查看>>
Excel中针对IP地址的排序方法
查看>>