Question

The layout of the app I am working on is the classical two-pane master-detail layout. In the leftpane the menu, in the rightpane the fragments that are shown as a result of pressing a menu item. One of the menu items initiates a ViewPager swiping structure. This ViewPager structure consists of a ScreenSlideActivity and a ScreenSlidePageFragment classes. The first class, as is standard, holds a pointer to the ViewPager object, the adapter (ScreenSlidePagerAdapter) with the essential methods like getItem and getFragment.

Everything works fine, except that the ViewPager occupies the entire screen, rather than in the right panel.

Question: how can I get the ViewPager to confine itself to the right panel, rather than occupying the entire screen?

Here is the layout of the two_pane, generated automatically by Eclipse when generating a master-detail template project:

<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:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:baselineAligned="false"
    android:divider="?android:attr/dividerHorizontal"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".ItemListActivity" >

    <!--
    This layout is a two-pane layout for the Items
    master/detail flow. See res/values-large/refs.xml and
    res/values-sw600dp/refs.xml for an example of layout aliases
    that replace the single-pane version of the layout with
    this two-pane version.

    For more on layout aliases, see:
    http://developer.android.com/training/multiscreen/screensizes.html#TaskUseAliasFilters
    -->

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/item_list"
        android:name="com.company.ItemListFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@android:layout/list_content" />

        <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="10dip"
        android:layout_marginBottom="20dip"
        android:src="@drawable/icon" />

    </LinearLayout>
    <FrameLayout
        android:id="@+id/item_detail_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3" />

</LinearLayout>

The menu item that kicks off the ViewPager does this:

Intent intent = new Intent(this, ScreenSlideNewActivity.class);
startActivity(intent);

I found a hint to a solution from an old post here, but that's based on the deprecated ActivityGroup (solution at the bottom).

Was it helpful?

Solution

ActivityGroup or any other way to use activities inside other activities are deprecated.

You need to encapsulate your ViewPager structure inside a Fragment and not inside an Activity.

From there instead of starting your Activity with an Intent you will just pop your Fragment in place of your R.id.item_detail_container with a simple

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
ScreenSlideNewFragment fragment = new ScreenSlideNewFragment();
transaction.replace(R.id.item_detail_container, fragment);
transaction.commit();

Converting Activity to a Fragment is straight forward as the lifecycles are very similar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top