All Products
Search
Document Center

Switch tab component

Last Updated: May 25, 2021

AUSegment is used to replace APSwitchTab, with related codes reconstructed and the original APIs reserved for smooth scrolling.

If the SDK version is later than 10.0.20, this component supports scrollable switching tab. The left and right spacing of each Tab is 14dp:

  • Scrolling switching is supported when the width of all tabs exceeds the initial width.
  • When the width of all tabs is less than the initial width, the option indicating whether to equally divide the tabs is provided. By default, equal division is selected.

Sample image

Dependency

See Quick start.

API description

 
  1. /**
  2. * Reset the tab view.
  3. */
  4. public void resetTabView(String[] tabNameArray)
  5. /**
  6. * Adjust the position of the selected line at the bottom. Generally, this method is called in the onPageScrolled callback of ViewPager.
  7. *
  8. * @param position The start position.
  9. * @param positionOffset The offset of the start position (in percentage).
  10. */
  11. public void adjustLinePosition(int position, float positionOffset)
  12. /**
  13. * Select a tab but do not adjust the position of the bottom line, which is suitable for tab switching using ViewPager.
  14. * Implement the selected line at the bottom by calling adjustLinePosition in the onPageScrolled callback of ViewPager.
  15. *
  16. * @param position The position.
  17. */
  18. public void selectTab(int position)
  19. /**
  20. * Select a tab and adjust the position of the bottom line.<br>
  21. * Used in non-ViewPager tab switching scenarios. The duration of the cutscene before each tab switching interval is 250 ms
  22. *
  23. * @param position The target selected tab.
  24. */
  25. public void selectTabAndAdjustLine(int position)
  26. /**
  27. * Select a tab and adjust the position of the bottom line.<br>
  28. * Used in non-ViewPager tab switching scenarios. The duration of the cutscene before each tab switching interval is customized.<br>
  29. * If the next animation is enabled before the previous animation finished, the previous animation is immediately terminated. The next animation starts after the final position of the previous animation is located.
  30. *
  31. * @param position The target position.
  32. * @param during The duration of the cutscene before each tab switching interval.
  33. */
  34. public void selectTabAndAdjustLine(int position, int during)
  35. /**
  36. * Set the tab switching listener.
  37. *
  38. * @param tabSwitchListener
  39. */
  40. public void setTabSwitchListener(TabSwitchListener tabSwitchListener)
  41. /**
  42. * Add a red dot to the specified position.
  43. * @param view Red dot view.
  44. * @param position
  45. */
  46. public void addTextRightView(View view, int position)
  47. /**
  48. * Add a red dot to the specified position.
  49. * @param view Red dot view.
  50. * @param params The relative position of the red dot.
  51. * @param position
  52. */
  53. public void addTextRightView(View view, RelativeLayout.LayoutParams params, int position)

API for scrollable tab

To use the scrolling function, set the custom property parameter scroll to true, for example, add app:scroll="true" to the layout file. The scrolling tab supports only the following four APIs. Other APIs are invalid to the scrollable tab.

 
  1. /**
  2. * Set the tab switching listener.
  3. * @param tabSwitchListener
  4. */
  5. public void setTabSwitchListener(TabSwitchListener tabSwitchListener)
  6. /**
  7. * Set the data source.
  8. * @param list
  9. */
  10. public void init(List<ItemCategory> list)
  11. /**
  12. * Set the selected tab.
  13. * @param position
  14. */
  15. public void setCurrentSelTab(int position)
  16. /**
  17. * Each tab has a fixed left-right spacing of 14dp. An option indicating whether to equally dividing the APIs is provided when the width of all tabs is less than the initial width.
  18. * By default, equal division is selected. If you set this parameter to false, equal division is not allowed.
  19. * @param divideAutoSize
  20. */
  21. public void setDivideAutoSize(boolean divideAutoSize)

Custom properties

The scroll property is added to the SDK of a version of 10.0.20 or later.

Property Purpose Type
tabCount The tab quantity. Integer
tab1Text Tab1 text. String, Reference
tab2Text Tab2 text. String, Reference
tab3Text Tab3 text. String, Reference
tab4Text Tab4 text. String, Reference
tabTextArray The tab text array. String, Reference
uniformlySpaced Whether self-adaptive. Boolean
tabTextColor The text color. Reference, Color
tabTextSize The text size. Dimension
buttomLineColor The color of the bottom line. Color, Reference
scroll Whether to support scrolling. Boolean

Sample code

XML

 
  1. <com.alipay.mobile.antui.segement.AUSegment
  2. android:id="@+id/switchtab_three"
  3. android:layout_width="fill_parent"
  4. android:layout_height="50dp"
  5. android:layout_marginTop="10dp"
  6. app:tab1Text="Left text"
  7. app:tab2Text="Middle text"
  8. app:tab3Text="Right text"
  9. app:tabCount="3"/>