Listview expander

Eduardo Gomez 3,416 Reputation points
2022-09-15T21:52:05.757+00:00

I have to put an expander on every circle (Where you can see the abbreviation of the car)

List of Cars

I have my ListViewPage Xaml

   <ContentPage.Content>  
           <CollectionView  
               x:Name="CarsList"  
               ItemsSource="{Binding Cars}"  
               SelectionMode="None">  
               <CollectionView.ItemTemplate>  
                   <DataTemplate>  
                       <SwipeView>  
                           <SwipeView.RightItems>  
                               <SwipeItems Mode="Execute">  
                                   <SwipeItem  
                                       BackgroundColor="Red"  
                                       Command="{Binding Source={x:Reference ListPage}, Path=BindingContext.DeleteCommand}"  
                                       CommandParameter="{Binding .}"  
                                       Text="Delete" />  
                               </SwipeItems>  
                           </SwipeView.RightItems>  
                           <local:CarView  
                               Padding="0,10,0,0"  
                               xct:TouchEffect.AnimationEasing="CubicInOut"  
                               xct:TouchEffect.Command="{Binding Source={x:Reference ListPage}, Path=BindingContext.PressedCommand}"  
                               xct:TouchEffect.CommandParameter="{Binding .}"  
                               xct:TouchEffect.LongPressCommand="{Binding Source={x:Reference ListPage}, Path=BindingContext.LongPressedCommand}"  
                               xct:TouchEffect.LongPressCommandParameter="{Binding .}"  
                               xct:TouchEffect.NativeAnimation="True"  
                               xct:TouchEffect.PressedScale="0.8" />  
                       </SwipeView>  
                   </DataTemplate>  
               </CollectionView.ItemTemplate>  
           </CollectionView>  
       </ContentPage.Content>  
   </ContentPage>  

And I have two more views, the CarPageView

   <ContentView.Content>  
   		<Grid>  
   			<Frame Margin="5,15,5,5"  
   			       BorderColor="LightGray"  
   			       CornerRadius="5"  
   			       HasShadow="False">  
   				<Grid>  
   					<Grid.RowDefinitions>  
   						<RowDefinition Height="Auto" />  
   					</Grid.RowDefinitions>  
   					<Grid IsVisible="{Binding IsExpanded, Source={x:Reference _carView}}">  
   						<Grid.RowDefinitions>  
   							<RowDefinition Height="Auto" />  
   							<RowDefinition Height="*" />  
   						</Grid.RowDefinitions>  
   						<Label HorizontalOptions="End"  
   						       Text="{Binding Notes}"  
   						       TextColor="Gray" />  
   						<Label Grid.Row="1" Text="{Binding Description}" />  
   					</Grid>  
   				</Grid>  
   			</Frame>  
   			<StackLayout Margin="20,0,0,0"  
   			             BackgroundColor="White"  
   			             HorizontalOptions="Start"  
   			             Orientation="Horizontal"  
   			             VerticalOptions="Start">  
   				<local:CarCircleView Margin="7,0,0,0"  
   				                     Command="{Binding ToggleCollapseCommand, Source={x:Reference _carView}}"  
   				                     HeightRequest="30"  
   				                     HorizontalOptions="Start"  
   				                     VerticalOptions="Start"  
   				                     WidthRequest="30" />  
   				<Label Margin="0,0,7,0"  
   				       FontAttributes="Bold"  
   				       Text="{Binding Name}"  
   				       VerticalTextAlignment="Center" />  
   			</StackLayout>  
   		</Grid>  
   	</ContentView.Content>  

And the circleView

   <ContentView.Content>  
           <Frame  
               x:Name="_borderOuter"  
               Padding="1"  
               BackgroundColor="{Binding Color}"  
               BorderColor="{Binding Color}"  
               HasShadow="False">  
               <Frame  
                   x:Name="_borderInner"  
                   Padding="5"  
                   BorderColor="{Binding Color}"  
                   HasShadow="False">  
                   <Label  
                       Margin="1,-1,0,0"  
                       FontAttributes="Bold"  
                       HorizontalOptions="Center"  
                       HorizontalTextAlignment="Center"  
                       Text="{Binding Abbreviation}"  
                       VerticalOptions="Center"  
                       VerticalTextAlignment="Center" />  
               </Frame>  
           </Frame>  
       </ContentView.Content>  

