My ViewPager does not show any fragments even though it load the fragment for my "Review section". I used the same code with my "rating section" and it work, even tried redo the ViewPager and adapter but still no progress. By the way i'm doing this within a fragment.
I put a log in the adapter to show whether the fragment is working and also change my Viewpager's background color to check if it's visible or not. Even though the ViewPager is visible but the fragment still does not show.
UPDATE: I found out that the problem caused by the activity i use to replace with the fragments.
Here the code snippet (in my activity which hold both "Rating Section" and "review Section".
SubSectionFragment_Rating subSectionFragment_rating = new SubSectionFragment_Rating();
manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.ratingSection,subSectionFragment_rating, subSectionFragment_rating.getTag()).commit();
SubSectionFragment_ReviewAndInstructions subSectionFragment = new SubSectionFragment_ReviewAndInstructions();
manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.subTab,subSectionFragment, subSectionFragment.getTag()).commit();
Should display this fragment in ViewPager
Review.xml
<FrameLayout 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="com.hybridelements.openchef.fragment_activities.fragment_subs.SubSectionFragment_ReviewAndInstructions">
<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tab_layout"
android:background="@android:color/holo_green_dark"/>
</RelativeLayout>
Review.java (part of the code)
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
final SubPagerAdapter_ReviewAndInstructions adapter = new SubPagerAdapter_ReviewAndInstructions(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
SubPagerAdapter_ReviewAndInstructions.java
public SubPagerAdapter_ReviewAndInstructions(FragmentManager fm, int NumOfTabs){
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
ReviewsFragment subTab1 = new ReviewsFragment();
Log.d("ReviewAndInstruction","ReviewsFragment loaded");
return subTab1;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}