片段无法与viewpager片段取代 [英] Fragment was unable to replace with fragment in viewpager

查看:173
本文介绍了片段无法与viewpager片段取代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与ViewPager概念碎片的工作。我创建中,我只使用其中一个得到它从数据库中,并显示所有更新片段的日记应用。直到这一切都很好。根据我的要求,当我点击一个按钮,在片段我需要显示的另一个片段,它允许用户存储图像。
我的问题是.....

I'm working with fragments with ViewPager concept. I'm creating a diary app in which I'm using only one fragment which gets all updates from database and show in it. Until this everything is fine...According to my requirement when i click on a button in fragment i need to show another fragment which allows the user to store the images. My problem is.....

- 如果我使用它没有在这个意义上正确地更换,如果A是由viewpager的片段,B是我想更换一个片段片段替换方法结果
   --Then如果我使用替代B是可见的,但A也将碎片B下显示

--If i use replace method in fragments it was not replacing properly in the sense if A is fragment which consists of viewpager and B is a fragment i want to replace.
--Then if i use replace B is visible but A also appears under the fragment B

FragmentManager m=getActivity().getSupportFragmentManager();
                FragmentTransaction ft = getActivity().getSupportFragmentManager()
                        .beginTransaction();
                Demobutton demobutton = new Demobutton();

                ft.replace(R.id.lay, demobutton);
                ft.commit();

希望你们明白我的问题。如果你觉得我的问题是不完整的,请让我知道。

Hope you guys understand my problem. If you feel my question is incomplete please let me know that.

推荐答案

我已经开发了一个小例子的应用程序,达到这个不覆盖本机类。

I've developed a small example app that achieves this without overwriting native classes.

的点是使用一个片段作为容器

The point is to use a fragment as a container.

在您ViewPagerAdapter:

In your ViewPagerAdapter:

@Override
public Fragment getItem(int position) {
    /*
     * IMPORTANT: This is the point. We create a RootFragment acting as
     * a container for other fragments
     */
    if (position == 0)
        return new RootFragment();
    else
        return new StaticFragment();
}

RootFragment版面应该像:

RootFragment layout should look like:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/root_frame" >
</FrameLayout>

您可以在这里查看我的完整的解释:<一href=\"http://stackoverflow.com/a/21453571/1631136\">http://stackoverflow.com/a/21453571/1631136

You could review my complete explanation here: http://stackoverflow.com/a/21453571/1631136

这篇关于片段无法与viewpager片段取代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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