Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-TabWidget的程序化滚动_Android_Tabs_Scroll_Tabwidget - Fatal编程技术网

Android-TabWidget的程序化滚动

Android-TabWidget的程序化滚动,android,tabs,scroll,tabwidget,Android,Tabs,Scroll,Tabwidget,我有一个TabWidget嵌套在水平滚动视图中。我正在以编程方式设置“活动/当前”选项卡(这样做没有问题)。我的问题是,TabWidget没有滚动显示新的活动选项卡 示例-我有9个选项卡,在任何给定时间只显示3个选项卡(因此请考虑3页选项卡)。我在表1(第1页)上。我以编程方式将当前选项卡设置为选项卡8(第3页)。选项卡内容正确切换(即,我现在看到了选项卡8的内容),但是,顶部的选项卡仍然显示选项卡1、2和3。如果我手动向右滚动,我将看到选项卡8(正确高亮显示)。我想自动滚动TabWidget,

我有一个
TabWidget
嵌套在
水平滚动视图中
。我正在以编程方式设置“活动/当前”选项卡(这样做没有问题)。我的问题是,
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);
    }

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

供参考:)

编辑:

我的回答不正确。这是正确的一个(海报自己发现的):

事实证明,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.
如果视图不可聚焦({@link#isFocusable}),它就不会真正聚焦 返回false),或者在触摸模式下可聚焦且不可聚焦 ({@link#isFocusableInTouchMode})设备处于触摸模式时。 为了纠正这个问题,我迭代了TabWidget视图,并将它们设置为可在触摸模式下聚焦:

for (int i = 0; i < count; i++) {
    getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}
for (int i = 0; i < count; i++) {
    getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}
for(int i=0;i
结果是
选项卡widget
视图的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.
为了纠正这个问题,我反复浏览了
选项卡widget
视图,并将它们设置为可在触摸模式下聚焦:

for (int i = 0; i < count; i++) {
    getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}
for (int i = 0; i < count; i++) {
    getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}
for(int i=0;i
设置为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);
}

@Vinay我在这方面取得了一些成功(10次中可能有1次)。由于它是如此简练,这让我相信可能存在竞争条件/计时/异步问题。基本上在
TabHost
级别,我调用setCurrentTabByTag(tag),然后说i=getCurrentTab()。然后在
TabWidget
上,我调用setCurrentTab(I)和focusCurrentTab(I)。有什么见解吗?当它不起作用时到底发生了什么?比如,它是否将它设置为某个标签,而该标签与您想要的标签相距甚远?它搞砸的方式有规律吗?为什么不提供相关的代码呢。对我来说,这可能是最容易看出哪里出了问题的方法。@Vinay我编辑了原始文章以包含代码。我很抱歉没有更清楚我所说的成功是什么意思,因为我之前很匆忙。通过一些成功,我的意思是,有时水平滚动的选项卡栏滚动到新的当前选项卡,而其他时候,选项卡不会移动以反映新选择的选项卡。
framelayout
内容正确切换,当前选项卡被正确选择/突出显示,但是,有时
tabhost
会“滚动”以显示当前选项卡,有时它不会移动。看起来您正在设置两次当前选项卡。我真的不知道为什么那样会把事情搞得一团糟,但这里可能就是这样。让我们试试看。试着注释掉“getTabWidget().setCurrentTab(i);”,看看会发生什么。如果这不能使它按您想要的方式工作,请取消对该部分的注释,然后注释掉“getTabWidget().focusCurrentTab(i);”。给我回电话,告诉我努力的结果。我很好奇为什么现在不起作用。@Vinay两个都不走运。。。另外,检查TabHost.java并在方法调用之后,setCurrentTabByTag(index)似乎调用了单独的TabWidgets focusCurrentTab,因此理论上我甚至不必进行该调用(focusCurrentTab反过来也调用setCurrentTab(index),因此在这少量代码中有许多重复的步骤)。最终结果是,当通过编程设置新选项卡时,TabHost/TabWidgets仍然无法滚动显示新的当前选项卡。注意这是在TabActivity中完成的(可以通过API调用收集到),我将在早上手动尝试。在这个逻辑选项卡小部件焦点工作正常后,我在水平滚动视图中添加了选项卡小部件。但是当尝试滚动选项卡小部件时,请建议如何避免此问题。