Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get notified when current tab is selected again

Tags:

android

I implemented a TabActivity which implements the OnTabChangeListener. The activity will be notified on tab changes (onTabChanged(String tabId)).

Is it also possible to get notified if the user selects the current tab again?

I would like to use this event to perform a "refresh" of the current tab content instead of providing a refresh button on the tab or in the options menu.


That's the way I finally solved the problem - solution hint was in MisterSquonk answer.

(1) Define a OnTabReselectListener which must be implemented by an activity which represents a tab content and which will be notified on reselect events.

/**
 * Interface definition for a callback to be invoked when a current selected tab
 * in a TabHost is selected again.
 */
public interface OnTabReselectListener {

    /**
     * Called when a current visible tab is selected again. Will not be invoked
     * on tab changes.
     */
    void onTabReselect();

}

(2) setOnTouchListener for each tabWidget child in onCreate() of the TabActivity (from MisterSquonk's answer)

for (int i = 0; i < tabWidget.getChildCount(); i++) {
    View v = tabWidget.getChildAt(i);
    v.setOnTouchListener(this);
}

(3) Implement OnTouchListener.onTouch() in the TabActivity. Do some logic to decide if the current tab was selected again and notifiy the tab's activity.

/**
 * @see android.view.View.OnTouchListener#onTouch(android.view.View,
 *      android.view.MotionEvent)
 */
@Override
public boolean onTouch(View v, MotionEvent event) {
    boolean consumed = false;
    // use getTabHost().getCurrentTabView to decide if the current tab is
    // touched again
    if (event.getAction() == MotionEvent.ACTION_DOWN
            && v.equals(getTabHost().getCurrentTabView())) {
        // use getTabHost().getCurrentView() to get a handle to the view
        // which is displayed in the tab - and to get this views context
        View currentView = getTabHost().getCurrentView();
        Context currentViewContext = currentView.getContext();
        if (currentViewContext instanceof OnTabReselectListener) {
            OnTabReselectListener listener = (OnTabReselectListener) currentViewContext;
            listener.onTabReselect();
            consumed = true;
        }
    }
    return consumed;
}
like image 512
FrVaBe Avatar asked Mar 30 '11 14:03

FrVaBe


1 Answers

You might be able to get this to work by having your TabActivity implement View.onTouchListener and calling setOnTouchListener for each of the tabs...

for (int i = 0; i < tabWidget.getChildCount(); i++) {
    View v = tabWidget.getChildAt(i);
    v.setOnTouchListener(this);
}

Then override onTouch() in your activity...

@Override
public boolean onTouch(View v, MotionEvent event) {

    // Not sure what to do here to identify each tab

    return false;
}

As you can see from the comment in the above, I'm not sure what to do with the View (v parameter) in the onTouch listener to identify which Tab has been touched.

I can confirm it fires whether or not you touch the currently selected tab or one which isn't selected and it doesn't interfere with changing tabs.

Hope it helps.

like image 118
Squonk Avatar answered Oct 21 '22 22:10

Squonk