转载于:Android 在ScrollView中的导航栏悬浮吸顶方法实现_萧萧风的博客-CSDN博客_android 悬浮吸顶x
效果:
实现原理:
往上滑动scrollview时:
当导航栏距屏幕顶部的距离小于等于标题栏的高度时悬浮,即上滑到紧贴标题栏时悬浮(移除原来位置的导航栏,在需要悬浮的位置添加之前移除的导航栏)。
反之:
(导航栏原来的位置)下滑到距离大于标题栏的高度时将悬浮的导航栏放回原来的位置。
实现步骤:
1、首先是布局实现,在内容布局(标题下面的容器)中,最外层使用FrameLayout包裹住ScrollView(自定义ScrollView,写一个接口监听它的onScrollChanged方法)控件,和一个空的LinearLayout控件即悬浮控件(用于动态将ScrollView控件中的导航栏添加到里面)。
(注:此处博主直接使用原生ScrollView,并调用view.setOnScrollChangeListener也可以实现该效果)
xml布局,大致酱紫,不重要的view都已省略,重点看注释,看注释,看注释:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--此处省略标题栏-->
<!--内容布局最外层使用FrameLayout-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--下面是自定义scrollview-->
<MyScrollView
android:id="@+id/myScrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:focusable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<!--此处省略头部view-->
<!--下面LinearLayout用于包裹导航栏控件-->
<LinearLayout
android:id="@+id/line_add_navigation_fixation"
android:layout_width="match_parent"
android:layout_height="填写固定的导航栏高度"
android:background="@color/white"
android:orientation="vertical">
<!--此处省略导航栏view-->
</LinearLayout>
<ViewPager
android:id="@+id/viewPage_device"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</MyScrollView>
<!--下面LinearLayout用于占位-->
<LinearLayout
android:id="@+id/line_add_navigation_suspension"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
2、然后是java代码
主要代码,重点还是看注释:
//scrollview的滑动监听
@Override
public void onScrollChanged(MyScrollView ceshimy, int scrollX, int scrollY, int oldl, int oldt) {
int[] location = new int[2];
lineAddNavigationFixation.getLocationOnScreen(location);
//lineAddNavigationFixation是包裹导航栏的LinearLayout
int y = location[1];//导航栏距屏幕顶部的距离,会随着scrollview的滑动而改变
if (y <= titleView.getHeight()) {//导航栏距屏幕顶部的距离小于等于标题栏的高度时 悬浮,即上滑到紧贴标题栏时 悬浮
addNavigationSuspension();
} else {//反之下滑到距离大于标题栏的高度时放回原来的位置
addNavigationFixation();
}
}
private void addNavigationSuspension() {//添加悬浮
//lineAddNavigationSuspension是布局中的占位LinearLayout
if (lineAddNavigationSuspension.getChildCount() == 0) {
if (navigationDevice.getParent() != null)
((ViewGroup) navigationDevice.getParent()).removeView(navigationDevice);
lineAddNavigationSuspension.addView(navigationDevice);
}
}
private void addNavigationFixation() {//悬浮固定到原位置
if (lineAddNavigationFixation.getChildCount() == 0) {
// L.e("---" + "固定");
if (navigationDevice.getParent() != null)
((ViewGroup) navigationDevice.getParent()).removeView(navigationDevice);
lineAddNavigationFixation.addView(navigationDevice);
}
}
————————————————
版权声明:本文为CSDN博主「萧萧风」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xxfen_/article/details/88549895