0

i am using the new android viewpager2, but when i load different fragments, the viewpager2 shows only the same fragments..

Is this an error? in the older viewpager everything worked.

As you can see here in the images, the android app load the same fragments. Fragment page1

Fragment page2

In the viewPagerAdapter I load or create for each position a different fragment Code ViewPagerAdaptor

public class ViewPagerAdapter extends FragmentStateAdapter {

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

    @NonNull
    @Override
    public Fragment createFragment(int position) {

        switch (position) {
            case 0:
                return  new page1();
            case 1:
                return  new page2();
            default:
                return  new pageDefault();
        }
    }
    @Override
    public int getItemCount() {return 3; }


}

The MainActivity.class

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = findViewById(R.id.tabs);
        viewPager2 = findViewById(R.id.view_pager);
    
        ViewPagerAdapter adapter = new ViewPagerAdapter(this);
        viewPager2.setAdapter(adapter);
    
        new TabLayoutMediator(tabLayout, viewPager2,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        tab.setText("Tab " + (position + 1));
                    }
                }).attach();
}

here come the fragments fragment_page1

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_page1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".page1">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="This is Page 0" />

</FrameLayout>

and fragment_page2

<?xml version="1.0" encoding="utf-8"?>
<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=".page2">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="This is Page 02"
        android:background="@color/red_100"/>

</FrameLayout>

and the java code of page1.java

package com.example.demo_viewpager2;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class page1 extends Fragment {

    public page1() {
        // Required empty public constructor
    }

    public static page1 newInstance() {
        page1 fragment = new page1();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.fragment_page1, container, false);
    }
}
Raimund Popp
  • 25
  • 1
  • 9

2 Answers2

3

After trying the code provided and add the missing files, i am unable to replicate your issue and is working fine on my testing.

ViewPagerAdapter.java file

package com.example.adapter;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import com.example.fragment.page1;
import com.example.fragment.page2;
import com.example.fragment.pageDefault;

public class ViewPagerAdapter extends FragmentStateAdapter {

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

    @NonNull
    @Override
    public Fragment createFragment(int position) {

        switch (position) {
            case 0:
                return new page1();
            case 1:
                return new page2();
            default:
                return new pageDefault();
        }
    }

    @Override
    public int getItemCount() {
        return 3;
    }
}

MainActivity.java file

package com.example.view;

import android.app.Activity;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.widget.ViewPager2;
import com.example.R;
import com.example.adapter.ViewPagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabLayout tabLayout = findViewById(R.id.tabs);
        ViewPager2 viewPager2 = findViewById(R.id.view_pager);

        ViewPagerAdapter adapter = new ViewPagerAdapter(this);
        viewPager2.setAdapter(adapter);

        new TabLayoutMediator(tabLayout, viewPager2,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        tab.setText("Tab " + (position + 1));
                    }
                }).attach();
    }
}

page1.java file

public class page1 extends Fragment {

    public page1() {
        // Required empty public constructor
    }

    public static page1 newInstance() {
        page1 fragment = new page1();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.fragment_page1, container, false);
    }
}

page2.java file

    public class page2 extends Fragment {
    
        public page2() {
            // Required empty public constructor
        }
    
        public static page2 newInstance() {
            page2 fragment = new page2();
            return fragment;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_page2, container, false);
        }
    }

pageDefault.java file

public class pageDefault extends Fragment {

    public pageDefault() {
        // Required empty public constructor
    }

    public static pageDefault newInstance() {
        pageDefault fragment = new pageDefault();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_page_default, container, false);
    }
}

main_activity.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.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

fragment_page1.xml file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_page1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:text="This is Page 0"
        android:textSize="20sp"
        android:textStyle="bold" />

</FrameLayout>

fragment_page2.xml file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="This is Page 02"
        android:background="@android:color/holo_red_light"/>

</FrameLayout>

fragment_page_default.xml file

<?xml version="1.0" encoding="utf-8"?>
<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">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="This is Page Default"/>

</FrameLayout>

Output:

enter image description here

Ric17101
  • 1,063
  • 10
  • 24
  • 1
    Hello Ric17101, how can i thank you? it works fine. i stepped all line by line through and found, that i had an typo error in the inflate of page2.. The reason was, that in the page2.java file, there was in the onCreate methode the inflate on R.layout.fragment_page1 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_page1, container, false); } Thank you very much – Raimund Popp Jan 11 '21 at 15:40
  • 1
    feel free to accept my answer or green check will do, ;) – Ric17101 Jan 12 '21 at 02:15
0

thank you, this workes really fine. there was an typo error in the inflate of page2 to the page1

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_page1, container, false); }

Raimund Popp
  • 25
  • 1
  • 9