Android的 - TabWidget的纲领性滚动 [英] Android - Programmatic scrolling of TabWidget

查看:154
本文介绍了Android的 - TabWidget的纲领性滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TabWidget 嵌套在 Horizo​​ntalScrollView 。我以编程方式设置活动/当前选项卡(其中我有没有问题做的)。我的问题是,在 TabWidget 不滚动显示thenewly活动标签。

示例 - 我有9个标签,只有3显示在任意给定时间(有这样想法的选项卡3页)。我的标签1(第1页)。我以编程方式设置当前选项卡标签8(第3页)。该选项卡内容切换正常(即我现在看到标签8中的内容),但是,标签在顶部仍然显示选项卡1,2,和3。如果我手动滚动到合适的,我会看标签8 (正确的高亮显示)。我想自动滚动 TabWidget ,这样当前活动的选项卡显示。

任何想法?

修改示例源:

 保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(结果code == RESULT_OK){
        字符串标记;

        / *逻辑的意图填充标记* /

        / *的原因是因为我知道我需要的选项卡的标签,
           而不是索引NUM。标签* /
        getTabHost()setCurrentTabByTag(标签)。
        INT I = getTabHost()getCurrentTab()。

        getTabWidget()setCurrentTab(i)中。;
        。getTabWidget()focusCurrentTab(ⅰ);
    }
 

解决方案

您必须既focusCurrentTab(int i)以和setCurrentTab(int i)以。 focusCurrentTab(我)将专注于TabWidget到第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<计数;我++){
    。getTabWidget()getChildAt(我).setFocusableInTouchMode(真正的);
}
 

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

解决方案

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

这篇关于Android的 - TabWidget的纲领性滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