我实现了一个实现OnTabChangeListener
的TabActivity
。 将在选项卡更改时通知活动( 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)TabActivity的onCreate()中的每个tabWidget子项的setOnTouchListener(来自MisterSquonk的答案)
for (int i = 0; i < tabWidget.getChildCount(); i++) { View v = tabWidget.getChildAt(i); v.setOnTouchListener(this); }
(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; }
您可以通过让TabActivity实现View.onTouchListener
并为每个选项卡调用setOnTouchListener来实现此function……
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; }
正如您在上面的评论中所看到的,我不知道如何处理onTouch侦听器中的View(v参数)以识别触摸了哪个Tab。
我可以确认无论您是否触摸当前选定的标签或未选中的标签,它都不会影响更改标签。
希望能帮助到你。
我对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组件。 因此,只需将layout-xml中的引用从TabHost更改为新的自定义类:
(注意android:id属性)
/汤米
不,这是不可能的。 以下是TabHost的代码
public void setCurrentTab(int index) { if (index < 0 || index >= mTabSpecs.size()) { return; } if (index == mCurrentTab) { return; } ...
您无权访问mCurrentTab,因此甚至不会调用侦听器。
但是,如果你真的想要你可以尝试reflection(例如使用Javareflection更改私有静态最终字段 )
我特别研究2.2并使用ActivityGroup来捆绑活动,因为片段是在API 11中引入的。所以,我不知道Fragments,但如果你正在使用ActivityGroup,这对我有用:
首先,感谢MisterSquonk为他“使用OnTouchListener
接口的想法!!”
1)使你的static TabHost tabhost
静态,即static TabHost tabhost
。 在ActivityGroup
,获取与static YourActivityGroup activitygroup;
相同的类(ActivityGroup)的static
类variablesstatic YourActivityGroup activitygroup;
并使用this
方法在onCreate
初始化,并创建一个方法来更改同一选项卡中的视图和活动。 喜欢:
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的新手。 任何问题将不胜感激。