goddess1875
goddess1875

Reputation: 103

How to prevent quickly click switch tab in Android

I have a tablayout like the one below:

I want to setup a interval time (for example: 500ms) between tab switch, this mean when I click "First" tab and then click "Second" immediately, it will not switch to tab "Second", if and only if "First" clicked and after 500ms, the click event on tab "Second" valid, if i click followed by "First", "Second", "Third" and "Fourth" in 500ms, the click on "Second" and "Third" will be invalid, the tab will switch from "First" to "Fourth" directly, how can I implement it????

Upvotes: 0

Views: 204

Answers (1)

user3485077
user3485077

Reputation: 146

I not sure if i completely understand your logic with the tabs. Anyways you can use Handler to wait for the 500ms. You can modifiy the example for your needs.

You can create a Handler like this:

boolean mClickAllowed = true;
Handler mHandler = new Handler();
Runnable mRunnable = new Runnable() {
    public void run() {
        mClickAllowed = true;
    }
};

Check if clicking is allowed. After a click on a tab you start the runnable and set mClickAllowed to false. The runnable will allow clicking after 500ms.

// Your Tab clickListner
public void onTabClick(View view) {
    if(mClickAllowed) {
        // When a tab is clicked, post the runnable
        mClickAllowed = false;
        mHandler.postDelayed(mRunnable, 500); // run after 500ms
    }
}

Dont forget to remove callbacks from your Handler after pausing your Activity

protected void onPause() {
    mHandler.removeCallbacks(mRunnable);
}

Upvotes: 1

Related Questions