8

Is there anyway to prevent double tap on ListView in Android? I found this when i accidentally tapped item on ListView and it opened up two new window. is there any way to prevent it from opening a same window twice.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
srbyk1990
  • 411
  • 5
  • 17
  • Can you provide a sample of your code or an image? Usually things are processed quickly which would prevent that behavior. – Anid Monsur Feb 10 '14 at 20:30
  • 2
    possible duplicate of http://stackoverflow.com/questions/16534369/avoid-button-multiple-rapid-clicks/16534470#16534470 – GreyBeardedGeek Feb 10 '14 at 20:53
  • Possible duplicate of [Android Preventing Double Click On A Button](https://stackoverflow.com/questions/5608720/android-preventing-double-click-on-a-button) – Gustavo Pagani Jul 16 '19 at 10:21

7 Answers7

2

Just add listView.setEnabled(false); on select of listview and after select when response will come or back button press just write---- listView.setEnabled(true);

Termininja
  • 6,620
  • 12
  • 48
  • 49
  • A possible solution but not the best practice :( – Mo Zaatar Mar 19 '18 at 03:15
  • In my case It works. When back to the listview I overrided OnResume() method. https://stackoverflow.com/questions/6636039/how-to-catch-event-of-coming-back-to-an-activity-after-hitting-back – JotaPardo Jun 20 '19 at 02:25
0

Have a try with introducing and Override of isEnabled method

    @Override
public boolean isEnabled(int position) {
    return false;
}

for the listview.

introduce a boolean for flag and an int to maintain last clicked position

 int recentlyClickedPoisition;
        boolean flagRecentClickedPoisition;

override the isEnabled method as follows

   @Override
   public boolean isEnabled(int position) {
        if (flagRecentClickedPoisition && recentlyClickedPoisition == position) {
            return false;
        } else {
            return true;
        }
    }

than set the last clicked position from your click listener as follows

public void setLastClickedPoisition(int recentlyClickedPoisition) {
    flagRecentClickedPoisition = true;
    this.recentlyClickedPoisition = recentlyClickedPoisition;
}

Hope this will work for you to prevent from double tap, enhance accordinly.

Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
0

You should restrict the target activity (one that opens when an item is clicked) to have only one instance at any point of time.

Answer to this SO question should help you in achieving that. That way if you accidentally double click, you will still see just one new screen.

Community
  • 1
  • 1
Viral Patel
  • 32,418
  • 18
  • 82
  • 110
0

If you are using just single item like TextView in list then just create a class implements OnItemClickListener in this call and then in to onItemClick() method initialize myListView.setOnItemClickListenet(null); then use Handler.postDelayed method to again set onItemClickListener. like

myListView.setOnItemClickListener(new MyClickListenerClass());

This is working for all time in my case.

0

In your XAML view page place isEnable property as a bindable and two way mode.

<ListView ItemsSource="{Binding items}"
                                  x:Name="listview"
                                  HasUnevenRows="True"
                                  IsEnabled="{Binding IsEnable, Mode=TwoWay}"
                                  RowHeight="10"
                                  SelectionMode="Single"
                                  VerticalScrollBarVisibility="Never">

In viewmodel of your xaml page :

    private bool _isEnable = true;
    public bool IsEnable
    {
        get => _isEnable;
        set
        {
            _isEnable = value; OnPropertyChanged(nameof(IsEnable));
        }
    }


public ICommand TapCommand => new Command<//Model>(async (obj) =>
    {
        IsEnable = false;
       //your stuff 
        IsEnable = true;
    });
0

I have a two pane layout, a listview that controls a detail view. First I thought a delayed handler is the worst idea, but after testing it is the simplest. Otherwise I would have to communicate between activity and fragment to enable the listview item once another detail was loaded. Error prone and complex.

/*example with Handler():*/

        final boolean[] allowClick = {true};

        myview.setOnClickListener(v -> {

            //exit if not allowed
            if(!allowClick[0])
                return;

            //do stuff

            //we clicked, block another click
            allowClick[0] =false;

            //wait 0.7 seconds and allow another click
            new Handler().postDelayed(() -> allowClick[0] =true, 700);

        });
CaptainCrunch
  • 1,230
  • 14
  • 15
0

This solution is implemented on a ListFragment. If the tap dismissed the ListFragment to show a detail view (which it normally would), the next time the ListFragment appears, the tap counter is reset in OnResume():

public class MyListFragment extends ListFragment {

    int mTapCounter;

    @Override
    public void onResume() {
        super.onResume();

        //Set-Reset ListView clickListener
        mTapCounter = 0;
    }

    //ListView item tap event handler
    @Override
    public void onListItemClick(@NonNull ListView l, @NonNull View v, int position, long id) {

        //Disable click listener to prevent double-tap
        mTapCounter++;

        //Only process single-tap
        if(mTapCounter == 1) {

            /* YOUR TAP HANDLER CODE HERE */
        }

        super.onListItemClick(l, v, position, id);
    }
}