动作条选项卡的内容重叠 [英] ActionBar tab contents overlapping

查看:114
本文介绍了动作条选项卡的内容重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一吨StackOverflow的这些消息。像其他许多人,我也有同样的问题与选项卡的内容切换标签时重叠。没有一个建议,我发现并没有与我的问题的工作。

I found a ton of these messages in StackOverflow. Like those many other people, I have the same problem with tab contents overlapping when switching tabs. None of the advises I found didn't work with my problem.

在我的应用程序启动时,它正确地显示了第一个选项卡的内容。当我点击其他选项卡,旧的内容留在屏幕上,而另一个选项卡的内容被添加在屏幕上了。当切换标签秒时间,所有的内容会消失。切换标签页不会做anyhting了。

When my app launches, it correctly shows the contents of the first tab. When I click the other tab, the old contents stay on the screen and the other tab's content is added on the screen, too. When switching tabs second time, all the contents disappear. Switching tabs won't do anyhting anymore.

我跟谷歌的开发者文档 href="http://developer.android.com/guide/topics/ui/actionbar.html#Tabs">。

I followed Google's Developer document here.

我的应用程序有这个的onCreate 方法。该级扩展 ActionBarActivity 从支持libary android.support.v7.app <​​/ code>。

My application has this onCreate method.. The class extends ActionBarActivity from the support libary android.support.v7.app.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    actionBar.setDisplayShowTitleEnabled(false);

    Tab tab = actionBar.newTab().setText("TAB1").setTabListener(new TabListener<Tab1Class>(this, "tab1", Tab1Class.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab().setText("TAB2").setTabListener(new TabListener<Tab2Class>(this, "tab2", Tab2Class.class));
    actionBar.addTab(tab);
}

我的 TabListener 类是从页我挂复制

public class TabListener<T extends Fragment> implements ActionBar.TabListener { private Fragment mFragment; private final Activity mActivity; private final String mTag; private final Class<T> mClass; public TabListener(Activity activity, String tag, Class<T> clz) { mActivity = activity; mTag = tag; mClass = clz; } @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { if(mFragment == null) { mFragment = Fragment.instantiate(mActivity, mClass.getName()); ft.add(android.R.id.content, mFragment, mTag); } else { ft.attach(mFragment); } } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { if(mFragment != null) { ft.detach(mFragment); } } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) {} }

Both classes I use as the contents of the tabs extend Fragment from android.support.v4.app.Fragment. They inflate their layouts in onCreateView methods.

我的选项卡的内容,同时使用的类 android.support.v4.app.Fragment 延长片段。他们在充气 onCreateView 方法及其布局。

What's wrong?

怎么了?

推荐答案

怎么了?

在快速浏览一下通过code为 ActionBarActivity ,似乎有对 ICS的错误及以上的执行部分的动作条(在code应该为pre ICS 设备),这也需要标签的照顾。

After a quick look through the code for the ActionBarActivity, there seems to be a bug for the ICS and above part of the implementation of the ActionBar(the code should work for pre ICS devices) which also takes care of the tabs.

ActionBarImplICS 类,它重新presents的实施 ICS 设备似乎 FragmentTransaction 传递给 onTabUnselected()的回调是完全无用的,因为它是不是听者的回调返回后的任何地方提交(该交易交付的另外两个回调 TabListener )。所以,一个坚定的碎片将永远不会从一个标签选择布局分离,这会留下越来越重叠的内容(由于的FrameLayout 持有两个片段)。

In the ActionBarImplICS class which represents the implementation for ICS devices it seems the FragmentTransaction passed to the onTabUnselected() callback is completely useless as it isn't committed anywhere after the listener's callback returns(the transaction is committed for the other two callbacks of the TabListener). So a committed fragment will never be detached from the layout on a tab selection and it will stay getting the overlapping content(due to the FrameLayout which holds both fragments).

我已经写了另外一个实施 TabListener 界面,完成所有的工作,从只有一个不受上述缺陷的回调(<$的C $ C> onTabSelected()):

I've written another implementation of the TabListener interface which does all of its job from only one of the callbacks which isn't affected by the above bug(onTabSelected()):

public class TabListenerImpl implements ActionBar.TabListener {

    private List<TabInfo> mTabs = new ArrayList<TabInfo>();
    private Context mContext;

    public TabListenerImpl(Context context) {
        mContext = context;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // iterate over all of the tabs, match the tag we have and see if
        // we also have a fragment instance for it. If we don't, create one
        // and add it to the container, if we have an instance simply attach
        // it. Detach every other tag which doesn't match the tag.
        for (TabInfo t : mTabs) {
            if (tab.getTag() == t.tag) {
                if (t.pageFragment == null) {
                    t.pageFragment = Fragment.instantiate(mContext,
                            t.clazz.getName());
                    ft.add(android.R.id.content, t.pageFragment, t.tag);
                } else {
                    ft.attach(t.pageFragment);
                }
            } else {
                if (t.pageFragment != null && !t.pageFragment.isDetached()) {
                    ft.detach(t.pageFragment);
                }
            }
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // faulty method
    }

    /**
     * Call this method BEFORE you call the actionBar.addTab() method!
     * 
     * @param tag
     *            a String representing the tag that was set on the tab to
     *            identify itself
     * @param clazz
     *            the class of the Fragment
     */
    public void addTab(String tag, Class<? extends Fragment> clazz) {
        TabInfo ti = new TabInfo();
        ti.clazz = clazz;
        ti.tag = tag;
        mTabs.add(ti);
    }

    // wrapper class
    private class TabInfo {
        Class<? extends Fragment> clazz;
        Fragment pageFragment;
        String tag;
    }

}

然后你可以为使用:

Which you could then use as:

TabListenerImpl listener = new TabListenerImpl(this);
Tab tab = actionBar.newTab().setText("TAB1").setTag("TAB1").setTabListener(listener);
listener.addTab("TAB1", Tab1Class.class);
actionBar.addTab(tab);

tab = actionBar.newTab().setText("TAB2").setTag("TAB2").setTabListener(listener);
listener.addTab("TAB2", Tab2Class.class);
actionBar.addTab(tab);

我会建议您设置一个容器的内容视图(也为选项卡的内容),而不是使用 android.R.id.content 容器。请记住,我的实现并不需要的配置更改的照顾。

I would advise you to set a container as the content view(and also for the tab content) and not use the android.R.id.content container. Keep in mind that my implementation doesn't take care of configuration changes.

这篇关于动作条选项卡的内容重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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