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.
-
- Android: orientation = "horizontal"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content">
-
- Android: id = "@ + id/buy_layout"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: background = "@ drawable/buy"/>
-
-
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.
- Xmlns: tools = "http://schemas.android.com/tools"
- Android: layout_width = "match_parent"
- Android: layout_height = "match_parent"
- Android: orientation = "vertical">
-
- Android: id = "@ + id/imageView1"
- Android: scaleType = "centerCrop"
- Android: layout_width = "match_parent"
- Android: layout_height = "45dip"
- Android: src = "@ drawable/navigation_bar"/>
-
-
- Android: id = "@ + id/scrollView"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent">
-
- Android: layout_width = "match_parent"
- Android: layout_height = "wrap_content"
- Android: orientation = "vertical">
-
- Android: id = "@ + id/iamge"
- Android: layout_width = "match_parent"
- Android: layout_height = "wrap_content"
- Android: background = "@ drawable/pic"
- Android: scaleType = "centerCrop"/>
-
- Android: id = "@ + id/buy"
- Layout = "@ layout/buy_layout"/>
-
- Android: layout_width = "match_parent"
- Android: layout_height = "wrap_content"
- Android: background = "@ drawable/one"
- Android: scaleType = "centerCrop"/>
-
- Android: layout_width = "match_parent"
- Android: layout_height = "wrap_content"
- Android: background = "@ drawable/one"
- Android: scaleType = "centerCrop"/>
-
- Android: layout_width = "match_parent"
- Android: layout_height = "wrap_content"
- Android: background = "@ drawable/one"
- Android: scaleType = "centerCrop"/>
-
-
-
-
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.
- Package com. example. meituandemo;
-
- Import android. content. Context;
- Import android. OS. Handler;
- Import android. util. AttributeSet;
- Import android. view. MotionEvent;
- Import android. widget. ScrollView;
- /**
- * Blog: http://blog.csdn.net/xiaanming
- *
- * @ Author xiaanming
- *
- */
- Public class MyScrollView extends ScrollView {
- Private OnScrollListener onScrollListener;
- /**
- * 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
- */
- Private int lastScrollY;
-
- Public MyScrollView (Context context ){
- This (context, null );
- }
-
- Public MyScrollView (Context context, AttributeSet attrs ){
- This (context, attrs, 0 );
- }
-
- Public MyScrollView (Context context, AttributeSet attrs, int defStyle ){
- Super (context, attrs, defStyle );
- }
-
- /**
- * Set the scrolling Interface
- * @ Param onScrollListener
- */
- Public void setOnScrollListener (OnScrollListener onScrollListener ){
- This. onScrollListener = onScrollListener;
- }
-
-
- /**
- * 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.
- */
- Private Handler handler = new Handler (){
-
- Public void handleMessage (android. OS. Message msg ){
- Int scrollY = MyScrollView. this. getScrollY ();
-
- // At this time, the distance is not the same as the distance in the recording. messages are sent to handler within 5 milliseconds.
- If (lastScrollY! = ScrollY ){
- LastScrollY = scrollY;
- Handler. sendMessageDelayed (handler. obtainMessage (), 5 );
- }
- If (onScrollListener! = Null ){
- OnScrollListener. onScroll (scrollY );
- }
-
- };
-
- };
-
- /**
- * Override onTouchEvent. When the user's hand is on MyScrollView,
- * Directly call back the Y-direction distance of MyScrollView to the onScroll method. When the user raises his hand,
- * 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.
- * MyScrollView sliding distance
- */
- @ Override
- Public boolean onTouchEvent (MotionEvent ev ){
- If (onScrollListener! = Null ){
- OnScrollListener. onScroll (lastScrollY = this. getScrollY ());
- }
- Switch (ev. getAction ()){
- Case MotionEvent. ACTION_UP:
- Handler. sendMessageDelayed (handler. obtainMessage (), 5 );
- Break;
- }
- Return super. onTouchEvent (ev );
- }
-
-
- /**
- *
- * Rolling callback Interface
- *
- * @ Author xiaanming
- *
- */
- Public interface OnScrollListener {
- /**
- * Callback method. The Y-direction distance of MyScrollView is returned.
- * @ Param scrollY
- *,
- */
- Public void onScroll (int scrollY );
- }
-
-
-
- }
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
- Package com. example. meituandemo;
-
- Import android. app. Activity;
- Import android. content. Context;
- Import android. graphics. PixelFormat;
- Import android. OS. Bundle;
- Import android. view. Gravity;
- Import android. view. LayoutInflater;
- Import android. view. View;
- Import android. view. WindowManager;
- Import android. view. WindowManager. LayoutParams;
- Import android. widget. LinearLayout;
- Import com. example. meituandemo. MyScrollView. OnScrollListener;
- /**
- * Blog: http://blog.csdn.net/xiaanming
- *
- * @ Author xiaanming
- *
- */
- Public class MainActivity extends Activity implements OnScrollListener {
- Private MyScrollView myScrollView;
- Private LinearLayout mBuyLayout;
- Private WindowManager mWindowManager;
- /**
- * Mobile phone screen width
- */
- Private int screenWidth;
- /**
- * Floating box View
- */
- Private static View suspendView;
- /**
- * Parameters of the Floating Box
- */
- Private static WindowManager. LayoutParams suspendLayoutParams;
- /**
- * Layout height
- */
- Private int buyLayoutHeight;
- /**
- * The distance between myScrollView and the top of its parent class Layout
- */
- Private int myScrollViewTop;
-
- /**
- * The distance between the purchased layout and the top of its parent Layout
- */
- Private int buyLayoutTop;
-
-
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
-
- MyScrollView = (MyScrollView) findViewById (R. id. scrollView );
- MBuyLayout = (LinearLayout) findViewById (R. id. buy );
-
- MyScrollView. setOnScrollListener (this );
- MWindowManager = (WindowManager) getSystemService (Context. WINDOW_SERVICE );
- ScreenWidth = mWindowManager. getdefadisplay display (). getWidth ();
- }
-
- /**
- * 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.
- */
- @ Override
- Public void onWindowFocusChanged (boolean hasFocus ){
- Super. onWindowFocusChanged (hasFocus );
- If (hasFocus ){
- BuyLayoutHeight = mBuyLayout. getHeight ();
- BuyLayoutTop = mBuyLayout. getTop ();
-
- MyScrollViewTop = myScrollView. getTop ();
- }
- }
-
-
-
- /**
- * 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.
- * 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.
- *
- */
- @ Override
- Public void onScroll (int scrollY ){
- If (scrollY> = buyLayoutTop ){
- If (suspendView = null ){
- ShowSuspend ();
- }
- } Else if (scrollY <= buyLayoutTop + buyLayoutHeight ){
- If (suspendView! = Null ){
- RemoveSuspend ();
- }
- }
- }
-
-
- /**
- * Display the suspended box for purchase
- */
- Private void showSuspend (){
- If (suspendView = null ){
- SuspendView = LayoutInflater. from (this). inflate (R. layout. buy_layout, null );
- If (suspendLayoutParams = null ){
- SuspendLayoutParams = new LayoutParams ();
- 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
- SuspendLayoutParams. format = PixelFormat. RGBA_8888;
- SuspendLayoutParams. flags = LayoutParams. FLAG_NOT_TOUCH_MODAL
- | LayoutParams. FLAG_NOT_FOCUSABLE; // floating window behavior, such as non-focusing and non-Modal Dialog Boxes
- SuspendLayoutParams. gravity = Gravity. TOP; // alignment of the floating window
- SuspendLayoutParams. width = screenWidth;
- SuspendLayoutParams. height = buyLayoutHeight;
- SuspendLayoutParams. x = 0; // The Position of the floating Window X
- SuspendLayoutParams. y = myScrollViewTop; // The Position of the floating window Y.
- }
- }
-
- MWindowManager. addView (suspendView, suspendLayoutParams );
- }
-
-
- /**
- * Remove the purchased floating box
- */
- Private void removeSuspend (){
- If (suspendView! = Null ){
- MWindowManager. removeView (suspendView );
- SuspendView = null;
- }
- }
-
- }
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.