Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 862
user2313832 Avatar asked Apr 24 '13 04:04

user2313832


1 Answers

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;
    }
}
like image 76
0xPixelfrost Avatar answered Oct 03 '22 20:10

0xPixelfrost