In this tutorial you will learn about the Kotlin ViewPager & TabLayout and its application with practical example.
Kotlin ViewPager & TabLayout
ViewPager: This is a layout manager which allows users to swipe left and right through pages of data. In a sense, it is nice way to show the user multiple tabs. We can also add and remove pages(tabs) dynamically. Let’s get more understand about ViewPager.
Class Declaration : The ViewPager class is a subclass of ViewGroup. Below is the class declaration.
1 |
public class ViewPager extends ViewGroup |
XML Declaration: We have to declare ViewPager in the XML then we can access this.
1 2 3 4 5 |
<android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> |
Constant Summary: There are some frequently used constant are listed below.
1.int SCROLL_STATE_DRAGGING: This constant indicates that the pager is being dragged by user.
2.int SCROLL_STATE_IDLE: This constant of ViewPager indicates that the pager is in idle state.
3.int SCROLL_STATE_SETTLING: This constant of ViewPager indicates that the state of ViewPager is in process of settling to a final position.
Method Summary: There are some commonly used methods are listed below.
1.PagerAdapter getAdapter(): This method is used to retrieve current adapter so that user can supply pages.
- void setAdapter(PagerAdapter adapter): This method is used to set a Pager Adapter that will supply views for this pager as needed.
- void setCurrentItem(int item): This method is used to set the currently selected pages.
- void setPagerMargin(int marginPixel): This method is used to set margin between pages.
- void setoffscreenPageLimit(int limit): This method is used to set number of pages.
TabLayout: The TabLayout provides a horizontal layout to display tabs. We can display more screens on a single screen using tabs. We can create sliding as well as non sliding tabs using TabLayout. Let’s get more understand about TabLayout.
Class Declaration:
1 |
public class TabLayout extends HorizontalScrollView |
1 2 3 4 5 6 7 8 9 10 |
XML Declaration: <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="40dp" app:tabGravity="fill" app:tabMode="fixed" app:tabSelectedTextColor="@color/colorAccent" app:tabIndicatorColor="#1c1c1d" /> |
Constant Summary: Some frequently used Constant are listed below.
1.int GRAVITY_CENTER: This constant is used to keep the tabs in the center of the TabLayout.
2.int GRAVITY_FILL: This constant is used to fill the TabLayout as much as possible.
3.int MODE_FIXED: This constant is used to fixed the tabs concurrently.
4.int MODE_SCROLLABLE: This constant is used to Scrollable tabs display a subset of tabs at any given moment and used to contain longer tabs labels and larger number of tabs.
Constructor Summary: Some public constructor are used in TabLayout which are listed below.
1.TabLayout(Context context).
2.TabLayout(Context context, AttributeSet attrs).
3.TabLayout(Context context, AttributeSet attrs, int defStyleAttr).
Method Summary: The most commonly methods are listed below, that will let you get more awareness about TabLayout.
1.void addTab(TabLayout.Tab tab, boolean setSelected): This method is used to add a tab to this Layout with the property of selection.
- void addTab(TabLayout.Tab tab, int position): This method also used to add a tab to this layout but at given position.
- void addTab(TabLayout.Tab tab): This method simply add a tab to this layout.
- void addTab(TabLayout.Tab tab, int position, boolean setSelected): This method is used to add a tab to this layout with given position and with selection status.
5.void addOnTabSelectedListener(TabLayout.OnTabSelectedListener listener): This method is used to invoke TabLayout.OnTabSelectedListener when tab selection changed.
- void clearOnTabSelectedListener(): This method simply removes all TabLayot.OnTabSelectedListeners which were previously added.
- TabLayout.Tab getTabAt(int index): This method returns the tab at the specified position.
- int getTabCount(): This method is used to returns number of tabs which are currently registered with action bar.
9.TabLayout.Tab newTab(): This method is used to create and return a new tab in TabLayout.Tab.
- void removeAllTabs(): This method is used to remove all tabs from the layout.
- void removeTabAt(int position): This method is used to remove a tab from the layout at specified position.
- void setTabGravity(int gravity): This method is used to set the gravity to use when laying out the tabs.
- void setTabMode(int mode): This method is used to set the behavior mode for the Tabs in this layout.
- void setTabTextColors(int normalColor, int selectedColor): This method is used to set the color of Tab’s Text when normal and when selected.
- void setupWithViewPager(ViewPager pager): This method is used to set TabLayout with ViewPager.
So this was about ViewPager and TabLayout, Click on Next for Example of TabLayout with ViewPager.