我正在尝试在Xamarin Android ListViewRenderer中轻触/滑动该项目。有人知道吗?这是我基本上所拥有的 - 在我刷卡/点击的情况下,我需要获取ListViewItem
public class MyListViewRenderer : ListViewRenderer
{
listener = new MyGestureListener();
detector= new GestureDetector(listener);
listener.OnTapped += HandleTapped;
}
private void HandleTapped(object sender, EventArgs e)
{
// how to get the tapped item here?
}
答案 0 :(得分:1)
您可以使用Custom Cells和查看单元格使用布局进行TapGestureRecognizer。
请检查示例代码。
public class Item
{
public int id { get;set; }
public string Name { get; set; }
public decimal Rate { get; set; }
}
public class ItemRepo
{
public ItemRepo()
{
CreateItem();
}
public ObservableCollection<Item> Items { get; set; }
public void CreateItem()
{
Items = new ObservableCollection<Item>();
Items.Add(new Item() {id=1, Name = "A-104",Rate = 50 });
Items.Add(new Item() {id=2, Name = "B-104", Rate = 55 });
Items.Add(new Item() {id=3, Name = "B-806", Rate = 70 });
Items.Add(new Item() {id=4, Name = "B-107", Rate = 95 });
Items.Add(new Item() {id=5, Name = "A-105", Rate = 80 });
Items.Add(new Item() {id=6, Name = "A-106", Rate = 72 });
Items.Add(new Item() {id=7, Name = "C-188", Rate = 32 });
Items.Add(new Item() {id=8, Name = "C-194", Rate = 58 });
Items.Add(new Item() {id=9, Name = "D-897", Rate = 88 });
Items.Add(new Item() {id=10, Name = "D-968", Rate = 60 });
}
}
ListView XML :检查堆栈布局TapGestureRecognizer和CommandParameter用于传递用于获取所选对象的id(key)。
<ContentPage.Content>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<ListView x:Name="ItemList">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" >
<StackLayout.GestureRecognizers >
<TapGestureRecognizer NumberOfTapsRequired="2" Tapped="TapGestureRecognizer_Tapped" CommandParameter="{Binding id}" />
</StackLayout.GestureRecognizers>
<Label x:Name="lblName" Text="{Binding Name}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
Listview代码
public partial class MainPage : ContentPage
{
ItemRepo ItemRepo = null;
public MainPage()
{
InitializeComponent();
ItemRepo = new ItemRepo();
ItemList.ItemsSource = ItemRepo.Items;
}
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
Item SelectedItem = ItemRepo.Items.FirstOrDefault(itm => itm.id == Convert.ToInt32(((TappedEventArgs)e).Parameter.ToString()));
if (SelectedItem != null)
{
DisplayAlert("Alert", "Name : " +SelectedItem.Name + " , Rate : " + SelectedItem.Rate, "OK");
}
}
}
答案 1 :(得分:1)
首先,您需要在PCL项目中使用 CustomList 和 CustomViewCell 。
<强> CustomViewCell(XML)强>
<ViewCell.View>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" x:Name="CellBaseLayout" >
<Label x:Name="lblName" Text="{Binding Name}" />
</StackLayout>
</ViewCell.View>
<强> CustomViewCell(.CS)强>
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class CustomViewCell : ViewCell
{
public delegate void CellTapedEvent(object source, EventArgs e);
public event CellTapedEvent CellTaped;
public CustomViewCell()
{
InitializeComponent();
tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += TapGestureRecognizer_Tapped;
CellBaseLayout.GestureRecognizers.Add(tapGestureRecognizer);
}
public static readonly BindableProperty NameProperty = BindableProperty.Create("Name", typeof(string), typeof(CustomViewCell), "");
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public int NumberOfTapsRequired { get { return tapGestureRecognizer.NumberOfTapsRequired; } set { tapGestureRecognizer.NumberOfTapsRequired = value; } }
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
CellTaped?.Invoke(this, e);
}
private TapGestureRecognizer tapGestureRecognizer;
public TapGestureRecognizer TapGestureRecognizer { get { return tapGestureRecognizer; } }
}
<强> CustomList 强>
public class CustomList : ListView
{
public delegate void CellItemTapedEvent(object source, EventArgs e);
public event CellItemTapedEvent CellItemTaped;
public CustomList() :base()
{
}
protected override void SetupContent(Cell content, int index)
{
base.SetupContent(content, index);
if(content is CustomViewCell)
{
CustomViewCell cell = (CustomViewCell)content;
cell.CellTaped += Cell_CellTaped;
}
}
private void Cell_CellTaped(object source, EventArgs e)
{
CellItemTaped?.Invoke(source, e);
}
protected override void UnhookContent(Cell content)
{
if (content is CustomViewCell)
{
CustomViewCell cell = (CustomViewCell)content;
cell.CellTaped -= Cell_CellTaped;
}
base.UnhookContent(content);
}
}
<强>的MainPage(XML)强>
<ContentPage.Content>
<StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<local:CustomList x:Name="ItemList">
<ListView.ItemTemplate>
<DataTemplate>
<local:CustomViewCell NumberOfTapsRequired="2" Name="{Binding Name}">
</local:CustomViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</local:CustomList>
</StackLayout>
</ContentPage.Content>
<强>的MainPage(的.cs)强>
public partial class MainPage : ContentPage
{
ItemRepo ItemRepo = null;
public MainPage()
{
InitializeComponent();
ItemRepo = new ItemRepo();
ItemList.ItemsSource = ItemRepo.Items;
}
}
<强> ListViewRendere 强>
[assembly: ExportRenderer(typeof(CustomList), typeof(MyListView))]
namespace ListViewRendererCross.Droid
{
public class MyListView : ListViewRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
// unsubscribe
if (e.NewElement is CustomList)
{
CustomList list = (CustomList)e.NewElement;
list.CellItemTaped -= List_CellItemTaped;
}
}
var th = this;
if (e.NewElement != null)
{
// subscribe
if(e.NewElement is CustomList)
{
CustomList list = (CustomList)e.NewElement;
list.CellItemTaped += List_CellItemTaped;
}
}
}
private void List_CellItemTaped(object source, EventArgs e)
{
//Hear you will get your Taped Item
Item SelectedItem = (Item)((CustomViewCell)source).BindingContext;
Android.App.AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.BaseActivity);
AlertDialog alert = dialog.Create();
alert.SetTitle("Alert");
alert.SetMessage("Name : " + SelectedItem.Name + " , Rate : " + SelectedItem.Rate);
alert.Show();
}
}
}