Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

android - Get notified when current tab is selected again

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...