开发者

Prevent TabHost Tab Change?

Is there any way with the stock Android TabHost to prevent a tab change from taking place ???

In iOS, there is a delegate callback called shouldSelectViewControlle开发者_Go百科r on the tabBarController, which if you return FALSE prevents the tab change from taking place.

Android has an onTabChanged() delegate, but that appears to be an after-the-fact notification that the tab change has taken place (it returns void).

Thanks.


If you don't want to use onTabChanged() for that, you can set OnClickListener/OnTouchListener for each tab and do it there. Example:

    for(int i=0; i<tabWidget.getTabCount(); i++) {
        tabWidget.getChildAt(i).setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();

                if (action == MotionEvent.ACTION_UP) {                      
                    String currentTabTag = (String) tabHost.getCurrentTabTag();
                    String clickedTabTag = (String) v.getTag();

                    if(clickedTabTag.equals("BAD TAG")) {
                        return true; // Prevents from clicking
                    }
                }
                return false;
            }               
        });
    }


If you want to prevent the tab from changing, for example you always want the tab be tab 0, try this code:

if (tabId.equals("Tab 1")) {
   // This will switch tab 1 to tab 0 immediately so that it seems like tab 0 does not change
  getTabHost().setCurrentTab(0);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