- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 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/
我有一个带有 CollectionView 的 ViewController,我想将 UICollectionViewDataSource 放在一个额外的类中,以便它可以被重用。 (我没有使用界面生成
我用 UICollectionView 创建了一个 UIView。 在 UIView 的接口(interface)声明中,我遵守了 UICollectionViewDataSource、UIColle
// // ViewController.swift // CollectionViewTry // // Created by Shivam Agrawal on
我正在开始一个包含 Collection View 的新项目,该项目将从 REST api 下载大量数据。我已将 Collection View 和数据源拆分为两个文件,但是当我运行该应用程序时,我得
我正在修补一个 CollectionView,它显示不断变化的数据模型中的项目。我有一个类将我的数据模型表示为 UICollectionViewDatasource。 在我的数据模型中,项目会被其他项
我已经将自定义数据源设置为 UICollectionView: let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout(
尽管我似乎能够在我的 View Controller 中将 dataSource 设置到 UICollectionView 上: - (void)setupCollectionView:(UIColl
我正在尝试实现一个 Collection View ,该 Collection View 作为 IBOutlet 连接到界面构建器中的 Collection View 。根据需要,我尝试将我为包含 C
当我尝试创建一个在 swift 中实现 UICollectionViewDataSource 的通用类时,它说我的类不符合协议(protocol)(有时 Xcode 崩溃)。 这是否意味着我们无法为
我正在尝试学习 RAC,我已经了解了如何使用 rac_signalForSelector 实现委托(delegate)方法。我正在使用 UICollectionView 来显示一些数据,并且我已经使用
在 UITableViewController 中,使用静态 UITableViewCells,我尝试创建一个可重用的网格,其中每行有一定数量的单元格,宽度和高度可变。 在 Identifying a
我正在尝试将 iOS Xamarin 应用程序转换为 Android Xamarin。我在 Android 中寻找合适的类似方法时遇到了问题 这是我需要转换为 Android Xamarin 的 iO
我用 swift 3.0 编写了一个演示,并用自定义 View 包装了一个 UICollectionView。延迟加载collecionView并将当前 View 设置为数据源,但在自定义 View
我正在使用内部有一个 UICollectionView 的 UITableViewCell。使用这种结构,一切都完美无缺: class PostCell: UITableViewCell, UICol
我有理论问题。 目前我的应用程序使用 UICollectionView 作为显示对象列表的方式。 UIViewController,包含 UICollectionView 作为 subview ,实现
这个问题在这里已经有了答案: Redundant conformance error message Swift 2 (3 个答案) 关闭 6 年前。 我正在学习这个 (http://www.ray
我有一个相当大的应用程序,它有很多 Collection View 。大多数 Collection View 对数据源和流布局委托(delegate)具有相同的实现(相同的大小、边距等)。我正在尝试创
我正在学习使用 UIPickerController 操作相机的教程。但是,在实现 UICollectionViewDatsaSource 时,我收到一条错误消息,指出 ViewController
我正在尝试为 UICollectionViewDelegate 和 UICollectionViewDataSource 实现一个单独的类。但是,我似乎总是需要访问数据模型才能计算某些委托(deleg
类型“Custom TableViewController”不符合协议(protocol)“UICollectionViewDataSource”。 我使用 Swift 创建了一个项目,并且有一个 U
我是一名优秀的程序员,十分优秀!