Reputation: 106
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
Upvotes: 5
Views: 5556
Reputation: 10444
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;
}
}
Upvotes: 5
Reputation: 4142
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.
Upvotes: 2