Android对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果

我之前写了一篇关于美团网,大众点评的购买框效果的文章Android对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果,我自己感觉效果并不是很好,如果快速滑动界面,显示悬浮框的时候会出现一卡的现象,有些朋友说有时候会出现两个布局的情况,特别是对ScrollView滚动的Y值得监听,我还使用了Handler来获取,还有朋友给我介绍了Scrolling Tricks这个东西,我下载试了下,确实美团网,大众点评的购买框用的是这种效果,但是Scrolling Tricks只能在API11以上使用,这个有点小悲剧,然后我做了下修改,并将实现思路分享给大家,实现起来很简单

首先还是要先对ScrollView进行滚动监听,直接在onScrollChanged()方法中就能获取滚动的Y值,之前那篇文章使用了Handler,走弯路了,直接看代码吧

  1. package com.example.meituandemo;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.widget.ScrollView;  
  6. /** 
  7.  * @blog http://blog.csdn.net/xiaanming 
  8.  *  
  9.  * @author xiaanming 
  10.  * 
  11.  */  
  12. public class MyScrollView extends ScrollView {  
  13.     private OnScrollListener onScrollListener;  
  14.       
  15.     public MyScrollView(Context context) {  
  16.         this(context, null);  
  17.     }  
  18.       
  19.     public MyScrollView(Context context, AttributeSet attrs) {  
  20.         this(context, attrs, 0);  
  21.     }  
  22.   
  23.     public MyScrollView(Context context, AttributeSet attrs, int defStyle) {  
  24.         super(context, attrs, defStyle);  
  25.     }  
  26.       
  27.     /** 
  28.      * 设置滚动接口 
  29.      * @param onScrollListener 
  30.      */  
  31.     public void setOnScrollListener(OnScrollListener onScrollListener) {  
  32.         this.onScrollListener = onScrollListener;  
  33.     }  
  34.       
  35.       
  36.     @Override  
  37.     public int computeVerticalScrollRange() {  
  38.         return super.computeVerticalScrollRange();  
  39.     }  
  40.       
  41.   
  42.     @Override  
  43.     protected void onScrollChanged(int l, int t, int oldl, int oldt) {  
  44.         super.onScrollChanged(l, t, oldl, oldt);  
  45.         if(onScrollListener != null){  
  46.             onScrollListener.onScroll(t);  
  47.         }  
  48.     }  
  49.   
  50.   
  51.   
  52.     /** 
  53.      *  
  54.      * 滚动的回调接口 
  55.      *  
  56.      * @author xiaanming 
  57.      * 
  58.      */  
  59.     public interface OnScrollListener{  
  60.         /** 
  61.          * 回调方法, 返回MyScrollView滑动的Y方向距离 
  62.          * @param scrollY 
  63.          *              、 
  64.          */  
  65.         public void onScroll(int scrollY);  
  66.     }  
  67.       
  68.       
  69.   
  70. }  
接下来看看主界面的布局文件
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:id="@+id/parent_layout"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ImageView  
  9.         android:id="@+id/imageView1"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="45dip"  
  12.         android:scaleType="centerCrop"  
  13.         android:src="@drawable/navigation_bar" />  
  14.   
  15.     <com.example.meituandemo.MyScrollView  
  16.         android:id="@+id/scrollView"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="fill_parent" >  
  19.   
  20.         <FrameLayout  
  21.             android:layout_width="match_parent"  
  22.             android:layout_height="wrap_content" >  
  23.   
  24.             <LinearLayout  
  25.                 android:layout_width="match_parent"  
  26.                 android:layout_height="wrap_content"  
  27.                 android:orientation="vertical" >  
  28.   
  29.                 <ImageView  
  30.                     android:id="@+id/iamge"  
  31.                     android:layout_width="match_parent"  
  32.                     android:layout_height="wrap_content"  
  33.                     android:background="@drawable/pic"  
  34.                     android:scaleType="centerCrop" />  
  35.   
  36.                 <include  
  37.                     android:id="@+id/buy"  
  38.                     layout="@layout/buy_layout" />  
  39.   
  40.                 <ImageView  
  41.                     android:layout_width="match_parent"  
  42.                     android:layout_height="wrap_content"  
  43.                     android:background="@drawable/one"  
  44.                     android:scaleType="centerCrop" />  
  45.   
  46.                 <ImageView  
  47.                     android:layout_width="match_parent"  
  48.                     android:layout_height="wrap_content"  
  49.                     android:background="@drawable/one"  
  50.                     android:scaleType="centerCrop" />  
  51.   
  52.                 <ImageView  
  53.                     android:layout_width="match_parent"  
  54.                     android:layout_height="wrap_content"  
  55.                     android:background="@drawable/one"  
  56.                     android:scaleType="centerCrop" />  
  57.             </LinearLayout>  
  58.   
  59.             <include  
  60.                 android:id="@+id/top_buy_layout"  
  61.                 layout="@layout/buy_layout" />  
  62.         </FrameLayout>  
  63.     </com.example.meituandemo.MyScrollView>  
  64.   
  65. </LinearLayout>  

下面是布局的效果图


