gpt4 book ai didi

listview - iOS 上的无限 ListView 刷新后跳转到第一项

转载 作者:行者123 更新时间:2023-12-02 19:27:59 24 4
gpt4 key购买 nike

我已经在 ObservableRangeCollection 的帮助下创建了具有无限滚动功能的 ListView。它在 Android 上运行良好,但在 iOS 上,只要添加新项目,它就会跳到启动。分页是通过行为完成的。 ListView是一个FlowListView。这是 XAML:

...
FlowItemsSource="{Binding Profiles.Result}
...
<controls:FlowListView.Behaviors>
<behaviors:FlowListViewPaginating
Command="{Binding LoadMoreCommand}"
Converter="{StaticResource ItemVisibilityConverter}">
</behaviors:FlowListViewPaginating>
</controls:FlowListView.Behaviors>
...

行为:

public class FlowListViewPaginating : Behavior<FlowListView>
{
public FlowListView AssosiatedObject { get; private set; }

public static readonly BindableProperty CommandProperty =
BindableProperty.Create("Command", typeof(DelegateCommand<Profile>), typeof(FlowListViewPaginating), null);

public static readonly BindableProperty InputConverterProperty =
BindableProperty.Create("Converter", typeof(IValueConverter), typeof(FlowListViewPaginating), null);


public DelegateCommand<Profile> Command
{
get { return ( DelegateCommand<Profile> )GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}

public IValueConverter Converter
{
get { return ( IValueConverter )GetValue(InputConverterProperty); }
set { SetValue(InputConverterProperty, value); }
}

protected override void OnAttachedTo(FlowListView bindable)
{
base.OnAttachedTo(bindable);

AssosiatedObject = bindable;
bindable.BindingContextChanged += OnBindingContextChanged;
bindable.ItemAppearing += OnItemAppearing;
}

protected override void OnDetachingFrom(FlowListView bindable)
{
base.OnDetachingFrom(bindable);

bindable.BindingContextChanged -= OnBindingContextChanged;
bindable.ItemAppearing -= OnItemAppearing;
AssosiatedObject = null;
}

private void OnItemAppearing(object sender, ItemVisibilityEventArgs e)
{
var flowListView = ( FlowListView )sender;

if ( flowListView.IsRefreshing ) return;
if ( Command == null ) return;

var parameter = Converter.Convert(e, typeof(object), null, null) as Profile;
if ( Command.CanExecute(parameter) )
Command.Execute(parameter);
}

protected void OnBindingContextChanged(object sender, EventArgs e)
{
OnBindingContextChanged();
}

protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();

BindingContext = AssosiatedObject.BindingContext;
}


}

行为转换器:

public class ItemVisibilityEventConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var eventArgs = value as ItemVisibilityEventArgs;
var collection = eventArgs?.Item as System.Collections.ObjectModel.ObservableCollection<object>;
return collection[0];
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

我的收藏:

public NotifyTask<ObservableRangeCollection<Profile>> Profiles { get; set; }

NotifyTask 与集合无关。它只是一个包装。

添加项目的方法:

private async Task OnLoadMore(Profile lastProfile)
{
IsBusy = true;
_pageNumber++;
Profiles.Result.AddRange(await _manager.GetProfilesAsync(_parameters.NextPage()));
IsBusy = false;
}

最佳答案

当项目更新时,这些示例不会滚动 iOS/Android 上的列表。请务必使用最新的nuget包。

XAML:

<flv:FlowListView FlowColumnCount="3" SeparatorVisibility="None" HasUnevenRows="false"
FlowItemTappedCommand="{Binding ItemTappedCommand}" FlowLastTappedItem="{Binding LastTappedItem}"
FlowItemsSource="{Binding Items}" Grid.ColumnSpan="2">

<flv:FlowListView.FlowColumnTemplate>
<DataTemplate>
<Label HorizontalOptions="Fill" VerticalOptions="Fill"
XAlign="Center" YAlign="Center" Text="{Binding Title}"/>
</DataTemplate>
</flv:FlowListView.FlowColumnTemplate>

</flv:FlowListView>

查看模型:

public UpdateItemsPageModel()
{
AddCommand = new BaseCommand((arg) =>
{
insertId++;
Items.Insert(10, new SimpleItem() { Title = string.Format("New {0}", insertId) });
});

RemoveCommand = new BaseCommand((arg) =>
{
Items.RemoveAt(10);
});
}

public ObservableCollection<object> Items
{
get { return GetField<ObservableCollection<object>>(); }
set { SetField(value); }
}

关于listview - iOS 上的无限 ListView 刷新后跳转到第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41978469/

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