Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Android] fix item indices and item count in a ListView – second try #15477

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" ?>
<controls:TestContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:Xamarin.Forms.Controls"
xmlns:issues="clr-namespace:Xamarin.Forms.Controls.Issues"
x:Class="Xamarin.Forms.Controls.Issues.Issue15305"
x:DataType="issues:ViewModelIssue15305">
<StackLayout>
<Label HorizontalTextAlignment="Center"
Padding="3.0"
Text="Activate TalkBack. Tap on the first or second item in the ListView on this page.&#x0a;If the screen reader reads out &quot;two of four in list, four items&quot; for the first item (or &quot;three of four in list, four items&quot; for the second item),&#x0a;the test has failed." />
<ListView ItemsSource="{Binding Path=Items}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<ViewCell>
<Label Text="{Binding Path=.}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</controls:TestContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.ObjectModel;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Xaml;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.ManualReview)]
#endif
#if APP
[XamlCompilation(XamlCompilationOptions.Compile)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 15305, "[Android] TalkBack always considers ListView's header and footer for indexing/counting", PlatformAffected.Android)]
public partial class Issue15305 : TestContentPage
{
public Issue15305()
{
#if APP
InitializeComponent();
#endif
}

protected override void Init()
{
BindingContext = new ViewModelIssue15305();
}
}

[Preserve(AllMembers = true)]
public class ViewModelIssue15305
{
public ViewModelIssue15305()
{
Items = new ObservableCollection<string>() { "first item", "second item", };
}

public ObservableCollection<string> Items { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue11954.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13918.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13794.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue15305.xaml.cs">
<DependentUpon>Issue15305.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Controls\MyCollectionView.xaml.cs">
<DependentUpon>MyCollectionView.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down Expand Up @@ -2419,6 +2422,9 @@
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13918.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue15305.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Controls\MyCollectionView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
Expand Down
9 changes: 4 additions & 5 deletions Xamarin.Forms.Platform.Android/CellAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,12 @@ bool AActionMode.ICallback.OnPrepareActionMode(AActionMode mode, IMenu menu)

public void OnItemClick(AdapterView parent, AView view, int position, long id)
{
var listView = parent as AListView;
if (listView != null)
position -= listView.HeaderViewsCount;

if (_actionMode != null || _supportActionMode != null)
{
var listView = parent as AListView;
if (listView != null)
position -= listView.HeaderViewsCount;
HandleContextMode(view, position);
}
else
HandleItemClick(parent, view, position, id);
}
Expand Down
3 changes: 0 additions & 3 deletions Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,9 +481,6 @@ protected override void HandleItemClick(AdapterView parent, AView view, int posi
cell = (Cell)(cellOwner as INativeElementView)?.Element;
}

// All our ListView's have called AddHeaderView. This effectively becomes index 0, so our index 0 is index 1 to the listView.
position--;

if (position < 0 || position >= Count)
return;

Expand Down
16 changes: 15 additions & 1 deletion Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ void OnScrollToRequested(object sender, ScrollToRequestedEventArgs e)
}

//Android offsets position of cells when using header
int realPositionWithHeader = scrollPosition + 1;
int realPositionWithHeader = scrollPosition + _headerView?.ChildCount ?? 0;

if (e.Position == ScrollToPosition.MakeVisible)
{
Expand Down Expand Up @@ -365,7 +365,14 @@ void UpdateFooter()
}

if (footer == null)
{
if (_footerView.ChildCount == 0)
{
AListView nativeListView = Control;
nativeListView.RemoveFooterView(_adapter.FooterView);
}
return;
}

if (_footerRenderer != null)
_footerRenderer.SetElement(footer);
Expand Down Expand Up @@ -397,7 +404,14 @@ void UpdateHeader()
}

if (header == null)
{
if (_headerView.ChildCount == 0)
{
AListView nativeListView = Control;
nativeListView.RemoveHeaderView(_adapter.HeaderView);
}
return;
}

if (_headerRenderer != null)
_headerRenderer.SetElement(header);
Expand Down