从主界面的布局你可以看出,我们在上面放置了一个购买的布局,可能你会想,先让上面的布局隐藏起来,等下面的布局滑动上来就将其显示出来,如果这样子就跟我之前写的那篇文章差不多,效果不是很棒,所以这篇修改版的肯定不是这样子的,我们还是先看主界面的代码吧

  1. package com.example.meituandemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.ViewTreeObserver.OnGlobalLayoutListener;  
  6. import android.widget.LinearLayout;  
  7.   
  8. import com.example.meituandemo.MyScrollView.OnScrollListener;  
  9.   
  10. /** 
  11.  * @blog http://blog.csdn.net/xiaanming 
  12.  *  
  13.  * @author xiaanming 
  14.  * 
  15.  */  
  16. public class MainActivity extends Activity implements OnScrollListener{  
  17.     /** 
  18.      * 自定义的MyScrollView 
  19.      */  
  20.     private MyScrollView myScrollView;  
  21.     /** 
  22.      * 在MyScrollView里面的购买布局 
  23.      */  
  24.     private LinearLayout mBuyLayout;  
  25.     /** 
  26.      * 位于顶部的购买布局 
  27.      */  
  28.     private LinearLayout mTopBuyLayout;  
  29.       
  30.   
  31.     @SuppressWarnings("deprecation")  
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);   
  35.         setContentView(R.layout.activity_main);  
  36.           
  37.         myScrollView = (MyScrollView) findViewById(R.id.scrollView);  
  38.         mBuyLayout = (LinearLayout) findViewById(R.id.buy);  
  39.         mTopBuyLayout = (LinearLayout) findViewById(R.id.top_buy_layout);  
  40.           
  41.         myScrollView.setOnScrollListener(this);  
  42.           
  43.         //当布局的状态或者控件的可见性发生改变回调的接口  
  44.         findViewById(R.id.parent_layout).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
  45.               
  46.             @Override  
  47.             public void onGlobalLayout() {  
  48.                 //这一步很重要,使得上面的购买布局和下面的购买布局重合  
  49.                 onScroll(myScrollView.getScrollY());  
  50.                   
  51.             }  
  52.         });  
  53.     }  
  54.   
  55.   
  56.   
  57.   
  58.     @Override  
  59.     public void onScroll(int scrollY) {  
  60.         int mBuyLayout2ParentTop = Math.max(scrollY, mBuyLayout.getTop());  
  61.         mTopBuyLayout.layout(0, mBuyLayout2ParentTop, mTopBuyLayout.getWidth(), mBuyLayout2ParentTop + mTopBuyLayout.getHeight());  
  62.     }  
  63.   
  64.   
  65.   
  66. }  
主界面就短短的几行代码,可能看完这些代码你还是没有明白到底是怎么做到的,没关系,我给大家说说,其实我们是让上面的购买布局和下面的购买布局重合起来了,layout()这个方法是确定View的大小和位置的,然后将其绘制出来,里面的四个参数分别是View的四个点的坐标,他的坐标不是相对屏幕的原点,而且相对于他的父布局来说的,
我们在主页面最外层的ViewGroup添加了布局状态改变的监听器,当绘制完了屏幕会回调到方法onGlobalLayout()中,我们在onGlobalLayout()方法中手动调用了下onScroll()方法,刚开始myScrollView.getScrollY()等于0,所以说当scrollY小于mBuyLayout.getTop()的时候,上面的购买布局的上边缘到myScrollView的上边缘的距离等于mBuyLayout.getTop()(即下面布局的上边缘到myScrollView的上边缘)所以刚开始上面的购买布局和下面的购买布局重合了。
当myScrollView向上滚动,而上面购买布局的上边缘始终要和myScrollView的上边缘保持mBuyLayout.getTop()这个距离,所以上面的购买布局也跟着向上滚动,当scrollY大于mBuyLayout.getTop()的时候,表示购买布局上边缘滑动到了导航栏布局,所以此时购买布局的上边缘与myScrollView的上边缘始终要保持scrollY这个距离,所以购买布局才会一直在导航栏下面,就好像粘住了一样,不知道你了解了没有?好了,不过根据这种思路你也可以刚开始使用一个悬浮框来覆盖在下面的购买布局上面,然后onScroll()方法中更新悬浮框的位置,不过悬浮框的x,y不是相对于父布局的,这点要注意下,这样子也能实现效果,不过相对于此,要复杂的多,所以我们遇到类似的功能直接使用这种就行了,简洁明了,好了,你是不迫不及待的想看下效果,那我们接下来就运行下程序吧


运行程序你会发现,无论我们怎么滑动,都不会出现之前那篇文章的那些情况,很流畅吧,这跟美团,大众点评的效果完全一致,好了,修改版的讲解就到这里结束了,有问题的请在下面留言,我会为大家解答的!

项目源码,点击下载


含有多个购买布局的效果,下一个购买布局会将上一个购买布局顶上去,使用方法也很简单,只需要将你需要设置的布局设置Tag为sticky, 如

  1. <FrameLayout  
  2.        android:layout_width="fill_parent"  
  3.        android:layout_height="100dip"  
  4.        android:background="#ff00ffff"  
  5.        android:tag="sticky" >  
  6.   
  7.        <Button  
  8.            android:id="@+id/button"  
  9.            android:layout_width="fill_parent"  
  10.            android:layout_height="wrap_content"  
  11.            android:text="Button" />  
  12.    </FrameLayout>  

这样子这个布局滚动到了顶部就会粘在顶部,大家可以下载试试!


推荐大家下载下面的代码


多个购买布局效果源码,点击下载
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值