Skip to content

Commit

Permalink
[Android] Only set click listener if the item is indeed clickable (do…
Browse files Browse the repository at this point in the history
…tnet#1884)

* [Android] Only set click listener if the item is indeed clickable

* - compatibility
  • Loading branch information
PureWeen authored Dec 29, 2021
1 parent 0771462 commit 4188242
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
break;
}
}
protected virtual bool IsSelectionEnabled(ViewGroup parent, int viewType) => true;

public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
Expand All @@ -82,12 +83,12 @@ public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int
if (viewType == ItemViewType.TextItem)
{
var view = new TextView(context);
return new TextViewHolder(view);
return new TextViewHolder(view, IsSelectionEnabled(parent, viewType));
}

var itemContentView = _createItemContentView.Invoke(ItemsView, context);

return new TemplatedItemViewHolder(itemContentView, ItemsView.ItemTemplate);
return new TemplatedItemViewHolder(itemContentView, ItemsView.ItemTemplate, IsSelectionEnabled(parent, viewType));
}

public override int ItemCount => ItemsSource.Count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public abstract class SelectableViewHolder : RecyclerView.ViewHolder, global::An

protected SelectableViewHolder(global::Android.Views.View itemView, bool isSelectionEnabled = true) : base(itemView)
{
itemView.SetOnClickListener(this);
if (isSelectionEnabled)
itemView.SetOnClickListener(this);

_isSelectionEnabled = isSelectionEnabled;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class TextViewHolder : SelectableViewHolder
public TextViewHolder(TextView itemView, bool isSelectionEnabled = true) : base(itemView, isSelectionEnabled)
{
TextView = itemView;
TextView.Clickable = true;
TextView.Clickable = isSelectionEnabled;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
VerticalOptions="Center"
FontSize="Large"
Text="{Binding}"/>
<Grid.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="TapGestureRecognizer_Tapped"></TapGestureRecognizer>
</Grid.GestureRecognizers>
</Grid>
</DataTemplate>
</CarouselView.ItemTemplate>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
namespace Maui.Controls.Sample.Pages
using System;

namespace Maui.Controls.Sample.Pages
{
public partial class CarouselViewPage
{
public CarouselViewPage()
{
InitializeComponent();
}


private async void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
await DisplayAlert("Item", "Tapped", "Successfully");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,21 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
}
}

protected virtual bool IsSelectionEnabled(ViewGroup parent, int viewType) => true;

public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
{
var context = parent.Context;

if (viewType == ItemViewType.TextItem)
{
var view = new TextView(context);
return new TextViewHolder(view);
return new TextViewHolder(view, IsSelectionEnabled(parent, viewType));
}

var itemContentView = _createItemContentView.Invoke(ItemsView, context);

return new TemplatedItemViewHolder(itemContentView, ItemsView.ItemTemplate);
return new TemplatedItemViewHolder(itemContentView, ItemsView.ItemTemplate, IsSelectionEnabled(parent, viewType));
}

public override int ItemCount => ItemsSource.Count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public abstract class SelectableViewHolder : RecyclerView.ViewHolder, global::An

protected SelectableViewHolder(global::Android.Views.View itemView, bool isSelectionEnabled = true) : base(itemView)
{
itemView.SetOnClickListener(this);
if (isSelectionEnabled)
itemView.SetOnClickListener(this);

_isSelectionEnabled = isSelectionEnabled;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class TextViewHolder : SelectableViewHolder
public TextViewHolder(TextView itemView, bool isSelectionEnabled = true) : base(itemView, isSelectionEnabled)
{
TextView = itemView;
TextView.Clickable = true;
TextView.Clickable = isSelectionEnabled;
}
}
}

0 comments on commit 4188242

Please sign in to comment.