Android-TabWidget的程序化滚动

| 我在
HorizontalScrollView
内嵌套了一个
TabWidget
。我正在以编程方式设置“活动/当前”选项卡(我没有问题)。我的问题是,ѭ0不滚动以显示新的活动标签。 示例-我有9个标签,在任何给定时间仅显示3个标签(所以请考虑3个\'pages \'标签)。我在标签1(第1页)上。我以编程方式将当前选项卡设置为选项卡8(第3页)。选项卡内容正确切换(即,现在我看到选项卡8的内容),但是,顶部的选项卡仍显示选项卡1、2和3。如果手动向右滚动,则将看到选项卡8 (正确突出显示)。我想自动滚动the0,以便显示当前活动的标签。 有任何想法吗? 编辑示例源:
 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);
    }
    
已邀请:
您必须同时focusCurrentTab(int i)和setCurrentTab(int i)。 focusCurrentTab(i)会将TabWidget的UI集中到第ith个选项卡(所需),并且,如您所知,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);
}
    
在OnTabChangeListener旁边设置为TabHost:
@Override
public void onTabChanged(final String tag) {
    View tabView = tabHost.getCurrentTabView();
    int scrollPos = tabView.getLeft() - (tabsHorizontalScrollView.getWidth() - tabView.getWidth()) / 2;
    tabsHorizontalScrollView.smoothScrollTo(scrollPos, 0);
}
    
事实证明,“ 0”视图已将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.
为了解决这个问题,我遍历了“ 0”视图并将它们设置为在触摸模式下可聚焦:
for (int i = 0; i < count; i++) {
    getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}
    

要回复问题请先登录注册