using
Telerik.XamarinForms.Common;
namespace
CustomizableListView
{
public
class
Song : NotifyPropertyChangedBase
{
private
bool
isFavourite;
public
string
Title {
get
;
set
; }
public
string
Author {
get
;
set
; }
public
string
Album {
get
;
set
; }
public
string
AlbumArt {
get
;
set
; }
public
bool
IsFavourite
{
get
{
return
this
.isFavourite;
}
set
{
if
(
this
.isFavourite != value)
{
this
.isFavourite = value;
this
.RaisePropertyCanged();
}
}
}
}
}
using
System;
using
System.Collections.ObjectModel;
using
Telerik.XamarinForms.Common;
namespace
CustomizableListView
{
public
class
ViewModel : NotifyPropertyChangedBase
{
private
ObservableCollection<Song> songs;
public
ViewModel()
{
this
.Songs =
this
.GetData();
}
public
ObservableCollection<Song> Songs
{
get
{
return
this
.songs;
}
set
{
if
(
this
.songs != value)
{
this
.songs = value;
this
.RaisePropertyCanged();
}
}
}
public
void
ReloadData()
{
this
.Songs =
this
.GetData();
}
private
ObservableCollection<Song> GetData()
{
// get songs data
}
}
}
<
telerikDataControls:RadListView.ItemTemplate
>
<
DataTemplate
>
<
telerikListView:ListViewTemplateCell
>
<
telerikListView:ListViewTemplateCell.View
>
<
Grid
Padding
=
"10"
>
<
Grid.ColumnDefinitions
>
<
ColumnDefinition
Width
=
"90"
/>
<
ColumnDefinition
Width
=
"*"
/>
</
Grid.ColumnDefinitions
>
<
Image
Source
=
"{Binding AlbumArt}"
/>
<
StackLayout
Padding
=
"10,0,0,0"
Grid.Column
=
"1"
>
<
Grid
>
<
Grid.ColumnDefinitions
>
<
ColumnDefinition
Width
=
"*"
/>
<
ColumnDefinition
Width
=
"Auto"
/>
</
Grid.ColumnDefinitions
>
<
Label
Text
=
"{Binding Title}"
TextColor
=
"#694A00"
LineBreakMode
=
"TailTruncation"
FontSize
=
"Large"
/>
<
Image
Source
=
"{Binding IsFavourite, Converter={StaticResource FavouriteImageConverter}}"
Grid.Column
=
"1"
/>
</
Grid
>
<
StackLayout
Orientation
=
"Horizontal"
Padding
=
"5,0,0,0"
>
<
Label
TextColor
=
"#806015"
Text
=
"by"
FontAttributes
=
"Italic"
FontSize
=
"Medium"
/>
<
Label
TextColor
=
"#806015"
Text
=
"{Binding Author}"
LineBreakMode
=
"TailTruncation"
FontSize
=
"Medium"
/>
</
StackLayout
>
<
StackLayout
Orientation
=
"Horizontal"
Padding
=
"15,0,0,0"
>
<
Label
TextColor
=
"#A0967D"
Text
=
"{Binding Album}"
LineBreakMode
=
"TailTruncation"
FontSize
=
"Small"
/>
</
StackLayout
>
</
StackLayout
>
</
Grid
>
</
telerikListView:ListViewTemplateCell.View
>
</
telerikListView:ListViewTemplateCell
>
</
DataTemplate
>
</
telerikDataControls:RadListView.ItemTemplate
>
<
telerikDataControls:RadListView.SelectedItemStyle
>
<
telerikListView:ListViewItemStyle
BackgroundColor
=
"{StaticResource SelectedBackgroundColor}"
BorderLocation
=
"Bottom"
BorderColor
=
"#A0967D"
BorderWidth
=
"2"
/>
</
telerikDataControls:RadListView.SelectedItemStyle
>
<
telerikDataControls:RadListView.ItemStyle
>
<
telerikListView:ListViewItemStyle
BackgroundColor
=
"{StaticResource BackgroundColor}"
BorderLocation
=
"Bottom"
BorderColor
=
"#A0967D"
BorderWidth
=
"2"
/>
</
telerikDataControls:RadListView.ItemStyle
>
<
telerikDataControls:RadListView.PressedItemStyle
>
<
telerikListView:ListViewItemStyle
BackgroundColor
=
"{StaticResource BackgroundColor}"
BorderLocation
=
"Bottom"
BorderColor
=
"#A0967D"
BorderWidth
=
"2"
/>
</
telerikDataControls:RadListView.PressedItemStyle
>
<
telerikDataControls:RadListView.LayoutDefinition
>
<
telerikListView:ListViewLinearLayout
ItemLength
=
"100"
/>
</
telerikDataControls:RadListView.LayoutDefinition
>
<
telerikDataControls:RadListView
x:Name
=
"listView"
IsItemSwipeEnabled
=
"True"
SwipeOffset
=
"50,0,50,0"
...>
<
telerikDataControls:RadListView.ItemSwipeContentTemplate
>
<
DataTemplate
>
<
Grid
>
<
Grid.ColumnDefinitions
>
<
ColumnDefinition
Width
=
"Auto"
/>
<
ColumnDefinition
Width
=
"*"
/>
<
ColumnDefinition
Width
=
"Auto"
/>
</
Grid.ColumnDefinitions
>
<
Grid
BackgroundColor
=
"#33FFFFFF"
Grid.Column
=
"0"
>
<
Button
Image
=
"{Binding IsFavourite, Converter={StaticResource FavouriteButtonConverter}}"
Style
=
"{StaticResource ButtonStyle}"
Clicked
=
"LoveButtonClick"
/>
</
Grid
>
<
Grid
BackgroundColor
=
"#33FF0000"
Grid.Column
=
"2"
>
<
Button
Image
=
"trash.png"
Style
=
"{StaticResource ButtonStyle}"
Clicked
=
"DeleteButtonClick"
/>
</
Grid
>
</
Grid
>
</
DataTemplate
>
</
telerikDataControls:RadListView.ItemSwipeContentTemplate
>
private
void
LoveButtonClick(
object
sender, EventArgs e)
{
var song = (sender
as
Button).BindingContext
as
Song;
song.IsFavourite = !song.IsFavourite;
}
private
void
DeleteButtonClick(
object
sender, EventArgs e)
{
var song = (sender
as
Button).BindingContext
as
Song;
this
.viewModel.Songs.Remove(song);
}
Happy coding!
Update: Check out the newly released Telerik Tagit, a cross-platform native mobile Xamarin app. We give you source code and a six-part walkthrough of how it was created. Check out how we implemented the ListView control in that app, right here!
Rosy started her career at Telerik as a junior support officer and changed her track to development. She is now a software developer and works on multiple products: Telerik UI for Windows Phone, Telerik UI for Windows Universal and Telerik UI for Xamarin Forms.