android - 在 Android 中使用它的类似 UICollectionViewDataSource 方法?

标签 android ios xamarin xamarin.ios xamarin.android

我正在尝试将 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/

相关文章:

php - 如何将元素数组从 iOS 发送到 PHP Web 服务

c# - Xamarin - 我如何在类外使用列表

android - 通过Wifi Direct连接多个Android设备

android - 使用带有问号 (?) 参数的 sqldb rawQuery

java - 如何在谷歌地图中绘制gpx文件

java - 列表到字符串。安卓

ios - 快速转换: ERROR - CGPathAddArc

ios - 自动滚动到 UITableView 底部

Xamarin 表单时间选择器 24 小时

c# - Xamarin调试时,断点不停