I'm going to develop an android app to use Fragments with a ViewPager using the FragmentPagerAdapter. I'm trying to replace fragments onClick of a button inside a fragment but when I click on that button, the current layout goes up and next layout still comes on that position i.e.2 layouts come on a single fragment and I don't want it.
I'm using these steps to replace but not getting changed properly,
FragmentTransaction transaction = getActivity()
.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.select_program_root, newFrag);
transaction.addToBackStack(null);
transaction.commit();
Even I'm using TabHost with ViewPager inside a horizontal ScrollView. Everything is fine in my app. I'm only stuck on replacing of fragments onClick of buttons which are located inside fragments.
Thanks in advance.
Finally, I solved it by implementing few lines of code.
Create this method into your FragmentActivity:
public void setCurrentItem(int item, boolean smoothScroll) {
mViewPager.setCurrentItem(item, smoothScroll);
}
And call it onClick of a Fragment:
((MainActivity) getActivity()).setCurrentItem(3, true);
Related
I have main activity with 4 tabs in the settings tab I need to implement this flow:
The user click on a recyclerView item (this has been handled).
A new fragment is shown instead of the settings tab.
I tried to make a replace transaction on view pager view, but when the user click on another tab the newly created fragment is still shown and it's not replaced by view pager.
Edit: My code for showing the nested fragment:
clickListenr = settingsRecyclerAdapter.observeClickListener()
.subscribe(id -> {
if(id == 3){
getFragmentManager().beginTransaction()
.replace(R.id.view_pager,new WebViewFragment())
.addToBackStack(null)
.commit();
}
});
I suggest you to use only Framelayout in your main fragment. Then Creat two fragment named like MainFragmentTop and , SettingFragment(nested fragment). Then in your main fragment onviewcreated method relapce your MainFragmentTop fragment into framelayout. Then when you want to open setting fragment replace or add same into framelayout. So, it's become easier to use.
This is working fine.
I have a viewPager in my app which has several tabs. In some of the tabs, on clicking an item a new fragment is shown. I want this fragment to cover the tabs. Doing this is possible but the approaches don't look good to me.
1.) one way is that I add the newly created fragment to the activity using getSupportFragmentManager(). This solves the problem but doesn't look like a good idea as it will create problems when using back button etc.
2.) Other way is to hide the tabs manually using Visibilty.GONE but problem with this approach is that this hiding of the tabs is visible, I mean the animation could be seen and looks bad.
Is there a better approach to do this problem?
This is my code. "sub_fragment_container" is present in the activity xml, so I get an error java.lang.IllegalArgumentException: No view found for id 0x7f0e00ff (com.my.app:id/sub_fragment_container) for fragment DetailFragment{
FragmentTransaction fragmentTransaction = getChildFragmentManager()
.beginTransaction();
Fragment profileFragment = new DetailFragment();
profileFragment.setArguments(bundle);
fragmentTransaction
.add(R.id.sub_fragment_container, profileFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Use android:animateLayoutChanges="true"
Or, to fine tune it further, here's a tested method that I've personally used in my app Newslet to solve the exact same problem.
Wrap your Toolbar and TabLayout within an AppBarLayout. This should be easy if you're aware of the Design Support Library. If not, this should get you started.
Add this piece of code in your onCreate():
LayoutTransition layoutTransition = new LayoutTransition();
layoutTransition.setDuration(200);
layoutTransition.setStartDelay(LayoutTransition.CHANGE_APPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.APPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
layoutTransition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);
appBarLayout.setLayoutTransition(layoutTransition);
After this, when you remove the Tabs or add it back, the layout change will animate smoothly.
Hope this helped you!
I want to be able to replace the current fragment which is loaded via the actionbar tabs using a ViewPager with another fragment that is not connected to any tab like this
getSupportFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.replace(id,frag , "VideoEdit")
.commit();
however when I do this it loads the fragment all right but the tabs don't work anymore and everytime I click on a tab it just stays on this fragment. I managed to find this gist
https://gist.github.com/andreynovikov/4619215 but I'm not sure it's the same thing I'm looking for and I don't want to try it out for fear of messing up my existing code with it.
You should avoid transitions like that.
I'll recomend to use FragmentWrapper.
One more fragment that you placed inside Tab it contains only FrameLayout as a container for other fragments. In this fragment, you could use childFragmentManager to add current Fragment and replace it with VideoEdit.
I am working on an application that contains three tabs and each tab has a fragment associated with it. On certain conditions i have to display child tab within second parent tab. But if that condition is not true then child tabs should not load and the parent tab should load one of the fragment i used in child tabs. For example, i have three fragments FragmentA, FragmentB, and FragmentC. Now if the condition is true then i will display tabs and load FragmentB and FragmentC inside FragmentA using Fragment tab host. But is condition is false i want to display FragmentB in FragmentA.
I tried using Fragment.replace() but it fails.
I think this is not a completely new issue.
You can find an answer here:
Separate Back Stack for each tab in Android using Fragments
Regards
Francesco.
In my intention you should override the onHiddenchange() for fragment A and check the condition over here.
I was able to solve the issue using a common fragment. In FragmentA's onCreateView if condition is true create Tabs using FragmentTabHost and add FragmentB and FragmentC to the tab. If condition is false then inflate a view using
View fragmentView = Inflator.Inflate(R.id.fagment_layout, FragmentB);
FragmentB fragmentB = new FragmentB();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(fragmentView, fragmentB);
transaction.commit();
It worked for me.
I'm using TabHost and I have a problem. What I want is first tab should be a FragmentActivity and it can switch to another Fragments. This application helps me but I have also questions. In this example, you can go to infinite another Fragments but it's happening by the help of launchNewFragment method and this method is called from xml. But I need to call this method programmatically because when button is clicked, I also want to move some values (such as selected values in dialog) to another Fragment. Or is there another way to solve this problem? I'm waiting your suggestions.
You might want to use Action Bar with Tabs instead of TabHost. You can get information about how to use it here: http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
No matter on the chosen solution, replacing the displayed fragment in an activity is done like this:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, new MyFragment());
transaction.commit();
You just have to add this to the button listener and replace MyFragment with your fragment.