0

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.

none
  • 1,699
  • 2
  • 13
  • 18
  • Which fragment do you want to change exactly? The one with the list or with viewpager or maybe a fragment inside a viewpager? – Michał Z. May 15 '13 at 09:33
  • Fragment inside ViewPager. Imagine situation: left side - list of countries, right side - blank now. You clicked on country name in the list. Now right side is ViewPager with two tabs. First fragment(first tab) has TextView, which tell us the capital of this country. You can click on the this textview, capital name, and this fragment, inside ViewPager, have to be replaced by another fragment with information about capital. – none May 15 '13 at 09:45
  • So you are looking for a way to replace fragments inside ViewPager. This may be helpful: http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager – Michał Z. May 15 '13 at 09:57
  • I've read it....Look at answer http://stackoverflow.com/a/13925130/1375027. He use replace() function too... But which ID I have to use in my case??? – none May 15 '13 at 10:01
  • Use root id of your first fragment(the one which you want to replace) layout. So for example if it's just a full screen `LinearLayout` with `TextView` inside it then use an id of this `LinearLayout`. – Michał Z. May 15 '13 at 10:13
  • I did it, but it was awful: layouts two fragments overlap one another – none May 15 '13 at 10:23
  • It's because you should create a fragment container that should be on the first page in your ViewPager. Just a fragment with an empty layout (I mean with LinearLayout but nothing else). And then you put your first fragment (the one with capital) in to this container using transaction. And when you want to replace it, you are using this container too. Sorry for messy answer but I'm at work and I don't have much time. – Michał Z. May 15 '13 at 10:29
  • Do you know of any examples? I think I understand the basic idea, but how to implement it .... – none May 15 '13 at 10:52
  • Just create an empty fragment with layout consisting of only Linearlayout. And put it in to ViewPager instead of your first fragment. Then Use transaction to place there first fragment. And when you want to replace it by second fragment also use transaction :) – Michał Z. May 15 '13 at 11:04

0 Answers0