VM

   public Command<Car> PressedCommand  
       {  
           get; set;  
       }  
     
       public Command<Car> LongPressedCommand  
       {  
           get; set;  
       }  
       public Command<Car> DeleteCommand  
       {  
           get; set;  
       }  
     
       public SampleViewModel()  
       {  
           DeleteCommand = new Command<Car>(DeleteAction);  
           LongPressedCommand = new Command<Car>(LongPressedAction);  
           PressedCommand = new Command<Car>(PressAction);  
           GridSpan = Device.Idiom == TargetIdiom.Phone ? 1 : 2;  
           BuildCars();  
       }  
     
       private void DeleteAction(Car obj)  
       {  
           Cars.Remove(obj);  
       }  
     
       private async void PressAction(Car obj)  
       {  
           if (obj != null && obj.Make == CarMake.Mercedes)  
           {  
               var MercedesView = new MecedesSpecificView()  
               {  
                   BindingContext = new MecedesSpecificViewModel(obj)  
               };  
               await Application.Current.MainPage.Navigation.PushAsync(MercedesView);  
           }  
           else  
           {  
               var DetailsPage = new ListViewDetailPage  
               {  
                   BindingContext = new ListViewDetailPageViewModel(obj)  
               };  
               await Application.Current.MainPage.Navigation.PushAsync(DetailsPage);  
     
           }  
       }  
     
       private async void LongPressedAction(Car obj)  
       {  
           var ans = await Application.Current.MainPage.DisplayAlert("Warning", "do you want to delete?", "OK", "Cancel");  
     
           if (ans)  
           {  
               Cars.Remove(obj);  
           }  
       }  
     
     
       private int _gridSpan;  
     
       public int GridSpan  
       {  
           get => _gridSpan;  
           set => SetProperty(ref _gridSpan, value);  
       }  
     
       private void BuildCars()  
       {  
           Cars = new ObservableCollection<Car>  
           {  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Notes = "test car", Description = "Some description", Color = Color.Black },  
               new Car { Abbreviation = "BMW", Make=CarMake.BMW, Name = "X5", Description = string.Concat(Enumerable.Repeat($"Some description {Environment.NewLine}", 10)), Color = Color.Purple },  
               new Car { Abbreviation = "M", Make=CarMake.Mercedes, Name = "AMG C Class", Description = string.Concat(Enumerable.Repeat($"Some description {Environment.NewLine}", 5)), Color = Color.CornflowerBlue},  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = "Some description", Color = Color.Brown },  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = string.Concat(Enumerable.Repeat($"Some description {Environment.NewLine}", 3)), Color = Color.Orange },  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = "Some description", Color = Color.DarkBlue },  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = string.Concat(Enumerable.Repeat($"Some description {Environment.NewLine}", 7)), Color = Color.DarkOrange },  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = "Some description", Color = Color.OrangeRed },  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = string.Concat(Enumerable.Repeat($"Some description {Environment.NewLine}", 4)), Color = Color.Violet},  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = string.Concat(Enumerable.Repeat($"Some description {Environment.NewLine}", 2)), Color = Color.DarkSalmon },  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = "Some description", Color = Color.Green },  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = string.Concat(Enumerable.Repeat($"Some description {Environment.NewLine}", 6)), Color = Color.GreenYellow},  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = string.Concat(Enumerable.Repeat($"Some description {Environment.NewLine}", 3)), Color = Color.LawnGreen},  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = "Some description", Color = Color.SkyBlue },  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = string.Concat(Enumerable.Repeat($"Some description {Environment.NewLine}", 5)), Color = Color.LightCyan },  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = "Some description", Color = Color.PaleTurquoise },  
               new Car { Abbreviation = "VW", Make=CarMake.VolksWagen, Name = "Polo", Description = "Some description", Color = Color.Purple },  
     
           };  
       }  
     
       private ObservableCollection<Car> _cars;  
     
       public ObservableCollection<Car> Cars  
       {  
           get  
           {  
               return _cars;  
           }  
           set  
           {  
               SetProperty(ref _cars, value);  
           }  
       }  
     
   }  

What I do not understand is, that I already tried to use the xamarin community expander on the CirclePage, but for some reason, it did not work.

This is the Instruccion that I got

  • Using the ListView Page:
  • add an expand/collapse animation when tapping the circle

I tried to add the expander, but I think that because of the TouchEffect on every cell, it doesn't work.

So how can I have the two o them play nice with each other, I want to maintain the ability to navigate, but I also want to be able to expand when I press the circle

https://reccloud.com/u/bg8pvw1 // this is how it is right now

Oh by the way I was able to use a collectonView after all, that is wwht I am making this update

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,021 Reputation points Microsoft Vendor
    2022-09-16T05:40:07.897+00:00

    Hello,

    It is caused by there is no Expander.Header and Expander.ContentTemplate in your xaml.

    It is needed to set them to expand or collapse the content in to header.

    You could refer to the following code:

    Integrate the circle with the text box as the header in the circleView:

       <ContentView.Content>  
           <StackLayout     ...>  
         
               <Frame ...>  
                   <Frame  
                       ...>  
                       <Label  
                           ... />  
                   </Frame>  
               </Frame>  
               <Label FontAttributes="Bold" Text="{Binding Text}" />  
           </StackLayout>  
       </ContentView.Content>  
    

    The CarPageView:

       <Grid>   
           <xct:Expander>  
               <xct:Expander.Header>  
                   <local:CarCircleView Margin="7,0,0,0"  
                                            HorizontalOptions="Start"  
                                            VerticalOptions="Start" />  
               </xct:Expander.Header>  
               <xct:Expander.ContentTemplate>  
                   <DataTemplate>  
                       <Frame Margin="5,15,5,5"  
                          BorderColor="LightGray"  
                          CornerRadius="5"  
                          HasShadow="False">  
                           <Grid>  
                               <Grid.RowDefinitions>  
                                   <RowDefinition Height="Auto" />  
                               </Grid.RowDefinitions>  
                               <Grid>  
                                   <Grid.RowDefinitions>  
                                       <RowDefinition Height="Auto" />  
                                       <RowDefinition Height="*" />  
                                   </Grid.RowDefinitions>  
                                   <Label HorizontalOptions="End"  
                                      Text="{Binding Description}"  
                                      TextColor="Gray" />  
                                   <Label Grid.Row="1" Text="{Binding Description}" />  
                               </Grid>  
                           </Grid>  
                       </Frame>  
                   </DataTemplate>  
         
               </xct:Expander.ContentTemplate>  
           </xct:Expander>  
       </Grid>  
    

    Please refer to Xamarin Community Toolkit Expander to get more details.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful