Android provides ScrollView rolling monitoring to achieve the suspension of purchases by Meituan and public comments.

Source: Internet
Author: User

Android provides ScrollView rolling monitoring to achieve the suspension of purchases by Meituan and public comments.

Post please indicate this article from xiaanming blog (http://blog.csdn.net/xiaanming), please respect others' hard work results, thank you!

With the rapid development of the mobile Internet, it has been closely related to our lives. Many people can see in the bus and subway lines looking down at their mobile phone screens, since then, the word "Bow family" has emerged. As a mobile industry developer, I myself am also a "Bow family ", during the commuting time, I took a look at the news on the bus and sent the time. Sometimes I also looked at some Interface Effects of popular apps. Why are apps so popular? There is also a direct relationship with user experience and uidesign. Recently, the following effects were seen in the apps of Meituan and the public comments. I feel that the users are good and user-friendly, so I tried to implement it myself, next, I will explain the Implementation ideas!

As shown in figure (2), when the layout is moved up to the navigation bar layout, the layout is immediately snapped up and pasted under the navigation bar layout. Other la S below can still be slide, when we slide down, the layout of the flash sales immediately slides down, which seems a bit complicated, but you may suddenly realize the idea.

When we slide upwards, we can determine whether the layout of the flash sale will slide under the navigation bar layout. If the flash sale is on the top of the navigation bar, we created a new floating box for flash sales to be displayed under the navigation bar, so that we can immediately snap up and paste it under the navigation bar. When we slide down, when the layout of an instant flash sale is just below the newly created instant flash sale suspension box, we will remove the instant flash sale suspension box. It may be a bit difficult to say. Now that we know our ideas, next we will implement the effect.

Create an Android project named MeiTuanDemo. Let's take a look at the layout of the Buy now (buy_layout.xml). Here, I cut the picture from the Meituan for convenience.

  1. Android: orientation = "horizontal"
  2. Android: layout_width = "fill_parent"
  3. Android: layout_height = "wrap_content">
  4. Android: id = "@ + id/buy_layout"
  5. Android: layout_width = "fill_parent"
  6. Android: layout_height = "wrap_content"
  7. Android: background = "@ drawable/buy"/>

  8. The layout of the flash sale is realized. Next, the layout of the main interface is implemented. The navigation bar is located above. For convenience, the pictures taken directly from the Meituan are arranged. Then, the ViewPager layout below is immediately snapped up the layout, other la s are placed in ScrollView, and the interface is quite simple.

    1. Xmlns: tools = "http://schemas.android.com/tools"
    2. Android: layout_width = "match_parent"
    3. Android: layout_height = "match_parent"
    4. Android: orientation = "vertical">
    5. Android: id = "@ + id/imageView1"
    6. Android: scaleType = "centerCrop"
    7. Android: layout_width = "match_parent"
    8. Android: layout_height = "45dip"
    9. Android: src = "@ drawable/navigation_bar"/>
    10. Android: id = "@ + id/scrollView"
    11. Android: layout_width = "fill_parent"
    12. Android: layout_height = "fill_parent">
    13. Android: layout_width = "match_parent"
    14. Android: layout_height = "wrap_content"
    15. Android: orientation = "vertical">
    16. Android: id = "@ + id/iamge"
    17. Android: layout_width = "match_parent"
    18. Android: layout_height = "wrap_content"
    19. Android: background = "@ drawable/pic"
    20. Android: scaleType = "centerCrop"/>
    21. Android: id = "@ + id/buy"
    22. Layout = "@ layout/buy_layout"/>
    23. Android: layout_width = "match_parent"
    24. Android: layout_height = "wrap_content"
    25. Android: background = "@ drawable/one"
    26. Android: scaleType = "centerCrop"/>
    27. Android: layout_width = "match_parent"
    28. Android: layout_height = "wrap_content"
    29. Android: background = "@ drawable/one"
    30. Android: scaleType = "centerCrop"/>
    31. Android: layout_width = "match_parent"
    32. Android: layout_height = "wrap_content"
    33. Android: background = "@ drawable/one"
    34. Android: scaleType = "centerCrop"/>
    35. You will find that the above main interface layout is not a ScrollView, but a custom MyScrollView. Next let's take a look at the code in the MyScrollView class.

      1. Package com. example. meituandemo;
      2. Import android. content. Context;
      3. Import android. OS. Handler;
      4. Import android. util. AttributeSet;
      5. Import android. view. MotionEvent;
      6. Import android. widget. ScrollView;
      7. /**
      8. * Blog: http://blog.csdn.net/xiaanming
      9. *
      10. * @ Author xiaanming
      11. *
      12. */
      13. Public class MyScrollView extends ScrollView {
      14. Private OnScrollListener onScrollListener;
      15. /**
      16. * It is mainly used when the user's finger leaves MyScrollView, and MyScrollView continues to slide. We use it to save the distance of Y and then compare
      17. */
      18. Private int lastScrollY;
      19. Public MyScrollView (Context context ){
      20. This (context, null );
      21. }
      22. Public MyScrollView (Context context, AttributeSet attrs ){
      23. This (context, attrs, 0 );
      24. }
      25. Public MyScrollView (Context context, AttributeSet attrs, int defStyle ){
      26. Super (context, attrs, defStyle );
      27. }
      28. /**
      29. * Set the scrolling Interface
      30. * @ Param onScrollListener
      31. */
      32. Public void setOnScrollListener (OnScrollListener onScrollListener ){
      33. This. onScrollListener = onScrollListener;
      34. }
      35. /**
      36. * It is used to obtain the Y distance of MyScrollView scrolling when the user's finger leaves MyScrollView, and then calls back to the onScroll method.
      37. */
      38. Private Handler handler = new Handler (){
      39. Public void handleMessage (android. OS. Message msg ){
      40. Int scrollY = MyScrollView. this. getScrollY ();
      41. // At this time, the distance is not the same as the distance in the recording. messages are sent to handler within 5 milliseconds.
      42. If (lastScrollY! = ScrollY ){
      43. LastScrollY = scrollY;
      44. Handler. sendMessageDelayed (handler. obtainMessage (), 5 );
      45. }
      46. If (onScrollListener! = Null ){
      47. OnScrollListener. onScroll (scrollY );
      48. }
      49. };
      50. };
      51. /**
      52. * Override onTouchEvent. When the user's hand is on MyScrollView,
      53. * Directly call back the Y-direction distance of MyScrollView to the onScroll method. When the user raises his hand,
      54. * MyScrollView may still slide, so when the user raises his hand, we send a message to the handler every Five milliseconds and process the message in the handler.
      55. * MyScrollView sliding distance
      56. */
      57. @ Override
      58. Public boolean onTouchEvent (MotionEvent ev ){
      59. If (onScrollListener! = Null ){
      60. OnScrollListener. onScroll (lastScrollY = this. getScrollY ());
      61. }
      62. Switch (ev. getAction ()){
      63. Case MotionEvent. ACTION_UP:
      64. Handler. sendMessageDelayed (handler. obtainMessage (), 5 );
      65. Break;
      66. }
      67. Return super. onTouchEvent (ev );
      68. }
      69. /**
      70. *
      71. * Rolling callback Interface
      72. *
      73. * @ Author xiaanming
      74. *
      75. */
      76. Public interface OnScrollListener {
      77. /**
      78. * Callback method. The Y-direction distance of MyScrollView is returned.
      79. * @ Param scrollY
      80. *,
      81. */
      82. Public void onScroll (int scrollY );
      83. }
      84. }

        You may understand the code by listening to the rolling Y value of ScrollView. We know that ScrollView does not implement rolling listening, so we must monitor ScrollView on our own, we naturally want to listen to the rolling Y axis in the onTouchEvent () method, but you will find that when we slide the ScrollView, we leave the ScrollView with our fingers. It may continue to slide for a distance, so we choose to judge whether the ScrollView stops sliding every 5 milliseconds when the user's finger leaves, callback the ScrollView's rolling Y value to the onscrolly (int scrollY) method of the OnScrollListener interface, we only need to call ScrollView. We only need to call the setOnScrollListener Method on ScrollView to listen to the scrolling Y value.

        The implementation of the ScrollView scrolling Y value listening, next is simple, we just need to display the buy now floating box and remove the floating box, next look at the main interface Activity code writing

        1. Package com. example. meituandemo;
        2. Import android. app. Activity;
        3. Import android. content. Context;
        4. Import android. graphics. PixelFormat;
        5. Import android. OS. Bundle;
        6. Import android. view. Gravity;
        7. Import android. view. LayoutInflater;
        8. Import android. view. View;
        9. Import android. view. WindowManager;
        10. Import android. view. WindowManager. LayoutParams;
        11. Import android. widget. LinearLayout;
        12. Import com. example. meituandemo. MyScrollView. OnScrollListener;
        13. /**
        14. * Blog: http://blog.csdn.net/xiaanming
        15. *
        16. * @ Author xiaanming
        17. *
        18. */
        19. Public class MainActivity extends Activity implements OnScrollListener {
        20. Private MyScrollView myScrollView;
        21. Private LinearLayout mBuyLayout;
        22. Private WindowManager mWindowManager;
        23. /**
        24. * Mobile phone screen width
        25. */
        26. Private int screenWidth;
        27. /**
        28. * Floating box View
        29. */
        30. Private static View suspendView;
        31. /**
        32. * Parameters of the Floating Box
        33. */
        34. Private static WindowManager. LayoutParams suspendLayoutParams;
        35. /**
        36. * Layout height
        37. */
        38. Private int buyLayoutHeight;
        39. /**
        40. * The distance between myScrollView and the top of its parent class Layout
        41. */
        42. Private int myScrollViewTop;
        43. /**
        44. * The distance between the purchased layout and the top of its parent Layout
        45. */
        46. Private int buyLayoutTop;
        47. @ Override
        48. Protected void onCreate (Bundle savedInstanceState ){
        49. Super. onCreate (savedInstanceState );
        50. SetContentView (R. layout. activity_main );
        51. MyScrollView = (MyScrollView) findViewById (R. id. scrollView );
        52. MBuyLayout = (LinearLayout) findViewById (R. id. buy );
        53. MyScrollView. setOnScrollListener (this );
        54. MWindowManager = (WindowManager) getSystemService (Context. WINDOW_SERVICE );
        55. ScreenWidth = mWindowManager. getdefadisplay display (). getWidth ();
        56. }
        57. /**
        58. * When the window has focus, that is, when all la s are drawn, we can obtain the height of the purchased layout and the position of myScrollView at the top of the parent layout.
        59. */
        60. @ Override
        61. Public void onWindowFocusChanged (boolean hasFocus ){
        62. Super. onWindowFocusChanged (hasFocus );
        63. If (hasFocus ){
        64. BuyLayoutHeight = mBuyLayout. getHeight ();
        65. BuyLayoutTop = mBuyLayout. getTop ();
        66. MyScrollViewTop = myScrollView. getTop ();
        67. }
        68. }
        69. /**
        70. * The Rolling callback method. When the rolling Y distance is greater than or equal to the purchased layout distance from the top of the parent layout, the purchased floating box is displayed.
        71. * If the distance between the rolling Y and the parent layout is smaller than the purchased layout, and the height of the purchased layout is added to the top of the parent layout, the suspended box is removed.
        72. *
        73. */
        74. @ Override
        75. Public void onScroll (int scrollY ){
        76. If (scrollY> = buyLayoutTop ){
        77. If (suspendView = null ){
        78. ShowSuspend ();
        79. }
        80. } Else if (scrollY <= buyLayoutTop + buyLayoutHeight ){
        81. If (suspendView! = Null ){
        82. RemoveSuspend ();
        83. }
        84. }
        85. }
        86. /**
        87. * Display the suspended box for purchase
        88. */
        89. Private void showSuspend (){
        90. If (suspendView = null ){
        91. SuspendView = LayoutInflater. from (this). inflate (R. layout. buy_layout, null );
        92. If (suspendLayoutParams = null ){
        93. SuspendLayoutParams = new LayoutParams ();
        94. SuspendLayoutParams. type = LayoutParams. TYPE_PHONE; // The type of the floating window, which is generally set to 2002, indicates that the window is on top of all applications, but under the status bar
        95. SuspendLayoutParams. format = PixelFormat. RGBA_8888;
        96. SuspendLayoutParams. flags = LayoutParams. FLAG_NOT_TOUCH_MODAL
        97. | LayoutParams. FLAG_NOT_FOCUSABLE; // floating window behavior, such as non-focusing and non-Modal Dialog Boxes
        98. SuspendLayoutParams. gravity = Gravity. TOP; // alignment of the floating window
        99. SuspendLayoutParams. width = screenWidth;
        100. SuspendLayoutParams. height = buyLayoutHeight;
        101. SuspendLayoutParams. x = 0; // The Position of the floating Window X
        102. SuspendLayoutParams. y = myScrollViewTop; // The Position of the floating window Y.
        103. }
        104. }
        105. MWindowManager. addView (suspendView, suspendLayoutParams );
        106. }
        107. /**
        108. * Remove the purchased floating box
        109. */
        110. Private void removeSuspend (){
        111. If (suspendView! = Null ){
        112. MWindowManager. removeView (suspendView );
        113. SuspendView = null;
        114. }
        115. }
        116. }


          The above code is relatively simple. The display and removal of floating boxes are determined based on the sliding distance of ScrollView. The implementation of floating boxes is mainly implemented through the WindowManager class, call the addView method of this class to add a floating box. removeView is used to remove the floating box.
          The above code is used to achieve the effect of the comments. Before running the project, we must add it to AndroidManifest. xml.


          Let's run the project and check the effect.

Related Article

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.