在当前标签再次被选择留下您的信息 [英] Get notified when current tab is selected again

查看:131
本文介绍了在当前标签再次被选择留下您的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个 TabActivity 它实现了 OnTabChangeListener 。 该活动将在标签的变化( onTabChanged(字符串tabId))。

通知

是不是也有可能,如果用户再次选择当前选项卡得到通知?

我想用提供的选项卡或在选项菜单中的刷新按钮这一事件进行了刷新当前选项卡的内容,而不是。


就是这样,我终于解决了这个问题 - 解决方案的提示是 MisterSquonk 的答案

(1)定义,必须由重$ P $活动实现的OnTabReselectListener psents一个选项卡的内容,哪些将在重选事件通知。

  / **
 *要调用回调接口定义时,当前选中的标签
 *在TabHost再次被选中。
 * /
公共接口OnTabReselectListener {

    / **
     *当当前可见的选项卡再次选择调用。将不会被调用
     *在标签的变化。
     * /
    无效onTabReselect();

}
 

(2)setOnTouchListener每个tabWidget孩子的onCreate的TabActivity的()(从MisterSquonk的答案)

 的for(int i = 0; I< tabWidget.getChildCount();我++){
    视图v = tabWidget.getChildAt(ⅰ);
    v.setOnTouchListener(本);
}
 

(3)在TabActivity实施OnTouchListener.onTouch()。做一些逻辑,以决定是否在当前选项卡中再次选择和notifiy标签的活动。

  / **
 * @see android.view.View.OnTouchListener#onTouch(android.view.View,
 * android.view.MotionEvent)
 * /
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
    布尔消耗= FALSE;
    //使用getTabHost()。getCurrentTabView,以决定是否在当前选项卡
    //再次感动
    如果(event.getAction()== MotionEvent.ACTION_DOWN
            &功放;&安培; v.equals(getTabHost()。getCurrentTabView())){
        //使用getTabHost()。getCurrentView()来得到一个处理的看法
        //这显示在标签 - 并获得这方面的意见方面
        查看currentView = getTabHost()getCurrentView()。
        上下文currentViewContext = currentView.getContext();
        如果(currentViewContext的instanceof OnTabReselectListener){
            OnTabReselectListener监听=(OnTabReselectListener)currentViewContext;
            listener.onTabReselect();
            消费= TRUE;
        }
    }
    返回消耗;
}
 

解决方案

您可能能够得到这个通过让您TabActivity实施工作 View.onTouchListener 并调用setOnTouchListener每个标签...

 的for(int i = 0; I< tabWidget.getChildCount();我++){
    视图v = tabWidget.getChildAt(ⅰ);
    v.setOnTouchListener(本);
}
 

然后重写onTouch()在你的活动......

  @覆盖
公共布尔onTouch(视图V,MotionEvent事件){

    //不知道这里做什么来识别每个选项卡

    返回false;
}
 

正如您从注释在上面看到的,我不知道做什么用的视图(v参数)在onTouch侦听器,以确定哪些选项卡已被触及。

我可以证实它激发你是否不碰未选择当前选定的选项卡或一个,它不会改变制表干扰。

希望它帮助。

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;
}

解决方案

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.

这篇关于在当前标签再次被选择留下您的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