I have a question about Android viewpager.

Asked 1 years ago, Updated 1 years ago, 122 views

If each item in the view phaser has the same layout, can we define only one fragment? What should I do?

android-viewpager android fragment

2022-09-21 14:49

1 Answers

Conversely, even if each page in ViewPager has a different UI, you can create and implement only one Fragment class.

However, that would be an inefficient method, such as requiring branching processing of behavior by page in the class. The code line will be very long.

That's why you typically want to create a Fragment class per page. If each page has a different role and UI, it would be efficient to write each Fragment class.

In conclusion, we can fully implement what you asked.

However, since you are currently trying to implement it before it is possible/impossible, I think it would be better to think about whether to write a Fragment class for each page or only one Fragment class.

Specifically, when there is a class called TestFragment, you can implement the getItem() of PagerAdapter as follows. If you need a separate bundle data for each page, you can use setArguments().

public Fragment getItem(intposition){
    return new TestFragment();
}


2022-09-21 14:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.