2

我有一个TabWidget嵌套在HorizontalScrollView. 我正在以编程方式设置活动/当前选项卡(我没有问题)。我的问题是,TabWidget没有滚动显示新的活动选项卡。

示例- 我有 9 个选项卡,在任何给定时间只显示 3 个(所以想想 3 个选项卡的“页面”)。我在标签 1(第 1 页)上。我以编程方式将当前选项卡设置为选项卡 8(第 3 页)。选项卡内容正确切换(即,我现在看到选项卡 8 的内容),但是,顶部的选项卡仍显示选项卡 1、2 和 3。如果我手动向右滚动,我将看到选项卡 8 (正确突出显示)。我想自动滚动TabWidget以显示当前活动的选项卡。

有任何想法吗?

编辑示例源:

 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);
    }
4

3 回答 3

2

设置为 TabHost 下一个 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);
}
于 2014-09-16T14:56:56.193 回答
1

事实证明,TabWidget视图将 focusableInTouchMode 设置为 false。根据 View.java 文档:

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.

为了纠正这个问题,我迭代了TabWidget视图并将它们设置为在触摸模式下可聚焦:

for (int i = 0; i < count; i++) {
    getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}
于 2011-05-27T19:52:29.547 回答
0

您必须同时使用 focusCurrentTab(int i) 和 setCurrentTab(int i)。focusCurrentTab(i) 将 TabWidget 的 UI 聚焦到第 i 个选项卡(您想要的),并且,如您所知,setCurrentTab(i) 将聚焦主视图中第 i 个选项卡的实际内容。

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

编辑:

我的回答不正确。这是正确的(发帖人自己发现的):

事实证明,TabWidget 视图的 focusableInTouchMode 设置为 false。根据 View.java 文档:

如果视图不可聚焦({@link #isFocusable} 返回 false),或者它可聚焦但在设备处于触摸状态时在触摸模式下不可聚焦({@link #isFocusableInTouchMode}),则视图实际上不会获得焦点模式。为了纠正这个问题,我遍历了 TabWidget 视图并将它们设置为在触摸模式下可聚焦:

for (int i = 0; i < count; i++) {
    getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}
于 2011-05-25T22:05:23.483 回答