简体   繁体   English

浮动动作按钮在scrollview android中滚动时隐藏并显示?

[英]Floating Action Button hide and show when scroll in scrollview android?

I have a nestedscrollview with content like some RelativeLayout , CardViews and textviews . 我有一个nestedscrollview ,内容类似于一些RelativeLayoutCardViewstextviews I am using a floatingactionbutton library for some reasons, as well. 由于某些原因,我也在使用floatingactionbutton库。 So I can't use any behavior for it. 所以我不能使用任何行为。 I don't know how I should handle the scrollchangelistener from scrollview to hide and show the fab dynamically like a behavior. 我不知道我应该如何处理scrollchangelistenerscrollview来像行为一样动态隐藏和显示fab。 Now, When programs or output cover full page of intent, the fab is totally disappeared. 现在,当程序或输出覆盖整个意图页面时,晶圆厂完全消失了。 It may be behind the content. 它可能在内容后面。 I cant access fab. 我无法访问工厂。 I want the FloatingActionButton over the scrollview . 我想要FloatingActionButtonscrollview Or show and hide the fab is also need. 或显示和隐藏晶圆厂也是需要的。 Please tell how to achieve that concept? 请告诉我如何实现这个概念?

Any suggestions how to hide and show the fab while scrolling? 有什么建议如何在滚动时隐藏和显示晶圆厂?

<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>

You are using Floating action inside the scroll view tag. 您正在滚动视图标记内使用浮动动作。 take it out of scroll view. 将其移出滚动视图。

Use something like this: 使用这样的东西:

<RelativeLayout
        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">

<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>
<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>

</RelativeLayout>
</ScrollView>
</RelativeLayout>

Try like this. 尝试这样。

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();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM