Unable to view fragments in ViewPager

Viewed 35

I've been recently working on a project and I have tried using ViewPager for the first time. Unfortunately, something is not going as expected. I have created my ViewPagerAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter {

private List<Fragment> fragmentList = new ArrayList<>();
private List<String> stringList = new ArrayList<>();

public ViewPagerAdapter(@NonNull FragmentManager fm) {
    super(fm);
}

public ViewPagerAdapter(@NonNull FragmentManager fm, int behavior) {
    super(fm, behavior);
}


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

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

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

public void addFragment(Fragment fragment, String title) {
    fragmentList.add(fragment);
    stringList.add(title);
}

There are multiple fragments handled in the activity by the adapter.

public class PatientActivity extends AppCompatActivity {

@BindView(R.id.tab_layout)
TabLayout tabLayout;
@BindView(R.id.view_pager_pacient_view)
ViewPager viewPager;

ActivityPatientBinding binding;
ViewPagerAdapter viewPagerAdapter;

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

    binding = ActivityPatientBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    ButterKnife.bind(this);

    setupViewPager();

}

private void setupViewPager() {
    //getChildFragmentManager?
    viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

    viewPagerAdapter.addFragment(PatientDetailsFragment.newInstance(), "Pacient");
    viewPagerAdapter.addFragment(StateFragment.newInstance(), "Stare");
    viewPagerAdapter.addFragment(PathologicalBackgroundFragment.newInstance(), "Antecedente");
    viewPagerAdapter.addFragment(TriageFragment.newInstance(), "Triaj");
    viewPagerAdapter.addFragment(EkgFragment.newInstance(), "Observatii");
    viewPagerAdapter.addFragment(AnalysisFragment.newInstance(), "Analize");
    viewPagerAdapter.addFragment(RecommendationFragment.newInstance(), "Recomandari");

    viewPager.setAdapter(viewPagerAdapter);
    tabLayout.setupWithViewPager(viewPager);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));

}

}

And the xml belonging to the activity is this one

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:fitsSystemWindows="true"
    android:background="?android:windowBackground"
    tools:context=".ui.addpatientpage.PatientActivity"
    android:orientation="horizontal">


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:paddingHorizontal="@dimen/spacing_medium"
            app:tabBackground="@android:color/transparent"
            app:tabGravity="center"
            app:tabIndicator="@drawable/shape_rounded_red"
            app:tabIndicatorColor="@color/colorNavigation"
            app:tabIndicatorGravity="center"
            app:tabIndicatorHeight="@dimen/spacing_xlarge"
            app:tabMinWidth="120dp"
            app:tabRippleColor="@android:color/transparent"
            app:tabSelectedTextColor="@android:color/white"
            app:tabTextAppearance="@style/TextAppearance.AppCompat.Body2"
            app:tabTextColor="@color/grey_60">

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

    </com.google.android.material.appbar.AppBarLayout>
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager_pacient_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

sample of the Fragment:

public class PatientDetailsFragment extends Fragment {

    public static PatientDetailsFragment newInstance() {
        return new PatientDetailsFragment();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.row_pacient_details, container, false);
        ButterKnife.bind(this, root);

        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}

Should I override the method onViewCreated in each fragment to make the visible?

I have looked at the code multiple times and tried different solutions, I have even tried to implement it with ViewPager2, but nothing seems to work. It feels like there is a small fix that is needed, but after many hours of trying, I came to the conclusion that I can not find it by myself. Can you please also take a look and eventually give me a hint? Should I override the method onViewCreated in each fragment to make the visible or do you see some other issue in here? If the adapter seems to contain all of the fragments specified in the main class, where can it go wrong?

0 Answers
Related