简体   繁体   English

如何在操作栏选项卡中显示所选片段

[英]How to show selected fragment in action bar tab

I am facing one issue regarding tab swipe. 我正面临一个关于标签滑动的问题。 My project is built on Android 3.2. 我的项目是基于Android 3.2构建的。 I am implementing tab swipe using support library 4.0 (android-support-v4.jar). 我正在使用支持库4.0(android-support-v4.jar)实现选项卡滑动。 Everything implemented is working fine but when I deploy my app to an ICS device, then in portrait mode I am getting a spinner in action bar for tab selection. 所有实现的工作都运行正常,但是当我将我的应用程序部署到ICS设备时,然后在纵向模式下,我在操作栏中获得了一个用于选项卡选择的微调器。 In portrait mode, the tab selection is not changing when swipe is done although content is changing, and everything is working fine in landscape mode. 在纵向模式下,尽管内容正在更改,但在完成滑动时选项卡选择不会更改,并且在横向模式下一切正常。

final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayHomeAsUpEnabled(true);
// Set up the ViewPager with the sections adapter.
ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);

mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        actionBar.setSelectedNavigationItem(position);
    }

});

I have tried putting breakpoint actionBar.setSelectedNavigationItem(position); 我试过把断点actionBar.setSelectedNavigationItem(position); on this line and even on portrait mode it's getting called but the selection is not changing. 在这条线上,甚至在肖像模式下它被调用,但选择没有改变。

Can anybody help with this? 任何人都可以帮忙吗?

EDITED: Found a similar problem but don't see exactly how it is solved and how to integrate it in my code. 编辑:发现了类似的问题,但没有看到它是如何解决的,以及如何将其集成到我的代码中。

Problem: Due to an insufficient real-state the platform uses collapsed navigation (ie Spinner). 问题:由于实际状态不足,平台使用折叠导航(即Spinner)。 The system auto-determines NAVIGATION_MODE_TABS for landscape & NAVIGATION_MODE_LIST for portrait, changing the orientation from landscape to portrait updates the UI but for some reason does not update the navigation mode property to NAVIGATION_MODE_LIST and hence mActionView.setDropdownSelectedPosition(position) is not called. 系统会自动确定横向的NAVIGATION_MODE_TABS和纵向的NAVIGATION_MODE_LIST,将方向从横向更改为纵向更新UI但由于某种原因不会将导航模式属性更新为NAVIGATION_MODE_LIST,因此不会调用mActionView.setDropdownSelectedPosition(位置)。 See the following code of ActionBarImpl : setSelectedNavigationItem 请参阅以下ActionBarImpl代码:setSelectedNavigationItem

  public void setSelectedNavigationItem(int position) { switch (mActionView.getNavigationMode()) { case NAVIGATION_MODE_TABS: selectTab(mTabs.get(position)); break; case NAVIGATION_MODE_LIST: mActionView.setDropdownSelectedPosition(position); break; default: throw new IllegalStateException( "setSelectedNavigationIndex not valid for current navigation mode"); } } 

Workaround solution: Through reflection we can get the tab spinner object and call setSelection method. 变通方法解决方案:通过反射,我们可以获取制表符微调器对象并调用setSelection方法。

private Spinner getTabSpinner()
{
    try
    {
        int id = getResources().getIdentifier("action_bar", "id", "android");
        View actionBarView = findViewById(id);

        Class<?> actionBarViewClass = actionBarView.getClass();
        Field mTabScrollViewField = actionBarViewClass.getDeclaredField("mTabScrollView");
        mTabScrollViewField.setAccessible(true);

        Object mTabScrollView = mTabScrollViewField.get(actionBarView);
        if (mTabScrollView == null) {
            return null;
        }

        Field mTabSpinnerField = mTabScrollView.getClass().getDeclaredField("mTabSpinner");
        mTabSpinnerField.setAccessible(true);

        Object mTabSpinner = mTabSpinnerField.get(mTabScrollView);
        if (mTabSpinner != null)
        {
            return (Spinner)mTabSpinner;
        }
    } 
    catch (Exception e) {
        return null;
    }

    return null;
}

Then call the above method in onPageSelected event. 然后在onPageSelected事件中调用上面的方法。

        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
            Spinner spinner = getTabSpinner();
            if (spinner != null) {
                spinner.setSelection(position);
            }
        }

Referred this post https://gist.github.com/2657485 推荐这篇文章https://gist.github.com/2657485

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM