我正在尝试将 iOS Xamarin 应用程序转换为 Android Xamarin。我在 Android 中寻找合适的类似方法时遇到了问题
这是我需要转换为 Android Xamarin 的 iOS Xamarin 代码
public class HomeViewDS : UICollectionViewDataSource
{
public HomeViewDS ()
{
}
public List<DBPlaylist> MyPlaylists{ get; set; }
public List<TweakSet> MyDiscoveries{ get; set; }
public event EventHandler ItemSelectedForPlaying;
public override nint GetItemsCount (UICollectionView collectionView, nint section)
{
if (MyPlaylists != null) {
return MyPlaylists.Count;
}
if (MyDiscoveries != null) {
return MyDiscoveries.Count;
}
return 0;
}
public override UICollectionViewCell GetCell (UICollectionView collectionView, Foundation.NSIndexPath indexPath)
{
CLLHomeItem v = collectionView.DequeueReusableCell (CLLHomeItem.Key, indexPath) as CLLHomeItem;
if (v == null)
v = CLLHomeItem.Create ();
if (MyPlaylists != null) {
v.MyItem = MyPlaylists [indexPath.Row];
}
if (MyDiscoveries != null) {
v.MyItem = MyDiscoveries [indexPath.Row];
}
v.ItemSelectedForPlaying -= V_ItemSelectedForPlaying;
v.ItemSelectedForPlaying += V_ItemSelectedForPlaying;
return v;
}
void V_ItemSelectedForPlaying (object sender, EventArgs e)
{
if (ItemSelectedForPlaying != null) {
ItemSelectedForPlaying (sender, null);
}
}
}
谁能给我一个将这段代码转换为 Android 的想法。如果有人能转换它,我将不胜感激。
提前致谢
最佳答案
Android 使用一个类似的概念,称为 Adapter
.与 iOS 上的数据源类似,它也实现了回收项目的方法。
所以大致可以翻译成:
public class MyAdapter : BaseAdapter
{
public List<DBPlaylist> MyPlaylists { get; set; }
public List<TweakSet> MyDiscoveries { get; set; }
public event EventHandler ItemSelectedForPlaying;
public override Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return 0;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view == convertView ?? CLLHomeItem.Create();
if (MyPlaylists != null)
{
view.MyItem = MyPlaylists[position];
}
if (MyDiscoveries != null)
{
view.MyItem = MyDiscoveries[position];
}
view.ItemSelectedForPlaying -= V_ItemSelectedForPlaying;
view.ItemSelectedForPlaying += V_ItemSelectedForPlaying;
return v;
}
public override int Count
{
get
{
if (MyPlaylists != null)
{
return MyPlaylists.Count;
}
if (MyDiscoveries != null)
{
return MyDiscoveries.Count;
}
return 0;
}
}
void V_ItemSelectedForPlaying(object sender, EventArgs e)
{
ItemSelectedForPlaying?.Invoke(sender, null);
}
}
然后您可以将它分配给您的 ListView
Adapter
属性。
关于android - 在 Android 中使用它的类似 UICollectionViewDataSource 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34173003/