Atul Atul - 4 months ago 23x
Android Question

Android: Understanding TabLayout, ViewPager, Fragment and Adapter

I am new to Android development. Also new to Java (although have ample work experience in C++/Windows)

After reading some documents and going through some links and SO answers, my understanding is:

  1. Fragments has contents (like lists, buttons, text, images etc).

  2. View pager adapter has fragments added.

  3. View pager adapter is set to view pager.

  4. TabLayout set up with ViewPager.

Thus each tab would show fragments added one by one.

Is my understanding correct?

But then question is:

Are we suppose to use these components in a particular way like: provide fragments (having contents like images, texts, lists etc) to ViewPager Adapter, And set the adapter up with tabs, And finally use view pager to set up the adapter.

Is that all fixed way of assembling/using these components or is there any more to it?

I didn't find any document with diagrams and examples describing it all in one place. Would be great if someone can share. Thanks!


Is that all fixed way of assembling/using these components or is there any more to it?

After you go through this, you will realise that View Pagers are basically used for Screen Slides.

This means you can pretty much not integrate your TabLayout with your ViewPager and it would still work, you can still slide fragments from left to right or vice versa.

Yes ViewPagers need Fragments

In android you can show one activity at a time. To show more information on a single screen you need to figure some way out. So you can't have activity inside an activity, thus can't have an activity inside the View Pager; but hey! you can have Fragments since they are not activities and they can hold views together.

If you provide only single fragment in a view pager it would still work.