Home > Enterprise >  ViewPager2 doesn't show Fragments
ViewPager2 doesn't show Fragments

Time:05-24

I am trying to implement ViewPager2 in my application and this is not the first time I am doing it but now it is not working.

I've been trying to find a solution for a couple of hours and trying different options but still - the fragments don't show up. ViewPager2 is displaying correctly - I checked this by changing its background color.

I'm using the OneUI 2.4.0 library - but I've tested this library before and everything worked, and now it won't.

ViewPager2 Adapter

public class MainViewPagerAdapter extends FragmentStateAdapter {

    private ArrayList<Fragment> arrayList = new ArrayList<>();


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

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        switch (position) {
            case 0:
                return new MainFragment();
            case 1:
                return new NewsFragment();
            case 2:
                return new ProfileFragment();

        }
        return null;
    }

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

MainFragment.java (other fragments code looks the same)

public class MainFragment extends Fragment {
    View mRootView;

    @Nullable
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_main, container, true);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.d("MainFragment", "Initialized"); // I don't see that in logcat
    }
}

fragment_main.xml (Other fragments have similar layout)

<?xml version="1.0" encoding="utf-8"?>
<de.dlyt.yanndroid.oneui.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.textview.MaterialTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/main_page"
        android:textColor="@color/sesl_primary_text"
        android:textSize="24sp"
        android:textStyle="bold" />
</de.dlyt.yanndroid.oneui.widget.NestedScrollView>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ToolbarLayout mToolbarLayout;
    private ViewPager2 mViewPager;
    private TabLayout mTabLayout;

    private MainViewPagerAdapter viewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initialize();
        setup();
    }

    private void initialize() {
        mToolbarLayout = (ToolbarLayout) findViewById(R.id.mToolbarLayout);
        mViewPager = (ViewPager2) findViewById(R.id.mMainPager);
        mTabLayout = (TabLayout) findViewById(R.id.mMainTabLayout);
    }

    private void setup() {
        viewPagerAdapter = new MainViewPagerAdapter(getSupportFragmentManager(), getLifecycle());

        mViewPager.setAdapter(viewPagerAdapter);
   }

And the problem is just that i don't see my fragments in ViewPager2.

CodePudding user response:

try to

return mRootView

instead of

return super.onCreateView(inflater, container, savedInstanceState);

CodePudding user response:

The place where you went wrong was when you did not add the layout to the root. The problem is this:

return super.onCreateView(inflater, container, savedInstanceState);

but, you r not using your view right? You are using the default view which is just empty. So, to solve it, you need to pass it your view instead of that. So, return your view like this:

return mRootView;
  • Related