如何显示在操作栏中的选项卡中选择片段 [英] How to show selected fragment in action bar tab

查看:160
本文介绍了如何显示在操作栏中的选项卡中选择片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面对的就卡刷卡问题。我的项目是建立在Android 3.2。我采取使用支持库4.0(Android的支持 - v4.jar)选项卡刷卡。实现一切工作正常,但是当我部署我的应用程序的ICS的设备,然后在纵向模式下我得到在行动酒吧微调的标签选择。在纵向模式下,标签的选择是不会改变的时候刷卡完成,虽然内容在不断变化,一切工作正常在横向模式。

 最后的动作条动作条= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayHomeAsUpEnabled(真正的);
//设置的ViewPager与部分适配器。
ViewPager mViewPager =(ViewPager)findViewById(R.id.pager);

mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener(){

    @覆盖
    公共无效onPageSelected(INT位置){
        actionBar.setSelectedNavigationItem(位置);
    }

});
 

我试图把断点 actionBar.setSelectedNavigationItem(位置); 在这条线上,甚至在肖像模式下它获取调用,但选择不改变

任何人可以帮助呢?

编辑: 发现一个<一个href="http://stackoverflow.com/questions/11030744/actionbar-selecttab-doesnt-refresh-spinner-in-compact-mode">similar问题但看不到究竟它是如何解决,如何在我的code集成。

解决方案

问题: 由于不充分的现实状态下的平台采用折叠导航(即微调)。该系统自动确定NAVIGATION_MODE_TABS景观和放大器; NAVIGATION_MODE_LIST肖像,从横向到纵向改变方向更新的用户界面,但由于某些原因不更新的导航模式属性NAVIGATION_MODE_LIST因此mActionView.setDropdownSelectedPosition(位置)没有被调用。见ActionBarImpl以下code:setSelectedNavigationItem

 公共无效setSelectedNavigationItem(INT位置){
    开关(mActionView.getNavigationMode()){
    案例NAVIGATION_MODE_TABS:
        selectTab(mTabs.get(位置));
        打破;
    案例NAVIGATION_MODE_LIST:
        mActionView.setDropdownSelectedPosition(位置);
        打破;
    默认:
        抛出新IllegalStateException异常(
                setSelectedNavigationIndex不适用于当前的导航模式);
    }
}
 

解决方法解决方案: 通过反思,我们可以得到的标签微调对象,并调用setSelection方法。

 私人微调getTabSpinner()
{
    尝试
    {
        INT ID = getResources()则getIdentifier(action_bar,ID,机器人)。
        查看actionBarView = findViewById(ID);

        类&LT;&GT; actionBarViewClass = actionBarView.getClass();
        现场mTabScrollViewField = actionBarViewClass.getDeclaredField(mTabScrollView);
        mTabScrollViewField.setAccessible(真正的);

        对象mTabScrollView = mTab​​ScrollViewField.get(actionBarView);
        如果(mTabScrollView == NULL){
            返回null;
        }

        现场mTabSpinnerField = mTab​​ScrollView.getClass()getDeclaredField(mTabSpinner)。
        mTabSpinnerField.setAccessible(真正的);

        对象mTabSpinner = mTab​​SpinnerField.get(mTabScrollView);
        如果(mTabSpinner!= NULL)
        {
            返回(微调)mTabSpinner;
        }
    }
    赶上(例外五){
        返回null;
    }

    返回null;
}
 

然后调用onPageSelected事件上面的方法。

 公共无效onPageSelected(INT位置){
            actionBar.setSelectedNavigationItem(位置);
            微调微调= getTabSpinner();
            如果(微调!= NULL){
                spinner.setSelection(位置);
            }
        }
 

提到这个帖子 https://gist.github.com/2657485

I am facing one issue regarding tab swipe. My project is built on Android 3.2. I am implementing tab swipe using support library 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. 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); 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 (i.e. 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. See the following code of 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.

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.

        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

这篇关于如何显示在操作栏中的选项卡中选择片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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