如何为SfListView编写自定义渲染器以禁用滚动?
由于分组和其他可用功能,我正在使用SfListView。 但是,我需要在Android和iOS中禁用滚动列表视图。 如何为SfListView编写自定义渲染器? 对于Xamarin表单ListView,我只需扩展ListViewRenderer类并重写OnElementChanged方法 另外,如何导出自定义渲染器 例如,以下代码适用于Xamarin forms ListView,并进行了相应的修改:如何为SfListView编写自定义渲染器以禁用滚动?,listview,xamarin.ios,xamarin.android,syncfusion,Listview,Xamarin.ios,Xamarin.android,Syncfusion,由于分组和其他可用功能,我正在使用SfListView。 但是,我需要在Android和iOS中禁用滚动列表视图。 如何为SfListView编写自定义渲染器? 对于Xamarin表单ListView,我只需扩展ListViewRenderer类并重写OnElementChanged方法 另外,如何导出自定义渲染器 例如,以下代码适用于Xamarin forms ListView,并进行了相应的修改: [assembly: ExportRenderer(typeof(SfListViewWith
[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中,我们为您的要求“在ListView中启用/禁用滚动条可见性”提供了属性“IsScrollBarVisible”。无需为此创建渲染器。默认情况下,IsScrollBarVisible属性值为true。您可以启用/禁用特定平台的滚动条可见性,如下面的代码示例所示
<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表单中本机ScrollView呈现程序中的某些限制,您无法在运行时更改IsScrollBarVisible属性。只能在初始化SfListView时定义它 在scroller=Xamarin.Forms.ScrollViewtypeofSfListView.GetFieldscrollView,BindingFlags.NonPublic.GetValueelement;行中提供“null引用异常”;。即使将这一行更改为此处提到的一行:,我也会得到相同的errorscroller=Xamarin.Forms.ScrollViewtypeofSfListView.GetFieldScrolllView,BindingFlags.NonPublic | BindingFlags.Instance.GetValueelement;请你看一下这个问题: