如何在 Xamarin 中创建无限滚动 [英] How can I creat endless scrolling in Xamarin

查看:29
本文介绍了如何在 Xamarin 中创建无限滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个应用程序,我试图创建循环滚动,例如向下滚动时我得到:

... - 项目 4 - 项目 5 - 项目 1 - 项目 2 - 项目 3 - 项目 4 - 项目 5 - 项目 1 - 项目 2 - 项目 3 - ...

这是我尝试的图片

因此滚动将是无止境的,因为相同的项目将被重复.有人可以帮我吗?

解决方案

可以参考

So I have this app where im trying to creat circular scrolling, for exemple while scrolling down i get :

... - Item 4 - Item 5 - Item 1 - Item 2 - Item 3 - Item 4 - Item 5 - Item 1 - Item 2 - Item 3 - ...

this a picture of what Im trying

So the scrolling will be endless because the same items will be repeated. Can anyone help me with that ?

解决方案

you could refer to Infinite Scrolling

here is a simple sample :

install Nuget Xamarin.Forms.Extended.InfiniteScrolling

the page's axml :

<?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:scroll="clr-namespace:Xamarin.Forms.Extended;assembly=Xamarin.Forms.Extended.InfiniteScrolling"
         x:Class="InfiniteScrollingApp.SimplePage">

    <!-- a normal list view -->
    <ListView  CachingStrategy="RecycleElement" ItemsSource="{Binding Items}">

        <!-- the behavior that will enable infinite scrolling -->
        <ListView.Behaviors>
            <scroll:InfiniteScrollBehavior />
        </ListView.Behaviors>

        <!-- the row definition -->
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                        <Label Text="{Binding Name}" />
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>


    </ListView>

the ViewModel :

public class MyViewModel
{
    public InfiniteScrollCollection<MyData> Items { get; set; }

    public MyViewModel()
    {

        var items = new InfiniteScrollCollection<MyData>();
        MyData data1 = new MyData() { Name = "1" };
        MyData data2 = new MyData() { Name = "2" };
        MyData data3 = new MyData() { Name = "3" };
        MyData data4 = new MyData() { Name = "4" };
        MyData data5 = new MyData() { Name = "5" };
        MyData data6 = new MyData() { Name = "6" };
        MyData data7 = new MyData() { Name = "7" };
        MyData data8 = new MyData() { Name = "8" };
        MyData data9 = new MyData() { Name = "9" };
        MyData data10 = new MyData() { Name = "10" };
        MyData data11 = new MyData() { Name = "11" };
        MyData data12 = new MyData() { Name = "12" };
        items.Add(data1);
        items.Add(data2);
        items.Add(data3);
        items.Add(data4);
        items.Add(data5);
        items.Add(data6);
        items.Add(data7);
        items.Add(data8);
        items.Add(data9);
        items.Add(data10);
        items.Add(data11);

        items.Add(data12);
        Items = items;
        //var dataSource = new MyDataSource();

        Items = new InfiniteScrollCollection<MyData>
        {
            OnLoadMore = async () =>
            {

                return items;
            }
        };
        // load the initial data
        Items.LoadMoreAsync();
    }
}

the model :

public class MyData
{
    public string Name { set; get; }
}

the effect is below :

这篇关于如何在 Xamarin 中创建无限滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