1

我有一个Xamarin.Forms.ListView包含按日期分组的事件。有发生在未来的事件和发生在过去的事件。

用户希望在他们的屏幕上加载最接近当前日期的未来事件,这样他们就不需要手动向下滚动来查看它。

我有哪些选项可以Xamarin.Forms.ListView为 iOS 和 Android 用户完成此任务?

当前初始视图的屏幕截图

4

1 回答 1

3

我已经取得了一些进展。我可以通过创建一个 CustomListView 和一个 iOS 渲染来支持它,从而在 iOS 中实现我的目标。

在 Xamarin.Forms 中,您创建一个 CustomListView,然后在加载列表后调用ScrollToRow(item,section)手动滚动到您需要的行。

在 iOS 中,渲染器将方法映射到UITableView消息ScrollToRow(...);

对于 Android,我仍然需要创建渲染器,但我知道我需要映射到调用getListView().setSelection(...);getListView().smoothScrollToPosition(...);

我相信有一种更优雅的方法可以做到这一点,但现在它正在完成工作

来源: Common.CustomListView

using System;
using Xamarin.Forms;

namespace Common {
    public class CustomListView : ListView {

        public Action<int, int, bool> ScrollToRowDelegate { get; set; }

        public void ScrollToRow(int itemIndex, int sectionIndex = 0, bool animated = false) {
            if (ScrollToRowDelegate != null) {
                ScrollToRowDelegate (itemIndex, sectionIndex, animated);
            }
        }
    }
}

iOS 渲染器来源:YourApplication.iOS.Renderers.CustomListViewRenderer

using System;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;

using Common;

using MonoTouch.UIKit;
using MonoTouch.Foundation;

using YourApplication.iOS.Renderers;

[assembly: ExportRenderer (typeof(CustomListView), typeof(CustomListViewRenderer))]
namespace YourApplication.iOS.Renderers
{
    public class CustomListViewRenderer : ListViewRenderer
    {
        protected override void OnModelSet (VisualElement view) {
            base.OnModelSet (view);
            var listView = view as CustomListView;
            listView.ScrollToRowDelegate = (itemIndex, sectionIndex, animated) => {
                ScrollToRow(itemIndex, sectionIndex, animated);
            };
        }

        private void ScrollToRow(int itemIndex, int sectionIndex, bool animated) {
            var tableView = this.Control as UITableView;
            var indexPath = NSIndexPath.FromItemSection (itemIndex, sectionIndex);
            tableView.ScrollToRow (indexPath, UITableViewScrollPosition.Top, animated);
        }   
    }
}
于 2014-07-10T16:10:50.667 回答