1

I found so many similar questions and so many different answers, but none of them helped me. I will try to post a clear question to get a clear answer if it's possible.

So I have an Activity that has a ViewPager with FragmentStatePagerAdapter containing (for now) only one fragment (HomeScreenFragment).

Inside HomeScreenFragment I have a RecyclerView with a list of several kind of different icons which should open a different fragment for each item click.

HomeScreenFragment has a FrameLayout as a root layout with id name "container". This is the method I'm calling to replace HomeScreenFragment with MainTypesFragment in this case:

 private void openAllTypesFragment() {
    MainTypesFragment fragment = new MainTypesFragment();
    FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.container, fragment);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.addToBackStack(HomeScreenFragment.class.getName());
    transaction.commit();
    eventBus.post(new Event(null, Event.EVENT_FRAGMENT));
}

Since FragmentStatePagerAdapter is initialized in MainActivity, I'm sending an event which will MainActivity catch and call adapter.notifyDataSetChanged();

Doing it this way, nothing happens.. I tried with getChildFragmentManager() instead of getActivity().getSupportFragmentManager() and in that case, previous fragment (HomeScreenFragment) is visible underneath new one (MainTypesFragment)..

Any ideas what am I doing wrong here?

user8789149
  • 121
  • 4
  • 12
  • you want replace previous fragment or you want just open new fragment above old ? – Yuri Misyac Oct 17 '17 at 10:08
  • I want to have 2 fragments inside viewpager (HomeScreenFragment) which can be replaced with several different possible fragments, depending on type of RecyclerView item click and one fragment which always be there as a second fragment inside viewpager – user8789149 Oct 17 '17 at 10:13
  • If I correct understand you need separated fragment stacks for each page ? Like you click at page1 list item 1 and will appear new fragment above list on page1 ? – Yuri Misyac Oct 17 '17 at 10:21
  • I'll try to describe it little more better: Let's say first fragment inside viewpager is MovieFragment, second fragment is ActorsFragment. If you click on a movie item inside MovieFragment, MovieFragment should be replaced with MovieDetails fragment, but ActorsFragment stays as a second fragment inside viewpager.. It shouldn't be above.. Whole fragment should be replaced with a new one – user8789149 Oct 17 '17 at 10:27

3 Answers3

0

The container you are using is wrong.Maintain one container in the activity xml and use that container to load all the fragments.

transaction.replace(R.id.container, fragment);

The container here is to be the activity container in which viewpager and other views should be present.

Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
  • But in that case I would replace whole viewpager with new fragment, right? I want to have 2 fragments inside viewpager and only first one can be replaced based on type of item clicked inside of it... For example, I have a HomeScreenFragment and SecondFragment inside viewpager when the app is running. After I click on one of the items inside HomeScreenFragment, it should be replaced with MainTypesFragment, but SecondFragment should be untouched.. Is that possible to achieve at all? – user8789149 Oct 17 '17 at 10:18
  • then you have to keep a frameLayout for that space inside xml that matches HomeScreenFragment view and then you have replace..If you add codes I can say clearly. – Sharath kumar Oct 17 '17 at 10:33
  • Actually better solution will be how you suggested. So I have a ViewPager inside FrameLayout with id fragment_container. When I call replace method, I put that id as a first parameter but it doesn't replace it well.. I can see the new Fragment, but the previous, main one is still visible under new one (The one from ViewPager)... – user8789149 Oct 17 '17 at 11:20
  • Also by default the fragment will have transparent background..You need to override that by adding a background.I think your problem is solved :) – Sharath kumar Oct 17 '17 at 11:26
  • It is! Thank you sir! BUT, the problem for now is, my new fragment should be transparent! :) any other way? – user8789149 Oct 17 '17 at 11:53
  • transparent makes bakground visible..so add another layout inside fragment as child and make it transparent.make parent layout of fragment background white. – Sharath kumar Oct 17 '17 at 12:09
  • My new fragment contains LinearLayout with RecyclerView inside it. So I should wrap RecyclerView with new LinearLayout with transparent background and root should have white background for example? ... The problem might be, my app theme has a transparent background and it shows a wallpaper... But if I REPLACE frame layout with new Fragment, should the whole frame layout that holds viewpager be gone (replaced)? – user8789149 Oct 17 '17 at 12:44
0

You can try my code :

    private void addFilmDetailFragment(Movie movie){
        android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
        android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        FilmDetailFragment fragment = new FilmDetailFragment();

        Bundle bundle = new Bundle();
        bundle.putParcelable("movie", movie);  // Key, value
        fragment.setArguments(bundle);

        fragmentTransaction.replace(R.id.mainContentStore, fragment);
//        fragmentTransaction.replace(R.id.mainContent, fragment, Config.TAG_FILM_DETAIL);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

In a fragment of viewpager :

<?xml version="1.0" encoding="utf-8"?> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainContentStore"
    android:layout_width="match_parent"
    android:layout_gravity="center"

    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerFilm"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</FrameLayout>

It worked for me, i hope it will help your problem!

Thientvse
  • 1,753
  • 1
  • 14
  • 23
  • Can you please provide some info about the structure and ids you are using? Is "mainContentStore" id of your current root layout or the frame layout viewpager is inside of? – user8789149 Oct 17 '17 at 10:21
  • But the fragment you are replacing is not inside ViewPager, right? You are just replacing a regular fragment with a new one? – user8789149 Oct 17 '17 at 11:24
0

You need container inside activity for ViewPager and inside fragments(pages), containers for opening new fragments inside pages.

And if you want to open new fragment you should user something like this in page fragment

getChildFragmentManager().beginTransaction()
      .replace(R.id.page_fragment_container, fragment)
      .addToBackStack(name)
      .commit()

where R.id.page_fragment_container container inside page fragment.

Yuri Misyac
  • 4,364
  • 2
  • 16
  • 32
  • It works, but the HomeScreenFragment (the one from ViewPager) stays visible under new one... – user8789149 Oct 17 '17 at 11:22
  • add background for New Fragment and in `android:clickable="true"` for root layout. If you need the opportunity to go back to the previous fragment. – Yuri Misyac Oct 17 '17 at 11:28