繁体   English   中英

如何在滚动时隐藏 CoordinatorLayout 内的 LinearLayout?

[英]How to hide LinearLayout inside CoordinatorLayout on scroll?

我怎样才能让这个 LinearLayout 滚动? 我希望 LinearLayout 中的这些文本视图在 RecyclerView 上方并在滚动时隐藏。 我读过我将通过添加以下app:layout_behavior="@string/appbar_scrolling_view_behavior"来获得它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>

您可以尝试设置坐标参考并在滚动经过或经过该特定参考点时隐藏 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);
}

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

暂无
暂无

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

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