Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ViewPager, on sliding to 3rd tab, child fragment in tab1 disappears

I have 3 tabs in a FragmentStatePagerAdapter. I have a fragment for tab 1 which adds views to its 'linearlayout' container inside a relative layout dynamically. i add dynamically some views in tab 1 fragment. I also added a complete fragment in that linearLayout container with other views up and down. this works fine.. every thing fine.

but when i go to 3rd page and come back. all views are there. but that complete fragment is vanished~~ EDIT
It vanishes from tab1 and appears below the views in tab2 where another childfragment is added..

First of all i have a listFragment in tab1 and tab2. based on listItem click i load a fragment in each tab, that both fragments in tab1 and 2 inflates the same below xml and adds dynamic views, but in some case i add a complete fragment instead of a view in the linear layout given in xml. Problem arose when i have to add two fragments in both new added fragments in tab 1 and tab2, after there were only lists, and slided to tab3. on returning back. fragment in 'tab1 main fragment' vanished and appeared below the fragment in 'tab2 main fragment'

this is the viewpager, in which i change different fragments by method setItem(fragment, position):

public static class ScreenSlidePagerAdapter extends
        FragmentStatePagerAdapter {

    public static ArrayList<Fragment> tabs_fragments = new ArrayList<Fragment>(
            3);

    public ScreenSlidePagerAdapter(FragmentManager fm,
            ArrayList<Fragment> tabs) {
        super(fm);
        ScreenSlidePagerAdapter.tabs_fragments = tabs;
    }

    @Override
    public Fragment getItem(int arg0) {
        return tabs_fragments.get(arg0);
    }

    @Override
    public int getCount() {
        return tabs_fragments.size();
    }

    @Override
    public int getItemPosition(Object object) {
        if (tabs_fragments.contains(object)) {
            return POSITION_UNCHANGED;
        }
        return POSITION_NONE;
    }

    public static void setItem(Fragment fr, int position) {
        if (position <= 2 && position >= 0) {
            tabs_fragments.remove(position);
            tabs_fragments.add(position, fr);
        } else
            Log.d("adding tab", "wrong tab position to add fragment at");
    }
}

this is how i add my fragment to linearlayout where 'll' is the linear layout inside the relative layout, that is the actual contentview for the fragment on tab1

ll.addView(getTitle("Set Message Receive Settings"));
        // View smsRecLayout = inflater.inflate(R.layout.fr_ev_sms_receive,
        // container, false);
        Bundle b = new Bundle();
        b.putInt("id", eventId);
        b.putInt("event", eventTypeId);
        Fragment fr = new FrEv_SMSReceive();
        fr.setArguments(b);
        getFragmentManager().beginTransaction()
                .add(ll.getId(), fr, "fr_sms_receive").commit();

this is the layout of fragment where another fragment is added in linear layout 'fr_event_container'. FragmentStatePagerAdapter

<RelativeLayout 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"
tools:context=".EventDetails" >

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:gravity="center_horizontal"
    android:padding="10dp"
    android:textIsSelectable="false" />

<ScrollView
    android:id="@+id/fr_event_container_scroll"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/sep1"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/title" >

    <LinearLayout
        android:id="@+id/fr_event_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>

<View
    android:id="@+id/sep1"
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_above="@+id/addOther"
    android:layout_centerHorizontal="true"
    android:layout_margin="10dp"
    android:background="#000"
    android:padding="10dp"
    android:visibility="gone" />

<Button
    android:id="@+id/next"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/addOther"
    android:layout_alignBottom="@+id/addOther"
    android:layout_alignParentRight="true"
    android:layout_marginRight="54dp"
    android:text="Next"
    android:width="90dp" />

like image 939
ahmadalibaloch Avatar asked Mar 27 '13 09:03

ahmadalibaloch


1 Answers

The ViewPager has a maximum offscreen page limit. You can set it higher with setOffscreenPageLimit(int)

like image 60
Stefan de Bruijn Avatar answered Nov 15 '22 04:11

Stefan de Bruijn