Two Touches on Listview - Xamarim Forms

Asked

Viewed 84 times

0

It is possible to have ONE touch for a certain function (today I have to select an item using ItemTapped) AND TWO TOUCHES OF THE SAME ListView to do another function?

I did not get this information on the Microsoft website and in any other source.

My XAML:

<!-- ListView -->
                <ListView x:Name="lstCompra"  
                          BackgroundColor="Aqua"
                          SeparatorColor="DodgerBlue"
                          ItemTapped="Compra_OnItemTapped"
                          HasUnevenRows="True"
                          Margin="10, 0, 10, 10">
                          <!--  HasUnevenRows = Serve para fazer com que o conteúdo digitado não seja cortado -->
                    <ListView.ItemTemplate>
                        <!-- DataTemplate  = exibe dados de uma coleção de objetos em um ListView -->
                        <DataTemplate>
                            <ViewCell>
                                <Grid Margin="2">

                                    <!-- Linhas -->
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>

                                    <!-- Colunas -->
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>

                                    <!-- Código -->
                                    <StackLayout Grid.Row="0"
                                                     Grid.Column="0"
                                                     Orientation="Horizontal">
                                        <Label Text="S.C.:"
                                                   FontSize="Small"/>
                                        <Label Text="{Binding ID_SOLCOMPRA}" 
                                                   FontSize="Small"
                                                   TextColor="Black"/>
                                    </StackLayout>

                                    <!-- Data -->
                                    <StackLayout Grid.Row="0"
                                                     Grid.Column="1"
                                                     Orientation="Horizontal">
                                        <Label Text="Data:"
                                                   FontSize="Small"
                                                   HorizontalOptions="Start"/>
                                        <Label Text="{Binding DT_CADASTRO}" 
                                                   FontSize="Small"
                                                   TextColor="Black"
                                                   HorizontalOptions="Start"/>
                                    </StackLayout>

                                </Grid>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>

My C#:

//Selecionando item da lista
        private void Compra_OnItemTapped(object sender, ItemTappedEventArgs e)
        {
            solicita = (sender as ListView).SelectedItem as SolicitarCompra;
        }

I’m trying to test what was passed to me by @Amadeuantunes and I haven’t succeeded (I’m very new to programming), gives error in this line btnClick.Clicked += (Sender, and) =>.

The mistake:

inserir a descrição da imagem aqui

THE XAML:

        <StackLayout>
            <Button Text="Teste"
                    Clicked="Btn_Teste"
                    x:Name="btnClick"/>
        </StackLayout>

The code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Teste
{
    public partial class MainPage : ContentPage
    {
        int ButtonCount = 0;

        public MainPage()
        {
            InitializeComponent();
        }

        void Btn_Teste(object sender, EventArgs e)
        {
            btnClick.Clicked += (sender, e) =>
            {

                if (ButtonCount < 1)
                {
                    TimeSpan tt = new TimeSpan(0, 0, 1);
                    Device.StartTimer(tt, TestHandleFunc);
                }
                ButtonCount++;
            };

            bool TestHandleFunc()
            {
                if (ButtonCount > 1)
                {
                    //Your action for Double Click here
                    DisplayAlert("", "Two Clicks", "OK");
                }
                else
                {
                    //Your action for Single Click here
                    DisplayAlert("", "One Click", "OK");
                }
                ButtonCount = 0;
                return false;
            }
        }
    }
}
  • show the code

  • Added the code @Leandroangelo

  • You are learning or working with Xamarim?

  • Both, I’m learning at work rs. My practice has been in a real app.

1 answer

0


I would use:

The first time you make Itemtapped triggers a "timer" when you click again stops the "timer" and calls another method that you can even call Double Click

1º Tapgesturerecognizer - start a "timer"

2º Tapgesturerecognizer Stops the "timer" and invokes another method called doupleTapped

Something like that

     btnClick.Clicked += (sender, e) =>
                {
                    if (ButtonCount < 1)
                    {
                        TimeSpan tt = new TimeSpan(0, 0, 1);
                        Device.StartTimer(tt, TestHandleFunc);
                    }
                    ButtonCount++;
                };

bool TestHandleFunc()
        {
            if (ButtonCount > 1)
            {
            //Your action for Double Click here
                DisplayAlert("", "Two Clicks", "OK");
            }
            else
            {
            //Your action for Single Click here
                DisplayAlert("", "One Click", "OK");
            }
            ButtonCount = 0;
            return false;
        }
  • Added the return of the test in my post, if you can check thank you.

  • This way it does not seem to be the best option after having seen about Tapgesture here in the company, we are trying to adapt it in the system. But thank you for the option.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.