Failed to change tab tabhost?

Is there any way with Android Android TabHost to prevent tabs from changing?

On iOS, there is a delegate callback called shouldSelectViewController on the tabBarController, which, if you return FALSE, prevents the tab from changing.

Android has a delegate onTabChanged (), but this seems to be a notification after the fact that a tab change has occurred (it returns void).

Thanks.

+4
source share
2 answers

If you do not want to use onTabChanged () for this, 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; } }); } 
+7
source

If you want the tab not to change, for example, you always want the tab to be on 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); } 
+2
source

Source: https://habr.com/ru/post/1347969/


All Articles