你好,我正在尝试在一个布局中实现 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>
答案 0 :(得分:2)
postRecyclerView1.Visiblity=View.gone
recyclerViewHome.Visiblity=View.visible
如果您想显示回收者视图主页,请使用上面的代码
其他在下面使用
postRecyclerView1.Visiblity=View.visible
recyclerViewHome.Visiblity=View.gone
答案 1 :(得分:1)
在这里,您将 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);
}
}
});