【问题标题】:how to hide a recyclerview when another scrolls当另一个滚动时如何隐藏recyclerview
【发布时间】:2021-08-29 00:43:15
【问题描述】:

你好我正在尝试在一个布局中实现 2 个回收器视图,一个在布局顶部是水平的,下面是第二个是垂直的,我想要的是当垂直回收器视图滚动时水平将保持隐藏直到垂直回到起始位置

这是我尝试过的一个解决方案,但有一个问题是动画有点奇怪,第一个滚动是滞后的(所以我找不到一个好的词来描述它),在滚动第一个项目之后,它又恢复正常并且平滑,并且在滚动第一项后,如果滚动回顶部,它只会完全滚动到顶部直到第一项

这里是问题的Screen Recording

 verticalRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull @NotNull RecyclerView recyclerView, int dx, int dy) {
                if (recyclerView.canScrollVertically(-1)) {
                    horizontalRecyclerView.setVisibility(View.GONE);
                    view_all.setVisibility(View.GONE);
                } else {
                    if (!(horizontalRecyclerView.getVisibility() == View.VISIBLE)) {
                        horizontalRecyclerView.setVisibility(View.VISIBLE);
                        view_all.setVisibility(View.VISIBLE);


                    }
                }
            }
        });

这是我的代码

fragment_home.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar" />
    </com.google.android.material.appbar.AppBarLayout>

    <TextView
        android:id="@+id/view_all_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appBarLayout"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="20dp"
        android:layout_marginTop="10dp"
        android:text="@string/view_all"
        android:textColor="@color/white"
        android:textStyle="bold" />

    <!--    Horizontal RecyclerView-->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/postRecyclerView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/view_all_text"
        android:background="@color/black"
        android:orientation="horizontal"
        android:overScrollMode="never"
        app:reverseLayout="true" />
    <!--    Vertical RecyclerView-->
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewHome"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/postRecyclerView1"
        android:layout_marginBottom="10dp"
        android:orientation="vertical"
        android:overScrollMode="never"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />

    <com.facebook.shimmer.ShimmerFrameLayout
        android:id="@+id/shimmerEffect"
        android:layout_below="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <include layout="@layout/post_item_container_shimmer_home" />

        <include layout="@layout/post_item_container_shimmer_home" />


    </com.facebook.shimmer.ShimmerFrameLayout>
</RelativeLayout>

post_item_container_home.xml

<RelativeLayout 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="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/Card_View"
        android:layout_width="match_parent"
        android:layout_height="500dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="5dp"
        app:shapeAppearanceOverlay="@style/RoundedCornerHome"
        tools:ignore="ObsoleteLayoutParam">

        <com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/imagePostHome"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:adjustViewBounds="true"
            android:contentDescription="@string/todo"
            app:layout_constraintDimensionRatio="H,16:9"
            app:shapeAppearanceOverlay="@style/RoundedCornerHome" />

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_below="@+id/Card_View"
        android:layout_marginStart="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="5dp"
        android:layout_marginBottom="10dp"
        android:background="@color/grey"
        app:shapeAppearanceOverlay="@style/RoundedCornerHome">


    </com.google.android.material.card.MaterialCardView>


</RelativeLayout>

【问题讨论】:

    标签: android xml android-layout android-recyclerview


    【解决方案1】:
    postRecyclerView1.Visiblity=View.gone
    recyclerViewHome.Visiblity=View.visible
    

    如果您想显示回收站视图,请使用上面的代码

    下面还有用

    postRecyclerView1.Visiblity=View.visible
    
    recyclerViewHome.Visiblity=View.gone
    

    【讨论】:

    • 好的,但是当滚动发生时如何实现它
    【解决方案2】:

    在这里,您将 onScrollListener 添加到您的垂直回收器视图并覆盖方法 onScrolled()。更多信息可以去doc。 帮助您的示例实现:

    verticalRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull @NotNull RecyclerView recyclerView, int dx, int dy) {
                //set the visibility of honrizontalRecyclerView base on whether we can scroll upwards (-1 indicates upwards)
               if(recyclerView.canScrollVertically(-1)) 
                 honrizontalRecyclerView.setVisibility(Visibility.GONE);
               else 
               {
                 if(!honrizontalRecyclerView.isVisible())
                   honrizontalRecyclerView.setVisibility(Visibility.VISIBLE);
               }
            }
        });
    

    【讨论】:

    • 哦,这是我的错误,我已经编辑发布
    • 它有多滞后?每个项目包含什么信息?添加我的答案之前是否会发生这种情况?
    • 应该是因为我们过于频繁地检查和设置可见性(在滚动时)。尝试添加 if 条件来检查水平视图的可见性,并将其设置为仅在不可见时可见
    • 请看我的更新代码sn-p。你这样做了吗?
    • 对不起,但我没有像你说的那样滞后。我们这里没有使用任何动画,所以效果不太好
    猜你喜欢
    • 1970-01-01
    • 2016-06-17
    • 2013-09-06
    • 2021-09-30
    • 1970-01-01
    • 2018-02-20
    • 2019-01-21
    • 1970-01-01
    • 2015-03-26
    相关资源
    最近更新 更多