滚动recyclelerview时如何向上或向下移动按钮

时间:2016-12-21 08:30:49

标签: android android-recyclerview android-coordinatorlayout

我使用过Cordinator布局但是当我滚动回收器时,我无法向上或向下滚动我的按钮。

任何人都有经验,请帮助我。感谢。

这是我的XML。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
            </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_scrollFlags="scroll|enterAlways|snap">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Test"/>
    </android.support.design.widget.CoordinatorLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:id="@+id/re_pro"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>

</android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:0)

使用此代码,希望此帮助

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
            </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_scrollFlags="scroll|enterAlways|snap">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Test"/>
    </android.support.design.widget.CoordinatorLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:id="@+id/re_pro"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>

<android.support.design.widget.FloatingActionButton
                android:id="@+id/fabCreateDefect"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentEnd="true"
                android:layout_gravity="bottom|end"
                android:src="@drawable/plus_white"
                app:backgroundTint="@color/toolbar_blue" />

</android.support.design.widget.CoordinatorLayout>

浮动操作按钮

FloatingActionButton fabCreateDefect = (FloatingActionButton) mView.findViewById(R.id.fabCreateDefect);


recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                if (dy > 0) //check for scroll down
                {
                   //Scroll button down   
                  fabCreateDefect.hide();


                } else {
                   //Scroll button up
                   fabCreateDefect.show();
                }
         }
    });