2011-03-30 62 views
16

我实施了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(); 

} 

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

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

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

/** 
* @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; 
} 
+0

你用什么来设置标签的指标? – Squonk 2011-03-30 15:48:56

+0

@MisterSquonk我正在使用一个字符串。看我的编辑。 – FrVaBe 2011-03-30 16:53:56

+0

看到我的答案可能的角度来探索。 – Squonk 2011-03-30 22:10:38

回答

9

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

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

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

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

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

    return false; 
} 

正如你可以从ABOV评论见e,我不确定如何处理onTouch侦听器中的View(v参数),以确定哪个Tab已被触摸。

我可以确认是否触摸当前选择的选项卡或未选中的选项卡,并且它不会干扰更改选项卡。

希望它有帮助。

+0

感谢这个解决方案的可能性。它为我工作。我添加了我最终对我的问题的方式(因为我不允许编辑您的答案)。 – FrVaBe 2011-03-31 10:14:19

+2

您的解决方案正在为我工​​作,谢谢 – Dharmendra 2011-10-21 09:35:27

+0

请参阅下面的链接, http://stackoverflow.com/questions/9372851/how-to-detecting-a-click-on-anready-selected-tab-button/ 17876162#17876162 – 2013-07-26 08:11:25

1

不,这是不可能的。下面是TabHost

public void setCurrentTab(int index) { 
    if (index < 0 || index >= mTabSpecs.size()) { 
     return; 
    } 

    if (index == mCurrentTab) { 
     return; 
    } 
    ... 

您没有访问mCurrentTab代码,所以听者甚至没有调用。

但是,如果你真的想你可以尝试反射(Change private static final field using Java reflection例如)

+1

这是一个有用的提示来看看TabHost代码。我试图扩展一个'CustomTabHost',它会通知一个'CustomOnTabSelectListener',但现在看起来很难让TabActivity使用这个自定义的TabHost - 我不想做太多'Custom'的东西.... +1那一刻(如果没有人顶的话,会接受你的回答) – FrVaBe 2011-03-30 16:20:15

4

我对Android很陌生,但我想我对这个问题有一个更好的解决方法。

首先(如前所述)在TabHost中的这段代码。Java使得它不可能侦听一个标签按钮第二点击:

public void setCurrentTab(int index) { 
if (index < 0 || index >= mTabSpecs.size()) { 
    return; 
} 

if (index == mCurrentTab) { 
    return; 
} 
... 

所以创建自己的TabHost的启动子(com.mydomain.CustomTabHost)

@Override 
public void setCurrentTab(int index) { 
if (this.getCurrentTab() == index) { 
     // User clicked active tab... 
    } 
    super.setCurrentTab(index); 
} 

你tabActivity正在寻找一个ID为“tabhost”的TabHost组件。因此,只要改变从TabHost布局的XML引用您的新的自定义类:

<com.yourdomain.CustomTabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent">

(注意了android:id属性)

/汤米

+0

+1这个替代解决方案 - 没有检查它的细节,但它应该工作。 – FrVaBe 2011-05-04 18:45:41

0

我尤其对2.2工作并使用ActivityGroup作为碎片在API 11中引入的捆绑活动。 所以,我不知道碎片,但如果您正在使用ActivityGroup,这适用于我:

首先,为MisterSquonk为他的“使用OnTouchListener界面的想法!!”

1)使您的TabHost静态,即static TabHost tabhost。在您的ActivityGroup中,采用与static YourActivityGroup activitygroup;相同的类(ActivityGroup)的static类变量,并在onCreate中用this进行初始化,并创建一个方法来更改同一选项卡中的视图和活动。像:

public void replaceContentView(String activityIdOrTag,Intent activityIntent) 
    {     
     View v= getLocalActivityManager().startActivity(activityIdOrTag,activityIntent) 
        .getDecorView(); 
    this.setContentView(v); 
} 

2)简单地实现OnTouchListener您的任何活动,其中包括在你的ActivityGroup或我建议,落实到你的ActivityGroup

3)设置一个OnTouchListener

YourTabActivity.tabhost.getCurrentTabView().setOnTouchListener(this)

4)现在OnTouch(View v,MotionEvent e)

if (e.getAction()==MotionEvent.ACTION_DOWN) 
    { 
    Intent backToTop = new Intent(this, YourTopLevel.class); 
    YourActivityGroup.activitygroup.replaceContentView("YourTopActivity", backToTop); 
    } 

希望这有助于你。我是Android新手。任何问题将不胜感激。

+0

感谢这个替代解决方案。不幸的是,我目前无法检查它。如果有人'喜欢',他应该可以随时加入! – FrVaBe 2012-04-12 12:45:17

相关问题