安卓:切换选项卡时,片段重叠

7

首先,感谢大家这个伟大的社区。

我正在尝试按照支持演示示例代码实现一个选项卡式片段接口。

最高层,我正在尝试实现两个选项卡,将单个片段与每个选项卡相关联,并在选择每个选项卡时相应地显示片段。

目前,我有两个问题(但我确定它们是相关的...)

1) 每个选项卡的片段彼此重叠。 这可能与不适当的片段附加/分离有关。

2) 第三个神秘的碎片在某个地方被创建并重叠其他碎片。


在模拟器上(以及在物理设备上),您可以看到选择任一选项卡时有两个重叠的片段

当选择选项卡1时, 片段1和未知片段重叠。

当选择选项卡2时, 片段1和片段2重叠。


链接到截图(没有足够的声望上传照片...)

(选项卡1重叠)http://s8.postimg.org/kv81yz745/tab1_overlapping.png

(选项卡2重叠)http://s8.postimg.org/3tf7wvs91/tab2_overlapping.png


在这里,我已经为演示/清晰度目的分离了每个片段的文本。

这些截图的链接在我的评论/回复中(没有足够的声望上传超过2个链接...)

活动布局(fragment_tabs.xml)

<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TabWidget
        android:id="@android:id/tabs"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>
</TabHost>

源代码

public class TabbedInfoHome extends SherlockFragmentActivity {

TabHost mTabHost;
TabManager mTabManager;

static String tag1name = "simple1";
static String tag2name = "simple2";

static String tab1string = "You are looking at fragment 1";
static String tab2string = "You are looking at fragment 2";


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

    setContentView(R.layout.fragment_tabs); 

    if (savedInstanceState == null) {
        // Do first time initialization -- add initial fragment.
        Fragment frag1 = CountingFragment.newInstance(tab1string);
        Fragment frag2 = CountingFragment.newInstance(tab2string);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        ft.add(R.id.realtabcontent, frag1, tag1name);
        ft.add(R.id.realtabcontent, frag2, tag2name);

        ft.commit();
    } 
    else {
        mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
    }    

    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();

    mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);

    mTabManager.addTab(mTabHost.newTabSpec(tag1name)
                .setIndicator(tag1name),
                TabbedInfoHome.CountingFragment.class,
                null);

    mTabManager.addTab(mTabHost.newTabSpec(tag2name)
                .setIndicator(tag2name),
                TabbedInfoHome.CountingFragment.class, 
                null);
}   

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("tab", mTabHost.getCurrentTabTag());
}

public static class CountingFragment extends SherlockFragment {
    String displayString;
    String FRAGMENT_TAG = this.getClass().getSimpleName();



    /**
     * Create a new instance of CountingFragment, providing "num"
     * as an argument.
     */
    static CountingFragment newInstance(String toDisplay) {
        CountingFragment f = new CountingFragment();

        Bundle args = new Bundle();
        args.putString("string", toDisplay);
        f.setArguments(args);
        return f;
    }

     /* When creating, retrieve this instance's number from its arguments. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);         
        displayString = getArguments() != null ? getArguments().getString("string") : "no string was passed in!";
    }


    /* The Fragment's UI is just a simple text view showing its
     * instance number. */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.hello_world, container, false);
        View tv = v.findViewById(R.id.text);

        boolean separateStrings = false;

        /* the overlapping is hard to decipher, so
         *  lets illustrate how both fragments are appearing */
        if(separateStrings) {

            String temp;

            /* b/c I only created TWO instances of the CountingFragments object, 
             *  there should only be TWO "displayStrings" to consider...
             */
            if( (displayString.compareTo(tab1string) == 0) ) {
                /* user clicked tab 1 */
                temp = "\n\n\n\n" + displayString;
            }
            else if( (displayString.compareTo(tab2string) == 0) ) {
                /* user clicked tab2 */
                temp = "\n\n\n\n\n\n\n" + displayString;
            }
            else {
                /* unknown CountingFragment instance */
                temp = "What am I doing here..??? ";
            }

            ((TextView)tv).setText(temp);

        }
        else {
            /* normal case of the fragment being shown; (but they overlap!) */
            ((TextView)tv).setText(displayString);
        }


        return v;
    }
}

/**
 * This is a helper class that implements a generic mechanism for
 * associating fragments with the tabs in a tab host.  It relies on a
 * trick.  Normally a tab host has a simple API for supplying a View or
 * Intent that each tab will show.  This is not sufficient for switching
 * between fragments.  So instead we make the content part of the tab host
 * 0dp high (it is not shown) and the TabManager supplies its own dummy
 * view to show as the tab content.  It listens to changes in tabs, and takes
 * care of switch to the correct fragment shown in a separate content area
 * whenever the selected tab changes.
 */
public static class TabManager implements TabHost.OnTabChangeListener {
    private final FragmentActivity mActivity;
    private final TabHost mTabHost;
    private final int mContainerId;
    private final HashMap<String, TabInfo> mTabs = new HashMap<String, TabInfo>();
    TabInfo mLastTab;

    static final class TabInfo {
        private final String tag;
        private final Class<?> clss;
        private final Bundle args;
        private Fragment fragment;

        TabInfo(String _tag, Class<?> _class, Bundle _args) {
            tag = _tag;
            clss = _class;
            args = _args;
        }
    }

    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context mContext;

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

