switch between fragments without 'FragmentPagerAdapter'

GyRo

I am trying to switch between fragments on my activity. I was reading this tutorial, but my case is a bit different since I don't want/can't use the 'FragmentPagerAdapter', instead, I want that a button that is pressed on activity will switch between 2 fragments.

My activity layout consist of a Button and ViewPager. in Addition I have got Fragment1 and Fragment2. How can I switch between this fragments using OnClick method?

My Layout:

<LinearLayout 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:orientation="vertical">

    <Button
     android:text="Switch it"
     android:onClick="switchFragment"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>

</LinearLayout>

and my activity:

public class MainHeaderFragment2 extends ActionBarActivity {    
    ...
    public void switchFragment(View view){
         Fragment fragment
         if(checkSomething())
           fragment = new Fragment1();
         else
           fragment = new Fragment2();
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        // now need to put the selected fragment in ViewPager somehow. 
        // How? that is my question
    }
}
kha

Based on your comments, this is what you should do:

Change your layout to this:

<Button
 android:text="Switch it"
 android:onClick="switchFragment"/>

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

To populate it first time with your first fragment:

Fragment1 fragment1 = Fragment1.create();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment1);
fragmentTransaction.commit();

To replace it,

Fragment2 fragment2 = Fragment2.create();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment2, "optionalTag");
fragmentTransaction.commit();

Fragmen1 and Fragment2 are the fragments you want to display or replace.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

FragmentPagerAdapter not restoring fragments upon Orientation Change

From Dev

Best way to switch between two fragments

From Dev

How to switch between fragments during onclick?

From Dev

Android Lifecycle management of Fragments within ViewPager and FragmentPagerAdapter

From Dev

Properly Interacting with fragments in FragmentPagerAdapter

From Dev

ViewPager and FragmentPagerAdapter with Fragments (not support library)

From Dev

How can I switch between two fragments, without recreating the fragments each time?

From Dev

Passing values between Fragments in FragmentPagerAdapter

From Dev

How to swipe between Fragments with FragmentPagerAdapter

From Dev

Switch between Fragments and Show hide the AlertDialog and maintain state

From Dev

Skipping Fragments During Swipes with a FragmentPagerAdapter

From Dev

Can't make FragmentPagerAdapter to update Fragments contents

From Dev

Using viewpager with radiogroup to switch between fragments

From Dev

FragmentPagerAdapter has empty Fragments

From Dev

Reload Activity to re-switch between fragments

From Dev

Fragments aren't replacing properly in FragmentPagerAdapter

From Dev

switch between different version of kernel without a reboot

From Dev

Communicate between two machines without a switch

From Dev

Switch fragments without replacing Android

From Dev

How to swipe between Fragments with FragmentPagerAdapter

From Dev

Sending list of fragments to FragmentPagerAdapter

From Dev

Can't make FragmentPagerAdapter to update Fragments contents

From Dev

Switch between X applications without a window manager

From Dev

Reload Activity to re-switch between fragments

From Dev

switch between nvidia and intel gpu without reboot

From Dev

Common way to switch fragments without loosing state

From Dev

How to add nextset of fragments without disturbing current fragments in FragmentPagerAdapter?

From Dev

Unknown number of fragments: FragmentPagerAdapter or FragmentStatePagerAdapter?

From Dev

How to switch between fragments without actually overlapping the previous fragments

Related Related

  1. 1

    FragmentPagerAdapter not restoring fragments upon Orientation Change

  2. 2

    Best way to switch between two fragments

  3. 3

    How to switch between fragments during onclick?

  4. 4

    Android Lifecycle management of Fragments within ViewPager and FragmentPagerAdapter

  5. 5

    Properly Interacting with fragments in FragmentPagerAdapter

  6. 6

    ViewPager and FragmentPagerAdapter with Fragments (not support library)

  7. 7

    How can I switch between two fragments, without recreating the fragments each time?

  8. 8

    Passing values between Fragments in FragmentPagerAdapter

  9. 9

    How to swipe between Fragments with FragmentPagerAdapter

  10. 10

    Switch between Fragments and Show hide the AlertDialog and maintain state

  11. 11

    Skipping Fragments During Swipes with a FragmentPagerAdapter

  12. 12

    Can't make FragmentPagerAdapter to update Fragments contents

  13. 13

    Using viewpager with radiogroup to switch between fragments

  14. 14

    FragmentPagerAdapter has empty Fragments

  15. 15

    Reload Activity to re-switch between fragments

  16. 16

    Fragments aren't replacing properly in FragmentPagerAdapter

  17. 17

    switch between different version of kernel without a reboot

  18. 18

    Communicate between two machines without a switch

  19. 19

    Switch fragments without replacing Android

  20. 20

    How to swipe between Fragments with FragmentPagerAdapter

  21. 21

    Sending list of fragments to FragmentPagerAdapter

  22. 22

    Can't make FragmentPagerAdapter to update Fragments contents

  23. 23

    Switch between X applications without a window manager

  24. 24

    Reload Activity to re-switch between fragments

  25. 25

    switch between nvidia and intel gpu without reboot

  26. 26

    Common way to switch fragments without loosing state

  27. 27

    How to add nextset of fragments without disturbing current fragments in FragmentPagerAdapter?

  28. 28

    Unknown number of fragments: FragmentPagerAdapter or FragmentStatePagerAdapter?

  29. 29

    How to switch between fragments without actually overlapping the previous fragments

HotTag

Archive