0

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.....

--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.

4

2 回答 2

0

我开发了一个小型示例应用程序,可以在不覆盖本机类的情况下实现这一目标。

重点是使用片段作为容器。

在您的 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 布局应如下所示:

<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>

你可以在这里查看我的完整解释:https ://stackoverflow.com/a/21453571/1631136

于 2014-01-30T10:21:34.527 回答
0

根据您使用 DemoButton 片段的方式,我有两个建议:

1)也许你的问题是嵌套片段。您从活动中获取 FragmentManager,但如果 Demobutton 已经是片段的一部分,请改用外部片段的 getChildFragmentManager()。

2)根据我在使用带有片段的 ViewPager 时的经验,ViewPager 的 PagerAdapter 应该执行所有片段事务。您可以扩展和覆盖支持库中的 FragmentPagerAdapter 类,以便在需要时在 ViewPager 中获取正确的片段。

于 2013-05-29T07:20:19.303 回答