Reputation: 1501
I have a ListView inside a fragment. When i click an item on the List i want to "start" a new fragment using replace. This is the result. I was supposed to get only the BUTTON:
This is inside onItemClick:
Fragment nextFrag= new VitaminFragment();
FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
transaction.replace(vitamin_fragment.getId(), nextFrag);
transaction.addToBackStack(null);
transaction.commit();
Inside VitaminFragment's onCreateView:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("created", "vitamin_fragment_2");
return inflater.inflate(R.layout.supplement_properties_layout, container, false);
}
and VitaminFragment XML (supplement_properties_layout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="77dp" />
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="48dip" />
<android.support.v4.view.ViewPager
android:id="@+id/supplements_properties_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/tabs" />
</RelativeLayout>
VitaminWiki's layout is created programatically:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View result = initComponents();
.....
.....
}
private RelativeLayout initComponents() {
vitamin_fragment = new RelativeLayout(getActivity());
vitamin_fragment.setId(View.generateViewId());
RelativeLayout.LayoutParams layout_57 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
vitamin_fragment.setLayoutParams(layout_57);
ListView listView = new ListView(getActivity());
......
vitamin_fragment.addView(listView);
com.astuetz.PagerSlidingTabStrip p = new PagerSlidingTabStrip(getActivity());
...........
p.setLayoutParams(params);
vitamin_fragment.addView(p);
ViewPager viewPager = new ViewPager(getActivity());
.........
vitamin_fragment.addView(viewPager);
return vitamin_fragment;
}
Finally HERE is the Activity that fragments are attached.
Upvotes: 0
Views: 5316
Reputation: 3016
The replace() method from FragmentTransaction simply removes all fragment from the specified container and then call add(). So if the specified container is empty, replace() simply adds a fragment on top of the current view.
You should post the XML file for your VitaminFragment, but I'd say add a background to your VitaminFragment and you should be fine.
And don't forget to make the main layout of your fragment clickable. Otherwise any click on something else than the button will go through the Fragment and probably click on an item on you ListView.
Your Fragment should look something like this :
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:clickable="true" >
EDIT : If you want the replace() method to work and actually remove the ListView from the activity, make sure you call it on the same container that you used to add the first fragment in the first place. Also, the first Fragment needs to have been added dynamically. When I say added dynamically, I mean using the FragmentTransaction.add() method.
Upvotes: 8