简体   繁体   English

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

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

How Can I make this LinearLayout scroll?我怎样才能让这个 LinearLayout 滚动? I want these text views in LinearLayout to be over RecyclerView and hide up when scrolling.我希望 LinearLayout 中的这些文本视图在 RecyclerView 上方并在滚动时隐藏。 I've read that I will get it by adding the following app:layout_behavior="@string/appbar_scrolling_view_behavior"我读过我将通过添加以下app:layout_behavior="@string/appbar_scrolling_view_behavior"来获得它app:layout_behavior="@string/appbar_scrolling_view_behavior"

But it's not working.但它不起作用。

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

You can get a view's Y coordinate by using this as a reference:您可以使用它作为参考来获取视图的 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.

Now you can use your Scrollview's Y coordinate and see if it's gone past or has seen your reference point like so:现在您可以使用 Scrollview 的 Y 坐标,看看它是否已经过去或已经看到您的参考点,如下所示:

private ScrollView mYourScrollView;

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

Pls try your RecyclerView inside NestedScrollView请在 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.

相关问题 如何在Scroll的CoordinatorLayout中隐藏FloatingActionMenu? - How to hide FloatingActionMenu inside CoordinatorLayout on Scroll? 如何在coordinatorLayout内的RelativeLayout底部对齐LinearLayout - how to align LinearLayout at the bottom of RelativeLayout inside coordinatorLayout 如何使用CoordinatorLayout和LinearLayout隐藏和显示App Bar,而不是recyclelerview - How to hide and show App Bar with CoordinatorLayout and LinearLayout, NOT recyclerview 使用CoordinatorLayout滚动隐藏工具栏 - Hide Toolbar on scroll with CoordinatorLayout 如何在CoordinatorLayout内部的工具栏上触发滚动行为 - How to Trigger a Scroll Behavior on a Toolbar inside a CoordinatorLayout 如何在片段中为LinearLayout使用CoordinatorLayout? - How to use CoordinatorLayout for LinearLayout in fragment? 在 webview 滚动上隐藏线性布局 - Hide linearlayout on webview scroll 在其中滚动 CoordinatorLayout 和 AppBarLayout 的顶部 - Scroll on top of the CoordinatorLayout and AppBarLayout inside it Android CoordinatorLayout中的Listview如何使其可滚动? - Android Listview inside CoordinatorLayout how to make it scroll-able? 使用AppBarLayout在CoordinatorLayout中滚动显示/隐藏BottomNavigationView - Show/hide BottomNavigationView on scroll in CoordinatorLayout with AppBarLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM