再次选择当前选项卡时收到通知

FrV*_*aBe 16 android

我实现了一个TabActivity实现的OnTabChangeListener.将在选项卡更改(onTabChanged(String tabId))上通知活动.

如果用户再次选择当前选项卡,是否也可以收到通知?

我想使用此事件来执行当前选项卡内容的"刷新",而不是在选项卡或选项菜单中提供刷新按钮.


这就是我最终解决问题的方式 - 解决方案提示是在MisterSquonk的回答中.

(1)定义一个OnTabReselectListener,它必须由表示选项卡内容的活动实现,并且将在重新选择事件上通知.

/**
 * 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();

}
Run Code Online (Sandbox Code Playgroud)

(2)TabActivity的onCreate()中的每个tabWidget子项的setOnTouchListener(来自MisterSquonk的答案)

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

(3)在TabActivity中实现OnTouchListener.onTouch().做一些逻辑来决定是否再次选择当前选项卡并且不知道选项卡的活动.

/**
 * @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;
}
Run Code Online (Sandbox Code Playgroud)

Squ*_*onk 9

您可以通过使用TabActivity实现View.onTouchListener并为每个选项卡调用setOnTouchListener来实现此功能...

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

然后在您的活动中覆盖onTouch()...

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

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

    return false;
}
Run Code Online (Sandbox Code Playgroud)

正如您在上面的评论中所看到的,我不确定如何处理onTouch侦听器中的View(v参数)以识别触摸了哪个Tab.

我可以确认无论您是否触摸当前选定的标签或未选中的标签,它都不会影响更改标签.

希望能帮助到你.