首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

当浮动按钮到达NestedScrollView内TextView的相同高度位置时,如何将浮动按钮的可见性设置为GONE?

要将浮动按钮的可见性设置为GONE,可以通过监听NestedScrollView的滚动事件,当滚动到指定位置时,修改浮动按钮的可见性。

首先,需要在布局文件中定义NestedScrollView和浮动按钮:

代码语言:txt
复制
<androidx.core.widget.NestedScrollView
    android:id="@+id/nestedScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这里是文本内容" />

</androidx.core.widget.NestedScrollView>

<Button
    android:id="@+id/floatingButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:visibility="visible"
    android:text="浮动按钮" />

然后,在代码中找到NestedScrollView和浮动按钮,并设置滚动监听:

代码语言:txt
复制
NestedScrollView nestedScrollView = findViewById(R.id.nestedScrollView);
final Button floatingButton = findViewById(R.id.floatingButton);

nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
    @Override
    public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
        // 获取TextView的位置
        TextView textView = findViewById(R.id.textView);
        int[] textViewLocation = new int[2];
        textView.getLocationOnScreen(textViewLocation);
        int textViewTop = textViewLocation[1];

        // 获取浮动按钮的位置
        int[] floatingButtonLocation = new int[2];
        floatingButton.getLocationOnScreen(floatingButtonLocation);
        int floatingButtonTop = floatingButtonLocation[1];

        // 判断浮动按钮是否到达TextView的位置
        if (floatingButtonTop >= textViewTop) {
            floatingButton.setVisibility(View.GONE);
        } else {
            floatingButton.setVisibility(View.VISIBLE);
        }
    }
});

这样,当NestedScrollView滚动到TextView的位置时,浮动按钮的可见性就会被设置为GONE。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

Android开发笔记(一百三十五)应用栏布局AppBarLayout

Android5.0推出工具栏Toolbar用来替代ActionBar,灵活性和易用性大大增强,有关Toolbar的详细介绍参见《Android开发笔记(一百一十九)工具栏Toolbar》。 可是仅仅使用Toolbar的话,还是有些呆板,比如说Toolbar固定占据着页面顶端,既不能跟着主体页面移上去,也不会跟着主体页面拉下来。为了让App页面更加生动活泼,势必要求Toolbar在某些特定的场景上移或者下拉,如此才能满足酷炫的页面特效需要。那么Android5.0也同时给出了相应的解决方案,即推出MaterialDesign库,通过该库中的AppBarLayout控件,对Toolbar加以包装,从而实现顶部工具栏的动态变化效果。 AppBarLayout其实继承自LinearLayout,所以具备LinearLayout的所有属性与方法。对于大家关心的额外功能,则主要有以下几点: 1、支持响应主体页面的滑动行为,即在主体页面上移或者下拉时,AppBarLayout能够捕捉到主体页面的滚动操作; 2、AppBarLayout捕捉到滚动操作之后,还要通知头部控件(通常是Toolbar),告诉头部控件你要怎么滚,是爱咋咋滚,还是满大街滚; 具体到实现上,要在工程中做以下修改: 1、添加几个库的支持,包括appcompat-v7库(Toolbar需要)、design库(AppBarLayout需要)、recyclerview库(主页面的RecyclerView需要); 2、布局文件的根布局采用android.support.design.widget.CoordinatorLayout,因为design库的动态效果都依赖于该控件; 3、CoordinatorLayout节点要添加命名空间声明xmlns:app="http://schemas.android.com/apk/res-auto"; 4、使用android.support.design.widget.AppBarLayout节点包裹Toobar; 5、Toobar节点添加滚动属性app:layout_scrollFlags="scroll|enterAlways",声明工具栏的滚动行为标志; 6、演示页面的主体页面使用RecyclerView控件,并给该控件节点添加行为属性app:layout_behavior="@string/appbar_scrolling_view_behavior",表示通知AppBarLayout捕捉RecyclerView的滚动操作。 下面是AppBarLayout结合RecyclerView实现的工具栏向上滚动效果截图:

04
领券