-1

After reading ViewPager's source code, I got that ViewPager is directly inherited the ViewGroup class,so it's easy to understand View can added to ViewPager.But from Google doc:

ViewPager is most often used in conjunction with Fragment, which is a convenient way to supply and manage the lifecycle of each page. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases. These are FragmentPagerAdapter and FragmentStatePagerAdapter

We can learn that Fragment can worked with ViewPager. but the following shows that Fragment is not a View.

public class Fragment implements ComponentCallbacks, OnCreateContextMenuListener

so my Question is Why fragment can be nested into ViewPager? How does Fragment work with ViewPager? Is possible to use Activity instead of Fragment?

MummyDing
  • 505
  • 1
  • 6
  • 17

1 Answers1

2

Fragment is not a View

True, but you can get the View from Fragment.getView()

Why fragment can be nested into ViewPager

Because FragmentPagerAdapter is an Adapter that you attach to a ViewPager to display Fragments

Related: Difference between FragmentPagerAdapter and FragmentStatePagerAdapter

Is possible to use Activity instead of Fragment?

This is unclear what you want to do, but generally, yes.... Unless you mean load an Activity into a ViewPager, then no. You are supposed to put a ViewPager into an Activity layout, then use the FragmentPagerAdapter to load Fragments into the ViewPager

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245