Using View or Fragment in ViewPager

I have a question whether to use View or Fragment with ViewPager.

Background: I have an Activity A that contains a ListView. Each ListView element opens Activity B. Activity B displays different content, depending on which ListView element is used in Activity A. The contents of Activity B are displayed inside the ListView.

Question: Now, instead of switching between Activity A and B to switch the contents, I have a requirement to implement horizontal viewing in order to switch the contents of everything in Activity B. One of the solutions that I found (I tried, and it works) is create many instances of Activity B ListView and use it with ViewPager + PagerAdapter. Another potential solution found in the document (I have not tried it) is to cast this ListView into a fragment, create many instances of the fragment and use it using ViewPager + FragmentPagerAdapter or FragmentStatePagerAdapter.

My question is, what are the benefits of using each approach? Should I overcome all the difficulties with casting a ListView into a fragment or just use a ListView with a ViewPager?

thanks

+6
source share
1 answer

A Fragment is a useful approach, I think when you want to associate some business user interface logic with a specific View (or group). As you know, this individual Fragment has its lifecycle callbacks, etc., as in the case of Activity .

Instead of having one Activity host a lot of ListView through one PagerAdapter , it might be cleaner to use the Fragment approach, because Fragment needs to deal with the logic behind the wheel of a single ListView .

This is very similar to the situation I just encountered. I show various vertical scroll shapes (consisting of many input fields) within the ViewPager . In my case, I went for the Fragment approach, because in my case, it is possible that the ViewPager should actually display a completely different view on certain pages. For example, the first few pages may display user input forms. But the graph will be displayed on the last page. To manage this graph requires a whole separate logic. To manage these input forms, and one graph from one Activity would become a little messy, and I would probably need to contain business logic in several delegate classes or something like that. So for me, Fragment was the obvious choice at the end. I have InputFormFragment and GraphFragment , and each of them contains only the applicable logic for the View that they provide.

Another thing to keep in mind is that in the near future you will also want to display a different type of View in the ViewPager . Or maybe you want to have another layout of the user interface, perhaps one that does not use ViewPager , but displays them all side by side (for example, the layout used on a large tablet in landscape mode). With Fragment s, everything is just much more modular, and you can make the code faster to make it faster. If, on the other hand, you have achieved your goal using one Activity that contains a simple PagerAdapter and all the logic for the ListView inside, you may find that in the future more work will be needed to support new types of View or special tablet layouts.

One thing I will say is to implement Fragment in the ViewPager itself through the FragmentPagerAdapter and FragmentStatePagerAdapter , everything can become a little awkward if you have special requirements; Fragment management can be difficult sometimes. For example, for my user interface, I needed to programmatically add and remove a ViewPager containing Fragment s. I also had to make sure that the adapter used did not destroy Fragment after it was shown, because I needed to collect data from all Fragment at the same time to a specific point. Also, I had to extend and modify the FragmentPagerAdatper to make sure that the Fragment passed their onDestroy() correctly and removed from the FragmentManager when the ViewPager was removed.

Fragment allow a very modular way to create user interfaces for different screen sizes and orientations and are great for how they encapsulate business logic and life cycles for individual user interface elements. However, if your script is really as simple as a few ListView in the ViewPager , and you know that you never need modularity, then the Fragment overhead can be excessive.

+10
source

Source: https://habr.com/ru/post/927273/


All Articles