如何为 SfListView 编写自定义渲染器以禁用滚动?
How can I write a custom renderer for SfListView to disable scrolling?
由于分组和其他可用功能,我正在使用 SfListView。
但是,我需要在 Android 和 iOS 中禁用滚动列表视图。
如何为 SfListView 编写自定义渲染器?
对于 Xamarin 表单 ListView,我可以只扩展 ListViewRenderer class 并覆盖 OnElementChanged 方法。
此外,如何导出自定义渲染器?
例如,以下代码适用于 Xamarin 表单 ListView(经过相应修改):
[assembly: ExportRenderer(typeof(SfListViewWithNoScroll), typeof(SfListViewWithNoScrollRenderer))]
namespace MyApp.Mobile.Droid.CustomRenderers
{
public class SfListViewWithNoScrollRenderer : //which class do I need to inherit?
{
protected override void OnElementChanged(ElementChangedEventArgs<SfListView> e)
{
//base.OnElementChanged(e);
//if (Control != null)
//{
// Control.VerticalScrollBarEnabled = false;
//}
//what do I write here?
}
}
}
这样做会影响 ListView 的滚动,但选择等交互仍然有效。您无法查看视口区域下方的项目。
[assembly: ExportRenderer(typeof(SfListView), typeof(CustomSfListViewRenderer))]
namespace XamarinSfListViewDemo.Droid
{
public class CustomSfListViewRenderer : ViewRenderer<SfListView,Android.Views.View>
{
private Xamarin.Forms.ScrollView scroller;
protected override void OnElementChanged(ElementChangedEventArgs<SfListView> e)
{
base.OnElementChanged(e);
var element = e.NewElement;
scroller = (Xamarin.Forms.ScrollView)typeof(SfListView).GetField("scrollView", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(element);
scroller.InputTransparent = true;
}
}
}
我没有在 iOS 中尝试过此代码,但 90% 应该可以。试一试,让我知道它是否有帮助!
在 SfListView 中,我们有 属性“IsScrollBarVisible”满足您的要求“Enable/disableScrollBar 在 ListView 中的可见性”。无需为此创建渲染器。默认情况下,IsScrollBarVisible 属性 值为 true。您可以 enable/disable 特定平台的滚动条可见性,如下面的代码示例。
<sync:SfListView x:Name="listView" ItemsSource="{Binding BookInfo}">
<sync:SfListView.IsScrollBarVisible>
<OnPlatform x:TypeArguments="x:Boolean">
<OnPlatform.Android>
<OnIdiom x:TypeArguments="x:Boolean" Phone="false" Tablet="false"/>
</OnPlatform.Android>
<OnPlatform.iOS>
<OnIdiom x:TypeArguments="x:Boolean" Phone="false" Tablet="false"/>
</OnPlatform.iOS>
<OnPlatform.WinPhone>
<OnIdiom x:TypeArguments="x:Boolean" Phone="true" Tablet="true"/>
</OnPlatform.WinPhone>
</OnPlatform>
</sync:SfListView.IsScrollBarVisible>
</sync:SfListView>
但它有一些限制。由于 Xamarin Forms 中本机 ScrollView 渲染器的一些限制,您无法在运行时更改 IsScrollBarVisible 属性。它只能在初始化 SfListView 时定义。
由于分组和其他可用功能,我正在使用 SfListView。 但是,我需要在 Android 和 iOS 中禁用滚动列表视图。 如何为 SfListView 编写自定义渲染器? 对于 Xamarin 表单 ListView,我可以只扩展 ListViewRenderer class 并覆盖 OnElementChanged 方法。
此外,如何导出自定义渲染器?
例如,以下代码适用于 Xamarin 表单 ListView(经过相应修改):
[assembly: ExportRenderer(typeof(SfListViewWithNoScroll), typeof(SfListViewWithNoScrollRenderer))]
namespace MyApp.Mobile.Droid.CustomRenderers
{
public class SfListViewWithNoScrollRenderer : //which class do I need to inherit?
{
protected override void OnElementChanged(ElementChangedEventArgs<SfListView> e)
{
//base.OnElementChanged(e);
//if (Control != null)
//{
// Control.VerticalScrollBarEnabled = false;
//}
//what do I write here?
}
}
}
这样做会影响 ListView 的滚动,但选择等交互仍然有效。您无法查看视口区域下方的项目。
[assembly: ExportRenderer(typeof(SfListView), typeof(CustomSfListViewRenderer))]
namespace XamarinSfListViewDemo.Droid
{
public class CustomSfListViewRenderer : ViewRenderer<SfListView,Android.Views.View>
{
private Xamarin.Forms.ScrollView scroller;
protected override void OnElementChanged(ElementChangedEventArgs<SfListView> e)
{
base.OnElementChanged(e);
var element = e.NewElement;
scroller = (Xamarin.Forms.ScrollView)typeof(SfListView).GetField("scrollView", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(element);
scroller.InputTransparent = true;
}
}
}
我没有在 iOS 中尝试过此代码,但 90% 应该可以。试一试,让我知道它是否有帮助!
在 SfListView 中,我们有 属性“IsScrollBarVisible”满足您的要求“Enable/disableScrollBar 在 ListView 中的可见性”。无需为此创建渲染器。默认情况下,IsScrollBarVisible 属性 值为 true。您可以 enable/disable 特定平台的滚动条可见性,如下面的代码示例。
<sync:SfListView x:Name="listView" ItemsSource="{Binding BookInfo}">
<sync:SfListView.IsScrollBarVisible>
<OnPlatform x:TypeArguments="x:Boolean">
<OnPlatform.Android>
<OnIdiom x:TypeArguments="x:Boolean" Phone="false" Tablet="false"/>
</OnPlatform.Android>
<OnPlatform.iOS>
<OnIdiom x:TypeArguments="x:Boolean" Phone="false" Tablet="false"/>
</OnPlatform.iOS>
<OnPlatform.WinPhone>
<OnIdiom x:TypeArguments="x:Boolean" Phone="true" Tablet="true"/>
</OnPlatform.WinPhone>
</OnPlatform>
</sync:SfListView.IsScrollBarVisible>
</sync:SfListView>
但它有一些限制。由于 Xamarin Forms 中本机 ScrollView 渲染器的一些限制,您无法在运行时更改 IsScrollBarVisible 属性。它只能在初始化 SfListView 时定义。