【问题标题】:Scrolling to start of Xamarin Forms ListView with header滚动到带有标题的 Xamarin Forms ListView 的开头
【发布时间】:2018-09-24 18:51:45
【问题描述】:

我在 Xamarin Forms 中滚动到 ListView 顶部时遇到了一些问题。我可以通过调用 ScrollTo 并传递第一项来滚动到第一项。问题是当列表有标题项时,我找不到滚动到标题的方法。这可能吗?我能想到的唯一解决方法是不使用标题,而只是在 ItemSource 列表的开头有另一个项目作为标题,但如果可能的话,我宁愿使用标题。谢谢。

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    所以我现在自己解决了这个问题。我的解决方案是继承 ListView 并添加一个公共 ScrollToTop 方法,该方法在调用时调用内部 ScrollToTopRequestedEvent。然后,我将每个平台上的 ListViewRenderer 子类化并注册了该事件。

    然后我在 Android 渲染器中调用 Control.SmoothScrollToPositionFromTop(0, 0) 滚动到顶部。

    在渲染的 iOS 中,我调用 Control.ScrollRectToVisible(new CoreGraphics.CGRect(0, 0, 1, 1), true)。

    【讨论】:

    • 感谢 Gareth 让我走上正轨!我最终使用了一个动作而不是一个事件,但这完美无缺;)
    • 能否提供代码?我是 Xamarin 的 Android 开发人员和新手 :)
    【解决方案2】:

    哇,感谢@Gareth Wynn,真是太酷了。 无论如何,这是每个人都可以使用的代码,更改类名和命名空间,不包括 iOS,与 Android 一样,只是使用 Gareth Wynn 的提示并行回答:

    共享NiftyListView.cs

            using System;
            using Xamarin.Forms;
    
                namespace AppoMobi
                {
                    public class NiftyListView : CListView
                    {
                        public event EventHandler EventScrollToTop;
    
                        //-------------------------------------------------------------------
                        public void ScrollToTop(bool animate=true)
                        //-------------------------------------------------------------------
                        {
                            //bool animate is not used at this stage, it's always animated.
                            EventScrollToTop?.Invoke(this, EventArgs.Empty);
                        }
                    }
                }
    

    安卓NiftyListView.Android.cs

    using System;
    using AppoMobi;
    using AppoMobi.Droid.Renderers;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    using ListView = Xamarin.Forms.ListView;
    
    [assembly: ExportRenderer(typeof(NiftyListView), typeof(NiftyListViewRenderer))]
    namespace AppoMobi.Droid.Renderers
    {
        //-------------------------------------------------------------------
        class NiftyListViewRenderer : ListViewRenderer
        //-------------------------------------------------------------------
        {
            protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
            {
                base.OnElementChanged(e);
    
                var view = (NiftyListView)Element;
                if (view == null) return;
    
                view.EventScrollToTop += OnScrollToTop;
    
            }
    
            //-------------------------------------------------------------------------------------------
            public async void OnScrollToTop(object sender, EventArgs eventArgs)
            //-------------------------------------------------------------------------------------------
            {
                Control.SmoothScrollToPositionFromTop(0, 0);
            }
    
        }
    }
    

    【讨论】:

    • 嘿,很高兴您分享了代码。从记忆中,我认为 SmoothScroll 对于包含大量项目的列表非常慢。我把它换成了另一种方法,但我不记得它是什么了。我会看看我能不能把我的代码也挖出来。
    • @GarethWynn 对于 Android,我使用 Control.SetSelectionAfterHeaderView() 进行非平滑滚动。
    【解决方案3】:

    ScrollTo(Object, Object, ScrollToPosition, Boolean) 将 ListView 滚动到组中的项目 https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.listview.scrollto?view=xamarin-forms

    每个组的模型:

    public class Notification {
     public int Id { get; set; }
     public string Title { get; set; };
     public Notification(int id, string title) {
        Id = id;
        Title = title;
     }
    }
    

    ItemSource 的组模型:

    public class NotificationGroup: List<Notification> {
     public string Title { get; set; }
     public string ShortTitle { get; set; }
     public NotificationGroup(string title) {
        Title = title;
     }
    }
    

    示例数据和使用情况:

    //SAMPLE DATA
    var notifications = new ObservableCollection <NotificationGroup> {
     new NotificationGroup("Group-01") {
       new Notification(1, "Item-1"),
       new Notification(2, "Item-2")
     },
     new NotificationGroup("Group-02") {
       new Notification(3, "Item-3"),
       new Notification(4, "Item-4")
     }
    };
    YourListViewName.ItemSource = notifications;
    
    //USING
    YourListViewName.ScrollTo(notifications.First()[0], notifications.First(), ScrollToPosition.Start, true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-03
      • 2019-01-25
      • 1970-01-01
      • 2019-08-10
      • 2013-05-18
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多