Add a slide to ViewPager

Source: Internet
Author: User

Add a slide to ViewPager
When using Netease news, if you slide the page between the left and right, you will find a red bar under the Tab above. You can slide with the sliding of the following page to specify the current page. After research, we found that ViewPager and custom View can be used to achieve similar results.

When using Viewpager, we generally register an OnPageChangeListener. Let's take a look at its code:

 
  1. /**
  2. * Callback interface for responding to changing state of the selected page.
  3. */
  4. public interface OnPageChangeListener {

  5. /**
  6. * This method will be invoked when the current page is scrolled, either as part
  7. * of a programmatically initiated smooth scroll or a user initiated touch scroll.
  8. *
  9. * @param position Position index of the first page currently being displayed.
  10. * Page position+1 will be visible if positionOffset is nonzero.
  11. * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
  12. * @param positionOffsetPixels Value in pixels indicating the offset from position.
  13. */
  14. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);

  15. /**
  16. * This method will be invoked when a new page becomes selected. Animation is not
  17. * necessarily complete.
  18. *
  19. * @param position Position index of the new selected page.
  20. */
  21. public void onPageSelected(int position);

  22. /**
  23. * Called when the scroll state changes. Useful for discovering when the user
  24. * begins dragging, when the pager is automatically settling to the current page,
  25. * or when it is fully stopped/idle.
  26. *
  27. * @param state The new scroll state.
  28. * @see ViewPager#SCROLL_STATE_IDLE
  29. * @see ViewPager#SCROLL_STATE_DRAGGING
  30. * @see ViewPager#SCROLL_STATE_SETTLING
  31. */
  32. public void onPageScrollStateChanged(int state);
  33. }
You can see that this interface has three callback methods. The onPageScrolled method is called when sliding. This method has three parameters. You can see that position is the sequence number of the first page currently displayed. positionOffset is a number ranging from 0 to 1, which indicates the position of the current page sliding, the larger the value, the larger the sliding amplitude. You need to know the sliding position through this method.

You need a custom View to draw this slide. When the onPageScrolled method is called, we need to redraw the slider to locate the position. The following is the custom VIew code:

 
  1. Public class ScllorTabView extends View {

  2. Private int mTabNum, mCurrentNum;
  3. Private float mWidth, mTabWidth, mOffset;
  4. Private final Paint mPaint = new Paint (Paint. ANTI_ALIAS_FLAG );
  5. Private int mBeginColor;
  6. Private int mEndColor;
  7. LinearGradient gradient;

  8. Public ScllorTabView (Context context, AttributeSet attrs ){
  9. Super (context, attrs );
  10. }

  11. Public void setTabNum (int n ){
  12. MTabNum = n;
  13. }

  14. Public void setCurrentNum (int n ){
  15. MCurrentNum = n;
  16. MOffset = 0;
  17. }

  18. Public void setOffset (int position, float offset ){
  19. If (offset = 0 ){
  20. Return;
  21. }
  22. MCurrentNum = position;
  23. MOffset = offset;
  24. Invalidate ();
  25. }

  26. @ Override
  27. Protected void onDraw (Canvas canvas ){
  28. Super. onDraw (canvas );
  29. If (mTabWidth = 0 ){
  30. MWidth = getWidth ();
  31. MTabWidth = mWidth/mTabNum;
  32. }
  33. // Calculate the position of the slider based on the position and offset.
  34. Float left = (mCurrentNum + mOffset) * mTabWidth;
  35. Final float right = left + mTabWidth;
  36. Final float top = getPaddingTop ();
  37. Final float bottom = getHeight ()-getPaddingBottom ();

  38. // If (gradient = null ){
  39. LinearGradient gradient = new LinearGradient (left, getHeight (), right,
  40. GetHeight (), mBeginColor, mEndColor, Shader. TileMode. CLAMP );
  41. MPaint. setShader (gradient );
  42. //}
  43. Canvas. drawRect (left, top, right, bottom, mPaint );
  44. }

  45. Public void setSelectedColor (int color, int color2 ){
  46. MBeginColor = color;
  47. MEndColor = color2;

  48. }
  49. }
Here, LinearGradient is used to set gradient, and can also be removed. Finally, you only need to add the code to call the update:

 
  1. @Override
  2. public void onPageScrolled(int position, float positionOffset,
  3. int positionOffsetPixels) {
  4. mScllorTabView.setOffset(position, positionOffset);
  5. }
Okay. Let's take a look at the implementation results.











Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.