I have very simple List/Detail app in dual pane mode: left side - ListFragment, right side - ViewPager. Here is 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:baselineAligned="false" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<fragment
android:id="@+id/site_list"
android:name="com.example.fragmentviewpager.ListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/viewpager"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" >
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Everything works fine: when you click on a list item, the detailed information is shown in ViewPager. ViewPager consist from two simple Fragments in tabs.ViewPager uses a FragmentStatePagerAdapter to create fragments.
Fragments layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Test"
android:textAppearance="?android:attr/textAppearanceLarge"
android:clickable="true" >
</TextView>
<TextView
android:id="@+id/tvPage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
</RelativeLayout>
Now, I want to show new fragment instead of first fragment when I click on TextView with id=title
in first fragment. I have listener on for this TextView:
title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Fragment3 fragment3 = new Fragment3();
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.SOME_ID, fragment3).commit();
}
});
But I have no idea, what i have to write instead SOME_ID
... And in general, can fragment replace itself? How replace fragment???
If you need additional code - just tell me.