实现步骤如下:
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. webpack 优化代码 让代码加载速度更快

    一,如何优化webpack构建 (1),缩小文件搜索范围, 优化Loader配置 module.exports = { module: { rules: [ { test:/\.js$/, use:[ ...

  2. Throwable类

    1.Throwable是所有异常的基类(父类),两个子类Error和Exception ①Error:java运行时系统的内部错误或资源耗尽错误,应用程序不应该抛出这种类型的对象,一旦发生这种异常除了 ...

  3. 【Spark】Spark核心之弹性分布式数据集RDD

    1. RDD概述 1.1 什么是RDD (1) RDD(Resilient Distributed Dataset)弹性分布式数据集,它是Spark的基本数据抽象,它代表一个不可变.可分区.里面的元素 ...

  4. thinkphp 5.1/tp5.1 route路由bug

    tp5.1下面RuleItem类中,match方法. 如果同一个控制器下面,写了两个路由,后一个路由比包含前一个路由,则访问后一个路由地址的时候,会跳转到前面定义的那个路由

  5. Zookeeper -- 关于Zookeeper

    Zookeeper是什么? 分布式协调框架 Zookeeper中文件呈树形结构,树形结构下包含多个节点,称为Znode:zk中节点存储数据不超过1M,指得是Znode中存储数据不超过1M Zookee ...

  6. win10安装kali组双系统

    一.镜像下载: 根据需求下载自己需要的版本 从官网下载kali 2018.2 的安装包:https://www.kali.org/downloads/ 二.烧录: 这里推荐用 win32 disk i ...

  7. python Tkinter 的 Text 保持焦点在行尾

    https://bbs.csdn.net/topics/390712532 text.see(END)

  8. [SGU223]Little Kings(状压DP)

    随便DP一下 Code #include <cstdio> int sta[150],cnt[150],tp,n,k; long long dp[12][144][150],Ans; in ...

  9. 线上CPU飚高(死循环,死锁...)

    之前排除服务器内存暴增的问题,在此看到一篇类似的文章,做个类似的记录. 1.top基本使用 top 命令运行图: 第一行:基本信息 第二行:任务信息 第三行:CPU使用情况 第四行:物理内存使用情况 ...

  10. linux如何制作程序桌面快捷方式

    1.生成通过apt或者dpkg安装的程序的桌面快捷方式 他们的快捷方式在/usr/share/applications中,比如我们要生成火狐的桌面快捷方式,执行下列命令 cp /usr/share/a ...