Android - Programmatic scrolling of TabWidget

老子叫甜甜 提交于 2019-12-06 11:02:01

问题


I have a TabWidget nested within a HorizontalScrollView. I am programmatically setting the active/current tab (which I have no problem doing). My problem is, the TabWidget is not scrolling to show thenewly active tab.

Example - I have 9 tabs, only 3 are shown at any given time (so think 3 'pages' of tabs). I am on tab 1 (page 1). I programmatically set the current tab to tab 8 (page 3). The tab content switches properly (ie, I now see the contents of tab 8), however, the tabs up at the top still show tabs 1, 2, and 3. If I manually scroll over to the right, I will see tab 8 (properly highlighted). I want to automatically scroll the TabWidget so that the currently active tab is shown.

Any ideas?

EDIT example source:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        String tag;

        /* logic to populate tag from intent*/

        /* reason for this is because I know the tag of the tab I need, 
           not the index num. of tab */
        getTabHost().setCurrentTabByTag(tag);
        int i = getTabHost().getCurrentTab();

        getTabWidget().setCurrentTab(i);
        getTabWidget().focusCurrentTab(i);
    }

回答1:


You must both focusCurrentTab(int i) and setCurrentTab(int i). focusCurrentTab(i) will focus the UI of the TabWidget to the ith tab (what you want), and, as you already know, setCurrentTab(i) will focus the actual content of the ith tab in the main View.

For reference: http://developer.android.com/reference/android/widget/TabWidget.html#focusCurrentTab(int)

EDIT:

My answer was incorrect. This is the correct one (that the the poster found out themself):

It turns out that the TabWidget views had focusableInTouchMode set to false. As per View.java documentation:

A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false), or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode}) while the device is in touch mode. To correct the problem I iterated over the TabWidget views and set them focusable in touch mode:

for (int i = 0; i < count; i++) {
    getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}



回答2:


set to TabHost next OnTabChangeListener:

@Override
public void onTabChanged(final String tag) {
    View tabView = tabHost.getCurrentTabView();
    int scrollPos = tabView.getLeft() - (tabsHorizontalScrollView.getWidth() - tabView.getWidth()) / 2;
    tabsHorizontalScrollView.smoothScrollTo(scrollPos, 0);
}



回答3:


It turns out that the TabWidget views had focusableInTouchMode set to false. As per View.java documentation:

A view will not actually take focus if it is not focusable ({@link #isFocusable} 
returns false), or if it is focusable and it is not focusable in touch mode 
({@link #isFocusableInTouchMode}) while the device is in touch mode.

To correct the problem I iterated over the TabWidget views and set them focusable in touch mode:

for (int i = 0; i < count; i++) {
    getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}


来源:https://stackoverflow.com/questions/6131218/android-programmatic-scrolling-of-tabwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!