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

Commit 94acebb

Browse files
jfversluisheikow10
andauthored
[Android] fix item indices and item count in a ListView (#15478)
* Revert "Revert "[Android] fix item indices and item count in a ListView (#15306)" (#15473)" This reverts commit 04ace58. * Fix scroll position in ListView on Android (#15305) Co-authored-by: Heiko Wobst <[email protected]>
1 parent 23dcbc2 commit 94acebb

File tree

6 files changed

+95
-9
lines changed

6 files changed

+95
-9
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<controls:TestContentPage
3+
xmlns="http://xamarin.com/schemas/2014/forms"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5+
xmlns:controls="clr-namespace:Xamarin.Forms.Controls"
6+
xmlns:issues="clr-namespace:Xamarin.Forms.Controls.Issues"
7+
x:Class="Xamarin.Forms.Controls.Issues.Issue15305"
8+
x:DataType="issues:ViewModelIssue15305">
9+
<StackLayout>
10+
<Label HorizontalTextAlignment="Center"
11+
Padding="3.0"
12+
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." />
13+
<ListView ItemsSource="{Binding Path=Items}">
14+
<ListView.ItemTemplate>
15+
<DataTemplate x:DataType="x:String">
16+
<ViewCell>
17+
<Label Text="{Binding Path=.}" />
18+
</ViewCell>
19+
</DataTemplate>
20+
</ListView.ItemTemplate>
21+
</ListView>
22+
</StackLayout>
23+
</controls:TestContentPage>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Collections.ObjectModel;
2+
using Xamarin.Forms.CustomAttributes;
3+
using Xamarin.Forms.Internals;
4+
using Xamarin.Forms.Xaml;
5+
6+
#if UITEST
7+
using Xamarin.Forms.Core.UITests;
8+
using Xamarin.UITest;
9+
using NUnit.Framework;
10+
#endif
11+
12+
namespace Xamarin.Forms.Controls.Issues
13+
{
14+
#if UITEST
15+
[Category(UITestCategories.ManualReview)]
16+
#endif
17+
#if APP
18+
[XamlCompilation(XamlCompilationOptions.Compile)]
19+
#endif
20+
[Preserve(AllMembers = true)]
21+
[Issue(IssueTracker.Github, 15305, "[Android] TalkBack always considers ListView's header and footer for indexing/counting", PlatformAffected.Android)]
22+
public partial class Issue15305 : TestContentPage
23+
{
24+
public Issue15305()
25+
{
26+
#if APP
27+
InitializeComponent();
28+
#endif
29+
}
30+
31+
protected override void Init()
32+
{
33+
BindingContext = new ViewModelIssue15305();
34+
}
35+
}
36+
37+
[Preserve(AllMembers = true)]
38+
public class ViewModelIssue15305
39+
{
40+
public ViewModelIssue15305()
41+
{
42+
Items = new ObservableCollection<string>() { "first item", "second item", };
43+
}
44+
45+
public ObservableCollection<string> Items { get; }
46+
}
47+
}

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,9 @@
18641864
<Compile Include="$(MSBuildThisFileDirectory)Issue11954.cs" />
18651865
<Compile Include="$(MSBuildThisFileDirectory)Issue13918.xaml.cs" />
18661866
<Compile Include="$(MSBuildThisFileDirectory)Issue13794.cs" />
1867+
<Compile Include="$(MSBuildThisFileDirectory)Issue15305.xaml.cs">
1868+
<DependentUpon>Issue15305.xaml</DependentUpon>
1869+
</Compile>
18671870
<Compile Include="$(MSBuildThisFileDirectory)Controls\MyCollectionView.xaml.cs">
18681871
<DependentUpon>MyCollectionView.xaml</DependentUpon>
18691872
<SubType>Code</SubType>
@@ -2419,6 +2422,9 @@
24192422
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue13918.xaml">
24202423
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
24212424
</EmbeddedResource>
2425+
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Issue15305.xaml">
2426+
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
2427+
</EmbeddedResource>
24222428
<EmbeddedResource Include="$(MSBuildThisFileDirectory)Controls\MyCollectionView.xaml">
24232429
<SubType>Designer</SubType>
24242430
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>

Xamarin.Forms.Platform.Android/CellAdapter.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,12 @@ bool AActionMode.ICallback.OnPrepareActionMode(AActionMode mode, IMenu menu)
132132

133133
public void OnItemClick(AdapterView parent, AView view, int position, long id)
134134
{
135+
var listView = parent as AListView;
136+
if (listView != null)
137+
position -= listView.HeaderViewsCount;
138+
135139
if (_actionMode != null || _supportActionMode != null)
136-
{
137-
var listView = parent as AListView;
138-
if (listView != null)
139-
position -= listView.HeaderViewsCount;
140140
HandleContextMode(view, position);
141-
}
142141
else
143142
HandleItemClick(parent, view, position, id);
144143
}

Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,6 @@ protected override void HandleItemClick(AdapterView parent, AView view, int posi
481481
cell = (Cell)(cellOwner as INativeElementView)?.Element;
482482
}
483483

484-
// All our ListView's have called AddHeaderView. This effectively becomes index 0, so our index 0 is index 1 to the listView.
485-
position--;
486-
487484
if (position < 0 || position >= Count)
488485
return;
489486

Xamarin.Forms.Platform.Android/Renderers/ListViewRenderer.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ void OnScrollToRequested(object sender, ScrollToRequestedEventArgs e)
308308
}
309309

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

313313
if (e.Position == ScrollToPosition.MakeVisible)
314314
{
@@ -365,7 +365,14 @@ void UpdateFooter()
365365
}
366366

367367
if (footer == null)
368+
{
369+
if (_footerView.ChildCount == 0)
370+
{
371+
AListView nativeListView = Control;
372+
nativeListView.RemoveFooterView(_adapter.FooterView);
373+
}
368374
return;
375+
}
369376

370377
if (_footerRenderer != null)
371378
_footerRenderer.SetElement(footer);
@@ -397,7 +404,14 @@ void UpdateHeader()
397404
}
398405

399406
if (header == null)
407+
{
408+
if (_headerView.ChildCount == 0)
409+
{
410+
AListView nativeListView = Control;
411+
nativeListView.RemoveHeaderView(_adapter.HeaderView);
412+
}
400413
return;
414+
}
401415

402416
if (_headerRenderer != null)
403417
_headerRenderer.SetElement(header);

0 commit comments

Comments
 (0)