【问题标题】:Floating Action Button hide and show when scroll in scrollview android?在滚动视图android中滚动时浮动动作按钮隐藏和显示?
【发布时间】:2019-09-05 08:34:19
【问题描述】:

我有一个nestedscrollview,其内容类似于RelativeLayoutCardViewstextviews。由于某些原因,我也在使用floatingactionbutton 库。所以我不能使用任何行为。我不知道我应该如何处理来自scrollviewscrollchangelistener 来像行为一样动态地隐藏和显示工厂。 现在,当程序或输出覆盖整页意图时,工厂完全消失了。它可能在内容的背后。我无法访问工厂。我想要FloatingActionButton 而不是scrollview。或者显示和隐藏工厂也是需要的。请告诉如何实现这个概念?

关于如何在滚动时隐藏和显示 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>

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    像这样试试。

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

    【讨论】:

      【解决方案2】:

      您正在滚动视图标签内使用浮动操作。将其从滚动视图中移除。

      使用这样的东西:

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

      【讨论】:

      • @Kavin 如果有帮助,请接受答案并投票。谢谢
      猜你喜欢
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多