在上一篇中,已经介绍了悬浮框停留在 顶部 , 如还没有看过的,请可以先看看 仿大众、美团悬浮框顶部悬停,因为本文是在其基础上稍加,修改而来:
首先修改一下 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/parent_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:textColor="#fcfbfb"
android:background="#00998c"
android:text="浮动在顶部"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<come.huitong.fei.testapplication.MyScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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="300dp"
android:background="#996699"
android:scaleType="centerCrop"/>
<ImageView
android:id="@+id/buy"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@mipmap/buy" />
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#006699"
android:scaleType="centerCrop"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="360dp"
android:background="#00990f"
android:scaleType="centerCrop"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#99008c"
android:scaleType="centerCrop"/>
</LinearLayout>
</come.huitong.fei.testapplication.MyScrollView>
<ImageView
android:id="@+id/top_buy_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@mipmap/buy"
android:visibility="gone"/>
</FrameLayout>
</LinearLayout>
然后,在主Activity中 修改一下onScroll 方法 :
/**
* 注意: mBuyLayout 的 父控件,是 MyScrollView (因为 getTop() 获取的 是 到 父控件的 顶端的 距离)
* @param scrollY
*/
@Override
public void onScroll(int scrollY) {
Log.i("距离", "scrollY==" + scrollY + " mBuyLayout.getTop()" + mBuyLayout.getTop()+
" mBuyLayout.getTop() "+ mBuyLayout.getTop());
if(scrollY < mBuyLayout.getTop()){
mTopBuyLayout.setVisibility(View.GONE);
}else{
mTopBuyLayout.setVisibility(View.VISIBLE);
}
}
至此,就完成了 页面向上滑动时,底部出现停留的 效果 。 本篇都是在 上一篇 (都很简单)的基础上完成的。
同时,像这种 浮动框 在上面 或是 在下面 ,原理都一样,这样实现起来,也简单方便,好理解。