gpt4 book ai didi

xamarin - 用于嵌套 ListView 的 MvxExpandableTableViewSource

转载 作者:行者123 更新时间:2023-12-01 10:35:11 25 4
gpt4 key购买 nike

我试图在 MvvmCross-iOSSupport 4.1 中通过 MvxExpandableTableViewSource 制作一个嵌套的可扩展列表,但没有希望。请告诉我正确的方法!

最佳答案

我尝试使用 MvxExpandableTableViewSource但我在设置 _isCollapsed 时遇到问题 bool 数组。

我知道你必须继承 MvxExpandableTableViewSource并确保 ItemSource您绑定(bind)的类型是 IEnumerable<IEnumerable<object>>但仍然没有任何运气,可能错过了一些东西。

所以我创建了另一种类型的 TableViewSource基于 MvxExpandableTableViewSource所以我可以设置 _isCollapsed bool 数组时 ItemSource已绑定(bind),然后我需要绑定(bind)到组标题部分的标题。这是我想出的:

public abstract class MyExpTVS<TItemSource, TItem> : MvxTableViewSource where TItemSource : ItemGroup<TItem>
{
/// <summary>
/// Indicates which sections are expanded.
/// </summary>
private bool[] _isCollapsed;

private IEnumerable<TItemSource> _itemsSource;
new public IEnumerable<TItemSource> ItemsSource
{
get
{
return _itemsSource;
}
set
{
_itemsSource = value;
_isCollapsed = new bool[ItemsSource.Count()];

for (var i = 0; i < _isCollapsed.Length; i++)
_isCollapsed[i] = true;
ReloadTableData();
}
}

public MyExpTVS(UITableView tableView) : base(tableView)
{
}

protected override void CollectionChangedOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
// When the collection is changed collapse all sections
_isCollapsed = new bool[ItemsSource.Count()];

for (var i = 0; i < _isCollapsed.Length; i++)
_isCollapsed[i] = true;

base.CollectionChangedOnCollectionChanged(sender, args);
}

public override nint RowsInSection(UITableView tableview, nint section)
{
// If the section is not colapsed return the rows in that section otherwise return 0
if ( (ItemsSource?.ElementAt((int)section)).Items.Any() && !_isCollapsed [(int)section] )
return (ItemsSource.ElementAt((int)section)).Items.Count();
return 0;
}

public override nint NumberOfSections(UITableView tableView)
{
return ItemsSource.Count();
}

protected override object GetItemAt(NSIndexPath indexPath)
{
if (ItemsSource == null)
return null;

return ItemsSource.ElementAt(indexPath.Section).Items.ElementAt(indexPath.Row);
}

protected object GetHeaderItemAt(nint section)
{
if (ItemsSource == null)
return null;

return ItemsSource.ElementAt((int)section);
}

public override UIView GetViewForHeader(UITableView tableView, nint section)
{
var header = GetOrCreateHeaderCellFor(tableView, section);

// Create a button to make the header clickable
UIButton hiddenButton = new UIButton(header.Frame);
hiddenButton.TouchUpInside += EventHandler(tableView, section);
header.AddSubview(hiddenButton);

// Set the header data context
var bindable = header as IMvxDataConsumer;
if (bindable != null)
bindable.DataContext = GetHeaderItemAt(section);
return header;
}

private EventHandler EventHandler(UITableView tableView, nint section)
{
return (sender, e) =>
{
// Toggle the is collapsed
_isCollapsed[(int)section] = !_isCollapsed[(int)section];
tableView.ReloadData();

// Animate the section cells
var paths = new NSIndexPath[RowsInSection(tableView, section)];
for (int i = 0; i < paths.Length; i++)
{
paths[i] = NSIndexPath.FromItemSection(i, section);
}

tableView.ReloadRows(paths, UITableViewRowAnimation.Automatic);
};
}

public override void HeaderViewDisplayingEnded(UITableView tableView, UIView headerView, nint section)
{
var bindable = headerView as IMvxDataConsumer;
if (bindable != null)
bindable.DataContext = null;
}

/// <summary>
/// This is needed to show the header view. Should be overriden by sources that inherit from this.
/// </summary>
/// <param name="tableView"></param>
/// <param name="section"></param>
/// <returns></returns>
public override nfloat GetHeightForHeader(UITableView tableView, nint section)
{
return 44; // Default value.
}

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
return base.GetCell(tableView, indexPath);
}

/// <summary>
/// Gets the cell used for the header
/// </summary>
/// <param name="tableView"></param>
/// <param name="section"></param>
/// <returns></returns>
protected abstract UITableViewCell GetOrCreateHeaderCellFor(UITableView tableView, nint section);

protected abstract override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item);
}

我当时是这样使用它的:

public class BaseGroupedTableView : MvxTableViewController
{
public override void ViewDidLoad()
{
base.ViewDidLoad();

var source = new GroupTableSource(TableView)
{
UseAnimations = true,
AddAnimation = UITableViewRowAnimation.Left,
RemoveAnimation = UITableViewRowAnimation.Right
};

this.AddBindings(new Dictionary<object, string>
{
{source, "ItemsSource Kittens"}
});

TableView.Source = source;
TableView.ReloadData();
}
}

public class GroupTableSource : MyExpTVS<KittenType, Kitten>
{
public GroupTableSource(UITableView tableView) : base(tableView)
{
string nibName = "KittenCell";
this._cellIdentifier = new NSString(nibName);
tableView.RegisterNibForCellReuse(UINib.FromName(nibName, NSBundle.MainBundle), CellIdentifier);


string nibName2 = "HeaderCell";
this._headerCellIdentifier = new NSString(nibName2);
tableView.RegisterNibForCellReuse(UINib.FromName(nibName2, NSBundle.MainBundle), HeaderCellIdentifier);
}

public override nfloat GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
{
return 120f;
}


protected override UITableViewCell GetOrCreateHeaderCellFor(UITableView tableView, nint section)
{
return tableView.DequeueReusableCell(this.HeaderCellIdentifier);
}

protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
{
return tableView.DequeueReusableCell(this.CellIdentifier);
}

private readonly NSString _cellIdentifier;
protected virtual NSString CellIdentifier => this._cellIdentifier;

private readonly NSString _headerCellIdentifier;
protected virtual NSString HeaderCellIdentifier => this._headerCellIdentifier;
}

还有一些其他的部分:比如标题的单元格和我使用的模型。

所以我创建了 MvvmCross-Samples 的一个分支,并使用分组集合示例修改了“WorkingWithCollection”项目,它可以在这里找到:

https://github.com/b099l3/MvvmCross-Samples/tree/master/WorkingWithCollections

看起来像这样:

Grouped Kittehs!!!

希望 Grouped Kittehs 有所帮助。

更新

这已合并到 MvvmCross iOS samples here

关于xamarin - 用于嵌套 ListView 的 MvxExpandableTableViewSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36823389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com