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