这可以通过使用自定义渲染
iOS的自定义呈现
编辑iOS上SelectionStyle
属性来完成。
下面的示例将UITableViewCellSelectionStyle
设置为None
。
using System;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using ListViewSample.iOS;
[assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellItemSelectedCustomRenderer))]
namespace ListViewSample.iOS
{
public class ViewCellItemSelectedCustomRenderer : ViewCellRenderer
{
public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
{
var cell = base.GetCell(item, reusableCell, tv);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
return cell;
}
}
}
Android的自定义呈现
创建一个新的绘制,ViewCellBackground.xml
并将其保存到Resources
>drawable
文件夹:
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape android:shape="rectangle">
<!--Change the selected color by modifying this hex value-->
<solid android:color="#FFFFFF" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</selector>
为ViewCell
创建自定义呈现using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using ListViewSample.Droid;
[assembly: ExportRenderer(typeof(ViewCell), typeof(ViewCellItemSelectedCustomRenderer))]
namespace ListViewSample.Droid
{
public class ViewCellItemSelectedCustomRenderer : ViewCellRenderer
{
protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, Android.Views.ViewGroup parent, Android.Content.Context context)
{
var cell = base.GetCellCore(item, convertView, parent, context);
cell.SetBackgroundResource(Resource.Drawable.ViewCellBackground);
return cell;
}
}
}
编辑:删除不执行自定义渲染器
样品Xamarin.Forms的ListView应用
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace ListViewSample
{
public class CustomViewCell : ViewCell
{
public CustomViewCell()
{
View = new Label
{
Text = "Hello World"
};
}
}
public class ListViewContentPage : ContentPage
{
public ListViewContentPage()
{
var itemSourceList = new List<CustomViewCell>();
itemSourceList.Add(new CustomViewCell());
itemSourceList.Add(new CustomViewCell());
var listView = new ListView();
listView.ItemTemplate = new DataTemplate(typeof(CustomViewCell));
listView.ItemsSource = itemSourceList;
listView.SeparatorVisibility = SeparatorVisibility.None;
Content = listView;
}
}
public class App : Application
{
public App()
{
// The root page of your application
MainPage = new NavigationPage(new ListViewContentPage());
}
}
}
OnListViewTextCellTapped这是一个自定义的方法? –
是的 - 对不起!我只是更新了我的答案,以包含OnListViewTextCellTapped方法。 –
好的。使用listview时,我可以访问选定的项目,这是否适用于此? –