Do not put a Fragment
inside a Fragment
. However, you can place the ViewPager
inside the fragment and get the result you are looking for.
I ran into the same problem a while ago for one of my applications (only I wanted the ViewPager
contain some ListView
s). This was the final result (see Rows:. Red contains Fragment
, blue ViewPagerIndicator
, green ViewPager
):
I accomplished this with the following steps. Fist, for the detail fragment, I created the following XML file:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.viewpagerindicator.TabPageIndicator android:id="@+id/viewpagerIndicator" android:layout_height="wrap_content" android:layout_width="match_parent" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
Then I set up a custom PagerAdapter to populate the ViewPager and TabPageIndicator:
public class MainPagerAdapter extends PagerAdapter implements TitleProvider {
Next, in my snippet, I installed the following:
public class MainFragment extends Fragment { private MainPagerAdapter pagerAdapter; private TabPageIndicator mIndicator; private ViewPager viewPager; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); View detailsFrame = context.findViewById(R.id.details_fragment);
source share