        @Override
        public View createTabContent(String tag) {
            View v = new View(mContext);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);
            return v;
        }
    }

    public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
        mActivity = activity;
        mTabHost = tabHost;
        mContainerId = containerId;
        mTabHost.setOnTabChangedListener(this);
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {

        tabSpec.setContent(new DummyTabFactory(mActivity));
        String tag = tabSpec.getTag();

        TabInfo info = new TabInfo(tag, clss, args);

        // Check to see if we already have a fragment for this tab, probably
        // from a previously saved state.  If so, deactivate it, because our
        // initial state is that a tab isn't shown.
        info.fragment = mActivity.getSupportFragmentManager().findFragmentByTag(tag);


        if (info.fragment != null ) {  // && !info.fragment.isDetached()) {
            Log.d("addingTab", "we already have a fragment for this tab. tabInfo.fragment.id: " + info.fragment.getId());
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
            ft.detach(info.fragment);
            ft.commit();
            mActivity.getSupportFragmentManager().executePendingTransactions();
        }

        // associate the tabSpec tag with a particular TabInfo object
        mTabs.put(tag, info);
        mTabHost.addTab(tabSpec);
    }

    @Override
    public void onTabChanged(String tabId) {
        TabInfo newTab = mTabs.get(tabId);

        if (mLastTab != newTab) {
            FragmentTransaction ft = mActivity.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                    ft.detach(mLastTab.fragment);
                }
            }

            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(mActivity,
                            newTab.clss.getName(), newTab.args);

                    ft.add(mContainerId, newTab.fragment, newTab.tag);
                } else {
                    ft.attach(newTab.fragment);
                }
            }

            mLastTab = newTab;
            ft.commit();
            mActivity.getSupportFragmentManager().executePendingTransactions();
        }
    }

}   

两个重叠的片段的截图,为了演示/清晰起见分开了(选项卡1分开)http://s8.postimg.org/gorsxdww5/tab1_separated.png(选项卡2分开)http://s8.postimg.org/az6yzc1c5/tab2_separated.png - Jimmy Ng
4个回答

10

经过多天的尝试,我成功让它正常工作。

在我的TabbedInfoHome类的onCreate方法中,当首次创建片段的新实例(savedInstanceState == null)时,我使用this.getSupportFragmentManager().executePendingTransactions()强制执行了FragmentTransaction中挂起的事务。

这个说明基于在以下文档中找到的信息:http://developer.android.com/reference/android/app/FragmentManager.html#executePendingTransactions()

在使用FragmentTransaction.commit()提交FragmentTransaction后,它会被异步地安排在进程的主线程上执行。如果你想立即执行任何此类挂起操作,可以调用此函数。

我仍然有一个未解决的问题是,缺少立即执行挂起事务将如何表现为原来提问中看到的重叠片段。

换句话说……缺少this.getSupportFragmentManager().executePendingTransactions()如何解释重叠片段?


编辑后的代码如下;增加了一行

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

setContentView(R.layout.fragment_tabs); 

if (savedInstanceState == null) {
    // Do first time initialization -- add initial fragment.
    Fragment frag1 = CountingFragment.newInstance(tab1string);
    Fragment frag2 = CountingFragment.newInstance(tab2string);
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

    ft.add(R.id.realtabcontent, frag1, tag1name);
    ft.add(R.id.realtabcontent, frag2, tag2name);

    ft.commit();
    this.getSupportFragmentManager().executePendingTransactions();      // <----- This is the key 
} 
else {
    mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
}    

mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();

mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);

mTabManager.addTab(mTabHost.newTabSpec(tag1name)
            .setIndicator(tag1name),
            TabbedInfoHome.CountingFragment.class,
            null);

mTabManager.addTab(mTabHost.newTabSpec(tag2name)
            .setIndicator(tag2name),
            TabbedInfoHome.CountingFragment.class, 
            null);
}    

我认为应该说,我的SherlockActivity使用与这个问题无关。谢谢。 - Jimmy Ng
executePendingTransactions()并没有解决我在DrawerLayoutNavigationView中遇到的同样问题。如果我太快地切换Fragment,它们仍然会重叠(是的,只有这种情况下)... - shkschneider

1
我在Nexus 5上也遇到了这个问题。
我认为我们有比删除解决方案更好的解决方法,可以通过以下方式实现:
在“所有xml文件”中,应该为其定义背景颜色,这将解决问题:
在您定义的View标签中添加此android:background="@android:color/black"

0

我也在尝试做同样的事情,今天早上遇到了同样的问题。我通过另一个类似的问题得到的信息解决了它们。

在我的情况下,我没有使用SherlockActionBar,但是这个解决方案对你也应该适用。

在检查选项卡是否填充了片段的部分,我使用了以下代码:

             // in order to avoid fragment 
     Fragment prevFragment;
        FragmentManager fm = mActivity.getFragmentManager();
        prevFragment = fm.findFragmentByTag(mTag); 
        if (prevFragment != null) { 
            mFragment = prevFragment; 
        } // \previous Fragment management

如果你将它适应SherlockActionBar命令,我想这应该对你有用(因为我阅读信息的地方他们使用了SherlockActionBar)。

希望它能对你有用!


谢谢您的回复,@mrgdiez。我还无法完全理解您的修复程序如何适用于我的原始代码?此外,您提到从另一个类似的问题中获取了一些信息。您能否发布该问题的链接,以便其他阅读此帖子的人也可以参考一下?无论如何再次感谢您的回复! - Jimmy Ng
你好 @Jimmy Ng,我没有发布链接,因为当我回答你的问题时,我再也找不到它了...我正在进行一个项目,几乎没有时间...非常抱歉我的回答,当我第一次阅读你的问题时,我认为这是我遇到的同样的问题,但现在我意识到不是。我是一个Android的初学者,所以再次道歉。 - mgrdiez

0

已经有很多解决方案了,但是由于一个愚蠢的错误,我无法解决这个问题。问题在于,在新的SDK中,默认情况下Activity被扩展为“ActionbarActivity”,其中Fragment被创建但它们会重叠。要解决这个问题,请将您的Activity扩展到“FragmentActivity”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接