实现步骤如下:
1,新建一个项目,新建一个MyScrollView继承自ScrollView
 
public class MyScrollView extends ScrollView {
 
    private OnScrollListener onScrollListener;
    
    public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        
    }
    public MyScrollView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }
    //监听滚动事件
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if(onScrollListener != null){   
            onScrollListener.onScroll(t);  //将top传出去
        }
    }
    /**
     * 设置滚动接口
     * @param onScrollListener
     */
    public void setOnScrollListener(OnScrollListener onScrollListener) {
        this.onScrollListener = onScrollListener;
    }
    
    /**
     * 
     * 滚动的回调接口
     * 
     *
     */
    public interface OnScrollListener{
        /**
         * 回调方法, 返回MyScrollView滑动的Y方向距离
         * @param scrollY
         *                 、
         */
        public void onScroll(int scrollY);
    }
 
}
很简单的重写,就是添加了一个回调,在滚动时去更新y值.
 
2,再看activity_main.xml的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent_layout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="45dip"
        android:scaleType="centerCrop"
        android:src="@drawable/navigation_bar" />
 
    <com.example.mymmmdemo.MyScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
 
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
 
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
 
                <ImageView
                    android:id="@+id/iamge"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/pic"
                    android:scaleType="centerCrop" />
 
                <include
                    android:id="@+id/buy"
                    layout="@layout/buy_layout" />
 
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/one"
                    android:scaleType="centerCrop" />
 
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/one"
                    android:scaleType="centerCrop" />
 
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/one"
                    android:scaleType="centerCrop" />
            </LinearLayout>
 
            <include
                android:id="@+id/top_buy_layout"
                layout="@layout/buy_layout" />
        </FrameLayout>
    </com.example.mymmmdemo.MyScrollView>
 
其实它是这样子的:

让悬浮框出现了两次,后面来解释为什么要这么做
 
3,悬浮框的buy_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
 
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/buy" />
 
</LinearLayout>
 
4,打开MainActivity.java
public class MainActivity extends Activity implements OnScrollListener {
 
    private MyScrollView mScrollView;
    /**
     * 在MyScrollView里面的购买布局
     */
    private LinearLayout mBuyLayout;
    /**
     * 位于顶部的购买布局
     */
    private LinearLayout mTopBuyLayout;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mScrollView=(MyScrollView) findViewById(R.id.scrollView);
        mScrollView.setOnScrollListener(this);
        
        mBuyLayout=(LinearLayout) findViewById(R.id.buy);
        mTopBuyLayout=(LinearLayout) findViewById(R.id.top_buy_layout);
        
        //监听子元素的状态  是否消失
        findViewById(R.id.parent_layout).getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            
            @Override
            public void onGlobalLayout() {
                onScroll(mScrollView.getScrollY());
            }
        });
    }
 
    @Override
    public void onScroll(int scrollY) {  //第一次进入应用也会触发onScroll()方法
        int mBuyLayout2ParentTop = Math.max(scrollY, mBuyLayout.getTop());//选取最大值
        mTopBuyLayout.layout(0, mBuyLayout2ParentTop, mTopBuyLayout.getWidth(), mBuyLayout2ParentTop + mTopBuyLayout.getHeight());
    }
    //原理:设置两个购买栏 , 第一个购买栏放在最顶头,第二个放置在正常位置, 一进入应用,首先会触发onScroll()方法,在方法里面判断
    //是否滑动到顶部,如果不是,则将顶头栏与正常栏重叠一起放置 
    //如果滑倒了顶部  ,则将顶部栏固定在原有位置
}
 
 

