SlidingTabLayout: replace with fragment - android

I've the following situation:
Linear Layout
ToolBar
SlidingTabLayout
ViewPager
FrameLayout
Initially, I only add a fragment to the FrameLayout, and all works. Next I remove this fragment, create new Adapter, and bind adapter to the SlidingTabLayout. All works again, but now I need to set tab visibility to false, and I do this with
slidingTabLayout.setVisibility(View.GONE);
but I also need to replace the current view with a new fragment.
Usually I do this with:
getFragmentManager().replace(parent, newFragment);
but now I can't do this because
getFragmentManager().replace(ViewPager, newFragment);
doesn't works. So how can I do this? And if is possible, can I add the ViewPager replacing in the backstack?

I find a solution. I made the following update to my layout:
Linear Layout
ToolBar
Linear Layout (id: parentScroll)
SlidingTabLayout
ViewPager
Frame Layout (id: parent)
So when I need to hide all the tabs and the ViewPager, in order to inflate a new Fragment in the Frame Layout, I do the following:
parentScroll.setVisibility(View.GONE);
MyFragment myFragment= new MyFragment ();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.parent, myFragment, "MYTAG");
transaction.commit();
In the onBackPressed of this new fragment:
MyFragment myFragment= (MyFragment) getSupportFragmentManager.findFragmentByTag("MYTAG");
if (myFragment!= null && myFragment.isVisible()) {
parentScroll.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction().remove(myFragment).commit();
}

Related

Added fragment scrolled down

I have two fragments the first fragment contains list of linear layouts and the whole fragment is in scroll view the second fragment is added and the first is hidden on choosing item from the first.
The problem is the second fragment is created scrolled down if the first fragment was scrolled down.
I tried the ways to force second fragment to be scrolled to (0,0) but failed.
the code used to add the second fragment
public void setActionOnClick(String id) {
CommentFragment frag = new CommentFragment();
Bundle bundle = new Bundle();
bundle.putString("id", id);
bundle.putString("TAG", TAG_NEWS_STORY);
((MainActivity) getActivity()).setCurrentTag(TAG_NEWS_STORY);
frag.setArguments(bundle);
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
android.R.anim.fade_out);
fragmentTransaction.add(R.id.main_content, frag, TAG_NEWS_STORY);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
on Attach of second fragment the first fragment is hidden.
I don't want to use fragmentTransaction.replace because there are calls to the api which I don't want to reload.
this is an old question but. probably your fragment place_hoder is inside an scrollview.
just check if R.id.main_content is in a scrollview or not.
you should add the ScrollView to the fragment-layout instead of instantiating the fragment inside one
Before this:
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
Add code to remove old fragment:
getSupportFragmentManager().beginTransaction().remove(yourOldFragment).commitAllowingStateLoss();

How to replace fragment from ViewPager with a new one

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

How to control ViewPager rotation which creates from the support.v4.app.ListFragment?

please help me.
I have the following problem.
From the fragment, which was created from getSupportFragmentManager() create ViewPager.
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
MyViewPager viewPager = MyViewPager.newInstance();
fragmentTransaction.add(R.id.container, viewPager);
fragmentTransaction.commit();
Everything is ok. But if i rotate the screen.
Triggered onCreateActivity().
In the fragment from which I create ViewPager.
In ViewPager
In page(page in ViewPager is a fragment).
Questions:
That is how it should be? By this logic, I must retain ViewPager
position and recreate ViewPager.
How to make it so that when rotating the screen called only
onCreateActivity() from ViewPager and page. And not in
parentFragment?
When i try create ViewPager from:
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
I get the following error ..
java.lang.IllegalArgumentException: No view found for id 0x7f0f0054 (com.jd:id/container) for fragment MyViewPager{41dcf008 #0 id=0x7f0f0054 My_FRAGMENT}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
Thanks.

Android Open Fragment over ViewPager

I'm new to android programming and I'm trying to make an app that uses tabs in a viewpager from one main fragmentactivity.
The viewpager and tabs work fine but I want to have an options menu that when an item is selected, opens a completely new fragment, but I don't seem to be able to remove the view pager. I would like to just be able to put the new fragment over the viewpager on the main screen but trying to do that with a fragmenttransaction doesn't seem to work
Any ideas?
Thank you for your time
Ensure that:
1) your Fragment (e.g.: ViewPagerFragment1), which will be selected by the viewpager, has a FrameLayout as root layout with the id "container" e.g:
<FrameLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/container"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout.... and so on
</FrameLayout>
2) Inside the ViewPagerFragment1 class, you have to replace/add the new fragment to the FrameLayout after an action has been triggered. E.g.:
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.selection:
// Create new fragment and transaction
NewFragment newFragment = new NewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment, "NewFragment");
transaction.addToBackStack(null);
transaction.commit();
break;
default:
break;
}
}
Well, you will be adding the Fragment to some FrameLayout using the ID of the FrameLayout I assume. Just make sure the FrameLayout you are adding the Fragment to is on top of the ViewPager.
For example if both the ViewPager and FrameLayout are in a RelativeLayout container then make sure the FrameLayout is declared below the ViewPager in the XML. This will stack the FragmeLayout on top of the ViewPager. When the Fragment is added to the FrameLayout it will be drawn on top.

Can't change a fragment in ViewPager

I'm trying to change a fragment inside a ViewPager:
public void change() {
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pager, mapa);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
mSectionsPagerAdapter.notifyDataSetChanged();
}
R.id.pager is my ViewPager id.
mapa is a inflated fragment.
If I call change(), the fragment I want to replace will disappear but the new fragment will not be load.
The view pager uses an Adapter to change the fragments it displays. Just like a ListView uses its adapter to get views to populate the list.
You have to extend FragmentPagerAdapter, create a new instance of it and set it to the pager with this line:
mViewPager.setAdapter(fragPagerAdapter);

Categories

Resources