java - 在 ScrollView android中滚动时 float 操作按钮隐藏和显示?

标签 java android xml

我有一个 nestedscrollview,其中包含一些 RelativeLayoutCardViewstextviews 等内容。由于某些原因,我也使用 floatingactionbutton 库。所以我不能为此使用任何行为。我不知道应该如何处理 scrollview 中的 scrollchangelistener 来像行为一样动态隐藏和显示 fab。 现在,当程序或输出覆盖整页 Intent 时,晶圆厂就完全消失了。可能是内容的后面。我无法访问 fab。我想要 FloatingActionButton 位于 scrollview 之上。或者显示和隐藏 fab 也是需要的。请告诉我如何实现这个概念?

关于如何在滚动时隐藏和显示 fab 有什么建议吗?

<ScrollView 
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scrollbars="none"
android:background="#fff"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android">


<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".programs.PrintANumber">

    <android.support.v7.widget.CardView
        android:id="@+id/program_title_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        app:cardElevation="2dp"
        android:layout_below="@id/definition_content"
        android:layout_centerHorizontal="true"
        app:contentPadding="15dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Print a Number"
            android:textColor="@color/colorPrimaryDark"
            android:textAlignment="center"
            android:textSize="20sp"
            android:textStyle="bold"/>
    </android.support.v7.widget.CardView>


    <android.support.v7.widget.CardView
        android:id="@+id/sourcecode_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        app:cardElevation="2dp"
        android:layout_below="@id/program_title_card"
        android:layout_centerHorizontal="true"
        app:contentPadding="6dp">

        <thereisnospon.codeview.CodeView
            android:id="@+id/program"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </thereisnospon.codeview.CodeView>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/output_title_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        app:cardElevation="2dp"
        android:layout_below="@id/sourcecode_card"
        android:layout_centerHorizontal="true"
        app:contentPadding="15dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Output"
            android:textColor="@color/colorPrimaryDark"
            android:textAlignment="center"
            android:textSize="20sp"
            android:textStyle="bold"/>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/output_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="6dp"
        app:cardElevation="2dp"
        android:layout_below="@id/output_title_card"
        android:layout_centerHorizontal="true"
        app:contentPadding="6dp">

        <thereisnospon.codeview.CodeView
            android:id="@+id/output"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </thereisnospon.codeview.CodeView>
    </android.support.v7.widget.CardView>

    <com.github.clans.fab.FloatingActionMenu
        android:id="@+id/menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom|end"
        android:paddingRight="10dp"
        android:paddingBottom="10dp"
        android:paddingLeft="10dp"
        fab:menu_labels_ellipsize="end"
        fab:menu_labels_singleLine="true"
        app:menu_icon="@drawable/fab_add"
        fab:menu_backgroundColor="#ccffffff"
        app:menu_colorNormal="@color/lightGrey"
        android:elevation="2dp">

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/id_opt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/opt1"
            android:padding="5dp"
            fab:fab_size="mini"
            fab:fab_label="Option1"
            app:rippleColor="@color/colorAccent"
            app:fab_colorNormal="@color/colorAccent" />

        <com.github.clans.fab.FloatingActionButton
            android:id="@+id/id_opt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/opt2"
            android:padding="5dp"
            fab:fab_size="mini"
            fab:fab_label="Option2"
            app:rippleColor="@color/colorAccent"
            app:fab_colorNormal="@color/colorAccent" />

    </com.github.clans.fab.FloatingActionMenu>

</RelativeLayout>

最佳答案

尝试这样。

private int oldScrollYPostion = 0; // inside your class

mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
    @Override
    public void onScrollChanged() {
        if (mScrollView.getScrollY() > oldScrollYPostion) {
            fab.hide();
        } else if (mScrollView.getScrollY() < oldScrollYPostion || mScrollView.getScrollY() <= 0) {
            fab.show();
        }
        oldScrollYPostion = mScrollView.getScrollY();
    }
});

关于java - 在 ScrollView android中滚动时 float 操作按钮隐藏和显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57801487/

相关文章:

java - 覆盖默认的 org.springframework.amqp.rabbit.connection.CachingConnectionFactory 实现

java - Maven - 在运行时查找依赖项

java - 是否存在线程安全的 statsd 客户端?

android - 仅使部分文本在 Android 中可编辑

java - 为什么 LiveData setValue 或 PostValue 在 View 中只触发一次 onChange?

android - Quckblox q-municate 视频聊天 'Token Required'

xml - RXML 模板生成器的 Rails 3.1 替代品

java - Spring IO 这个名字是什么意思?

c - libxml2 所有 xml 转 char

sql - T-SQL解析XML赋予空白值