如何在滚动时在CoordinatorLayout中隐藏LinearLayout?

问题描述 投票:0回答:2

如何使此LinearLayout滚动?我希望LinearLayout中的这些文本视图超过RecyclerView并在滚动时隐藏。我读过我将通过添加以下内容app:layout_behavior="@string/appbar_scrolling_view_behavior"

来获得它

但是它不起作用。

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>
android android-coordinatorlayout
2个回答
0
投票

您可以尝试设置坐标参考并在滚动到特定参考点之上或上方时隐藏LinearLayout。

您可以以此为参考来获取视图的Y坐标:

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.

现在,您可以使用Scrollview的Y坐标,看它是否已过去或是否已看到参考点,如下所示:

private ScrollView mYourScrollView;

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

0
投票

请在NestedScrollView中尝试您的RecyclerView

    <?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>
© www.soinside.com 2019 - 2024. All rights reserved.