
实现步骤如下:
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仿人人客户端(v5.7.1)——个人主页(三)
转载请标明出处:http://blog.csdn.net/android_ls/article/details/9405089 声明:仿人人项目,所用所有图片资源都来源于其它Android移动应用,编 ...
- Android仿人人客户端(v5.7.1)——新鲜事之完整篇
转载请标明出处: http://blog.csdn.net/android_ls/article/details/9228083 声明:仿人人项目,所用所有图片资源都来源于其它Androi ...
- Android 仿美团网,大众点评购买框悬浮效果之修改版
转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17761431),请尊重他人的辛勤劳动成果,谢谢! 我之前写 ...
- Android仿美团地址选择
最近做了这个功能,分享一下,用的是百度地图api,和美团外卖的地址选择界面差不多,也就是可以搜索或者滑动地图展示地址列表给用户选择,看下效果图先. 文章重点 1.展示地图并定位到“我”的位置2.滑动地 ...
- Android 仿美团网,探索使用ViewPager+GridView实现左右滑动查看更多分类的功能
看下效果图,自己考虑下自己会如何实现,然后再继续看看作者的实现~ 不记得什么时候,我留意到到美团网首页有使用ViewPager+GridView实现左右滑动查看更多分类的一个功能,感觉它很有趣,于是想 ...
- android手机卫士、3D指南针、动画精选、仿bilibli客户端、身份证银行卡识别等源码
Android精选源码 android身份证.银行卡号扫描源码 android仿bilibili客户端 android一款3D 指南针 源码 android手机卫士app源码 android提醒应用, ...
- android仿漫画源码、抽奖转盘、Google相册、动画源码等
Android精选源码 android实现仿今日头条的开源项目 波浪效果,实现流量的动态显示 美妆领域的app, 集成了摄像头取色, 朋友圈, 滤镜等 android仿漫画源码 android一个视差 ...
- [Android] Android 手机下 仿 微信 客户端 界面 -- 微聊
Android 手机下 仿 微信 客户端 界面 -- 微聊 (包括聊天列表 + 聊天对话页 + 朋友圈列表页 + 我的/发现 列表页) 项目演示: 功能说明: 1)底部标签切换 (TabHost + ...
- Android开发:仿美团下拉列表菜单,帮助类,复用简单
近期在项目中须要用到下拉菜单.公司比較推崇美团的下拉菜单,于是要实现该功能.想着.这个功能应该是一个常常会用到的.于是何不写一个帮助类,仅仅要往这个类里面传入特定的參数,既能够实现下来菜单,并且还能够 ...
随机推荐
- Canvas路径方向
使用Canvas路径画图需要注意方向,画图方向是顺时针还是逆时针需要记住.下面让我们看看Canvas常见路径方向. arc 参数值 context.arc(x,y,r,sAngle,eAngle,co ...
- thinkphp5 rbac权限
thinkphp 5 rbac权限 一 先创建一个数据库; 例如:创建一个test数据库;然后创建3个 表分别为:test_admin (管理员表), test_role,test_auth. 这个是 ...
- 让pandas的输出结果中显示全部数据
import pandas as pd pd.set_option('display.max_columns', 1000) pd.set_option('display.width', 1000) ...
- python 爬虫 5i5j房屋信息 获取并存储到数据库
from lxml import etree from selenium import webdriver import pymysql def Geturl(fullurl):#获取每个招聘网页的链 ...
- 搭建简单Django服务并通过HttpRequester实现GET/POST http请求提交表单
调试Django框架写的服务时,需要模拟客户端发送POST请求,然而浏览器只能模拟简单的GET请求(将参数写在url内),网上搜索得到了HttpRequester这一firefox插件,完美的实现了模 ...
- BZOJ1452_Count_KEY
题目传送门 二维树状数组,对于每个颜色开一个树状数组,用容斥求解. code: #include <cstdio> using namespace std; int read() { ') ...
- spring源码-BeanFactoryPostProcessor-3.2
一.BeanFactoryPostProcessor这个是spring容器的拓展之一,其目的是在容器初始化完成之前,通过beanFactory对上下文进行进行操作. 二.常用场景,需要对beanDef ...
- 更改steam的游戏库
用记事本打开steam/steamapps/libraryfolders.vdf,然后按照格式添加条目 "LibraryFolders"{ "TimeNextStatsR ...
- 使数据可供ArcGIS Server访问
内容来自ESRI官方文档(点击访问),简单总结如下: 1 ArcGIS Server用于发布服务的数据必须存储在服务器可以访问的位置: 2 这样的位置有三种类型: 本地路径:将数据本地存储在每台 Ar ...
- Python对象引用问题总结
对于对象引用问题,一直是一知半解的状态,现整理以备使用. 操作不可变对象进行加减运算时,会在内存中创建新的不可变实例,不会影响原来的引用>>> c=12>>> d= ...