【问题标题】:How can a scroll a specific row or section to the top of a Xamarin.Forms.ListView如何将特定行或部分滚动到 Xamarin.Forms.ListView 的顶部
【发布时间】:2014-08-29 04:43:44
【问题描述】:

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

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

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

【问题讨论】:

  • 我猜 Xamarin.Forms API 中可能还不支持滚动到项目。这不是一个明确的答案,但从我迄今为止对 API 所做的探索来看,这似乎是未来版本的内容。不要引用我的话。
  • 如何在每一行获得右尖括号按钮?

标签: xamarin xamarin.forms


【解决方案1】:

我已经取得了一些进展。我可以通过创建一个 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);
        }   
    }
}

【讨论】:

  • 我想知道,使用SelectedItem 属性有一种快速而肮脏的方法。见这里:link
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-02
相关资源
最近更新 更多