简体   繁体   中英

How to hide LinearLayout inside CoordinatorLayout on scroll?

How Can I make this LinearLayout scroll? I want these text views in LinearLayout to be over RecyclerView and hide up when scrolling. I've read that I will get it by adding the following app:layout_behavior="@string/appbar_scrolling_view_behavior"

But it's not working.

XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    android:background="@drawable/gradientbackground"
    android:screenOrientation="portrait" >

    <androidx.recyclerview.widget.RecyclerView
        android:layout_marginTop="100dp"
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="text2" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="text2" />
        </LinearLayout>


    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/bottom_navigation_background"
        android:elevation="0dp"
        android:outlineProvider="none"
        app:itemIconTint="@color/bottom_navigation_colors"
        app:labelVisibilityMode="selected"
        app:layout_anchor="@+id/recyclerview"
        app:layout_anchorGravity="bottom|center"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        app:layout_scrollFlags="scroll|enterAlways"
        app:menu="@menu/bottom_nav_menu" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

You can try to set a coordinate reference and hide LinearLayout when you scroll past or over that particular reference point.

You can get a view's Y coordinate by using this as a reference:

private View mIamYourView;

mIamYourView.getY(); // You can opt this as int, since it's what's going to be needed, I think this returns a double or a float.

Now you can use your Scrollview's Y coordinate and see if it's gone past or has seen your reference point like so:

private ScrollView mYourScrollView;

if(mScrollView.getScrollY() >= mIamYourView.getY()){
 mIamYourview.setVisiblity(VIEW.GONE);
}

Pls try your RecyclerView inside NestedScrollView

    <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    android:background="@drawable/gradientbackground"
    android:screenOrientation="portrait" >



    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_marginTop="100dp"
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="text2" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="text2" />
        </LinearLayout>


    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/bottom_navigation_background"
        android:elevation="0dp"
        android:outlineProvider="none"
        app:itemIconTint="@color/bottom_navigation_colors"
        app:labelVisibilityMode="selected"
        app:layout_anchor="@+id/recyclerview"
        app:layout_anchorGravity="bottom|center"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        app:layout_scrollFlags="scroll|enterAlways"
        app:menu="@menu/bottom_nav_menu" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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