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;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…