OberDulli94
OberDulli94

Reputation: 187

ViewPager2 swipe tab layout doesn't display the fragment names

I'm trying to implement a tablayout in my java based android app with the new ViewPager2 widget. My code is working perfectly but it doesn't display the tab text. The logs tell me that my function actually sets the correct name for every tab.

here is my function inside my activity where i want the tab layout to display:

private void setUpViewPagerNew(){

            String[] fragment_names = {"Gallery", "Photo"};

            mViewPager2 = (ViewPager2) findViewById(R.id.container2);
            TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout_viewpager2);
            SectionsPagerAdapterNew sectionsPagerAdapterNew = new SectionsPagerAdapterNew(this);
            sectionsPagerAdapterNew.addFragment(new GalleryFragment());
            sectionsPagerAdapterNew.addFragment(new PhotoFragment());

            mViewPager2.setAdapter(sectionsPagerAdapterNew);


            TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(
                    tabLayout, mViewPager2, (tab, position) -> {
                        tab.setText(fragment_names[position]);

                Log.d(TAG, "setUpViewPagerNew: TAB: " + tab);
                Log.d(TAG, "setUpViewPagerNew: POSITION: " + position);
                Log.d(TAG, "setUpViewPagerNew: GET TEXT: " + tab.getText());
                
            });
            tabLayoutMediator.attach();
        } 

This is my FragmentStateAdapter helper class:

public class SectionsPagerAdapterNew extends FragmentStateAdapter {

    private static final String TAG = "SectionsPagerAdapter";

    private final List<Fragment> mFragmentList = new ArrayList<>();

    public SectionsPagerAdapterNew(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    public SectionsPagerAdapterNew(@NonNull Fragment fragment) {
        super(fragment);
    }

    public SectionsPagerAdapterNew(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }


    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getItemCount() {
        return mFragmentList.size();
    }


    public void addFragment(Fragment fragment){
        mFragmentList.add(fragment);
    }
}

This is my ViewPager layout file:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.tabs.TabLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tab_layout_viewpager2"
            android:background="@color/red"
            app:tabSelectedTextColor="@color/light_blue">

        </com.google.android.material.tabs.TabLayout>

       <androidx.viewpager2.widget.ViewPager2
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:id="@+id/container2">
       </androidx.viewpager2.widget.ViewPager2>




    </RelativeLayout>

</merge>

And last but not least this is the log output:

setUpViewPagerNew: TAB: com.google.android.material.tabs.TabLayout$Tab@37a259f
setUpViewPagerNew: POSITION: 0
setUpViewPagerNew: GET TEXT: Gallery
setUpViewPagerNew: TAB: com.google.android.material.tabs.TabLayout$Tab@a72a6ec
setUpViewPagerNew: POSITION: 1
setUpViewPagerNew: GET TEXT: Photo

Thanks for your help!

Upvotes: 2

Views: 735

Answers (1)

OberDulli94
OberDulli94

Reputation: 187

I finally found my failure. Be carefull to set the right tab layout id for the TabLayoutMediator.

Upvotes: 1

Related Questions