android仿美团客户端购买框悬浮特效的更多相关文章

  1. Android仿人人客户端(v5.7.1)——个人主页(三)

    转载请标明出处:http://blog.csdn.net/android_ls/article/details/9405089 声明:仿人人项目,所用所有图片资源都来源于其它Android移动应用,编 ...

  2. Android仿人人客户端(v5.7.1)——新鲜事之完整篇

    转载请标明出处: http://blog.csdn.net/android_ls/article/details/9228083       声明:仿人人项目,所用所有图片资源都来源于其它Androi ...

  3. Android 仿美团网,大众点评购买框悬浮效果之修改版

    转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17761431),请尊重他人的辛勤劳动成果,谢谢! 我之前写 ...

  4. Android仿美团地址选择

    最近做了这个功能,分享一下,用的是百度地图api,和美团外卖的地址选择界面差不多,也就是可以搜索或者滑动地图展示地址列表给用户选择,看下效果图先. 文章重点 1.展示地图并定位到“我”的位置2.滑动地 ...

  5. Android 仿美团网,探索使用ViewPager+GridView实现左右滑动查看更多分类的功能

    看下效果图,自己考虑下自己会如何实现,然后再继续看看作者的实现~ 不记得什么时候,我留意到到美团网首页有使用ViewPager+GridView实现左右滑动查看更多分类的一个功能,感觉它很有趣,于是想 ...

  6. android手机卫士、3D指南针、动画精选、仿bilibli客户端、身份证银行卡识别等源码

    Android精选源码 android身份证.银行卡号扫描源码 android仿bilibili客户端 android一款3D 指南针 源码 android手机卫士app源码 android提醒应用, ...

  7. android仿漫画源码、抽奖转盘、Google相册、动画源码等

    Android精选源码 android实现仿今日头条的开源项目 波浪效果,实现流量的动态显示 美妆领域的app, 集成了摄像头取色, 朋友圈, 滤镜等 android仿漫画源码 android一个视差 ...

  8. [Android] Android 手机下 仿 微信 客户端 界面 -- 微聊

    Android 手机下 仿 微信 客户端 界面 -- 微聊 (包括聊天列表 + 聊天对话页 + 朋友圈列表页 + 我的/发现 列表页) 项目演示: 功能说明: 1)底部标签切换 (TabHost + ...

  9. Android开发:仿美团下拉列表菜单,帮助类,复用简单

    近期在项目中须要用到下拉菜单.公司比較推崇美团的下拉菜单,于是要实现该功能.想着.这个功能应该是一个常常会用到的.于是何不写一个帮助类,仅仅要往这个类里面传入特定的參数,既能够实现下来菜单,并且还能够 ...

随机推荐

  1. window server IIS组建方法

    文章来自:二度云IIS(Internet Information Server,互联网信息服务)是一种Web(网页)服务组件,其中包括Web服务器.FTP服务器.NNTP服务器和SMTP服务器,分别用 ...

  2. PHP读取excel

    $file = '';//文件名称 $file_ext = explode('.',$file);$file_ext = $file_ext[1];$data['file_ext'] = $file_ ...

  3. 使用PHPExcel 读取 表格数据, 发现中文全变成 FALSE??

    出现这样的情况, 你可以看看你的表格是不是 CSV 格式的. 如果是, 那就赶紧另保存为 xls.xlsx 等格式的表格 . 因为 PHPExcel 对 Csv 的表格不感冒....

  4. Python知乎热门话题爬取

    本例子是参考崔老师的Python3网络爬虫开发实战写的 看网页界面: 热门话题都在 explore-feed feed-item的div里面 源码如下: import requests from py ...

  5. 『Python基础-12』各种推导式(列表推导式、字典推导式、集合推导式)

    # 『Python基础-12』各种推导式(列表推导式.字典推导式.集合推导式) 推导式comprehensions(又称解析式),是Python的一种独有特性.推导式是可以从一个数据序列构建另一个新的 ...

  6. linux mint系统 cinnamon桌面 发大镜功能

    让我来告诉迷途中的你cinnamon桌面一个好用的功能. 选择设置 选择窗口 -> 选择行为 看那个窗口移动和调整大小的特殊键 Alt 好了按住alt在滑动滑轮 世界不一样了 对于小屏幕高分辨率 ...

  7. Tomcat 8.5 基于 Apache Portable Runtime(APR)库性能优化

    Tomcat可以使用Apache Portable Runtime来提供卓越的性能及可扩展性,更好地与本地服务器技术的集成.Apache Portable Runtime是一个高度可移植的库,位于Ap ...

  8. Apache Tomcat 8.5 安全配置与高并发优化

    通常我们在生产环境中,Tomcat的默认配置显然不能满足我们的产品需求,所以很多时候都需要对Tomcat的配置进行调优,以下综合我自己的经验来配置 Tomcat 安全与优化情况,如果你有更好的方案,请 ...

  9. Windows Server 2008 R2 安装域

    在Windows Server 2008 R2里面安装域. 1.首先在"服务"里面添加"角色": 2.选择对应的域角色 3.安装完成后要启动配置向导 4.选择新 ...

  10. 谁说接口不能有代码?—— Kotlin接口简介(KAD 26)

    作者:Antonio Leiva 时间:Jun 6, 2017 原文链接:https://antonioleiva.com/interfaces-kotlin/ 与Java相比,Kotlin接口允许你 ...