Devrath
Devrath

Reputation: 42824

placing viewpager with sliding tabs inside fragments as a nested format

What i am trying to do:

  1. I am using a drawer where i have 10 fragments
  2. Inside one of the fragment i am trying to add view pager with sliding tabs(that has view pager indicator as per jake wharton)

What is happening:

  1. When i tried to do this i get error as described here in one of my other stackoverflow question
  2. I am not able to bring the architecture described in the figure below

Question::

  1. Is placing view pager with sliding tabs not possible inside fragments ?
  2. If so are there any samples online ? (Jake wharton sample is done with placing widgets in activity not fragments)
  3. If any other way is available are there any open source library for this

Note: i am aware nested way is not advised, but i need to get this architecture


Architecture i am trying to get it:

fig

Upvotes: 3

Views: 2468

Answers (1)

Vasilov Artur
Vasilov Artur

Reputation: 1487

At first, you can try this sample from Google https://developer.android.com/samples/SlidingTabsBasic/project.html. It also contains the SlidingTabLayout class, try it, if you use something different.

This project is similar to your (has the same layout) except navigation drawer.

So, let's do the following things:

your main layout may look like this:

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/content_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:id="@+id/drawer"
        android:layout_width="@dimen/drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="@color/drawer_back"
        android:gravity="left|center_vertical"
        android:orientation="vertical">

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="@dimen/drawer_width"
            android:layout_height="0dip"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:background="@color/drawer_back"
            android:choiceMode="singleChoice"
            android:clipToPadding="false"
            android:divider="@android:color/darker_gray"
            android:dividerHeight="1dp" />

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

For fragment with sliding tabs use the following layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">

    <yourpackage.slidinglayout.SlidingTabLayout
        android:id="@+id/sliding_tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:background="@drawable/toolbar_shadow" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"
        android:layout_marginTop="@dimen/small_padding"
        android:background="@android:color/white"/>
</LinearLayout>

After that you should create a Fragments for your ViewPager and etc. but I think, you've already done it.

Fragment with sliding tabs and ViewPager - it's up to you.

On drawer item with sliding tabs clicked, simple use the FragmentTransaction:

mCurrentFragment = new SlidingTabsFragment();
mTransaction.replace(R.id.content_fragment, mCurrentFragment);
mTransaction.commit();

Upvotes: 1

Related Questions