meysam khajvand
meysam khajvand

Reputation: 3

Tab layout in a fragment (fragments will not be shown)

I'm trying to make a tab layout in fragment with some fragments in it. The tabs will be clicked, but they do not show fragments, there's just nothing there. Here is my code:

view pager adapter:

public class PrViewPAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> frgList = new ArrayList<>();
private final List<String> titleList = new ArrayList<>();
public PrViewPAdapter(@NonNull FragmentManager fm) {
    super(fm);
}
public void addfrg(Fragment frg,String title){
    frgList.add(frg);
    titleList.add(title);
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return titleList.get(position);
}

@NonNull
@Override
public Fragment getItem(int position) {
    return frgList.get(position);
}

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

and this is main fragment

public class profile extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootview =  inflater.inflate(R.layout.activity_profile,container,false);
   final TabLayout tabLayout = (TabLayout) rootview.findViewById(R.id.tabLayout);
   final ViewPager viewPager = (ViewPager) rootview.findViewById(R.id.view_pager);
    PrViewPAdapter adapt = new PrViewPAdapter(getChildFragmentManager());
    adapt.addfrg(new neveshteFragment(),"نوشته ها");
    adapt.addfrg(new rezome(),"رزومه");
    adapt.addfrg(new nemonekarFragment(),"نمونه کار");
    adapt.addfrg(new licenseFragment(),"گواهینامه ها");
    viewPager.setAdapter(adapt);
    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });

     return rootview;
}
}

and this is the xml file

    <com.google.android.material.tabs.TabLayout
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/pureblack"
        android:layoutDirection="rtl">
    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabLayout">

    </androidx.viewpager.widget.ViewPager>

Upvotes: 0

Views: 1654

Answers (1)

Vinh Nguyen
Vinh Nguyen

Reputation: 652

The problem is your layout xml not correct because it looks like ConstraintLayout wrapping TabLayout & ViewPager, but 2 views set their constraints not correctly. You can fix by setting them by this way:

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layoutDirection="rtl"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/linearLayout"
        app:tabGravity="fill"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/black"
        >
    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tabLayout"
        >

    </androidx.viewpager.widget.ViewPager>

Upvotes: 1

Related Questions