你好,我正在尝试在一个布局中实现 2 个回收器视图,一个在布局顶部是水平的,第二个是垂直的,我想要的是当垂直回收器视图滚动时,水平将保持隐藏,直到垂直回到起始位置
这是代码
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
</com.google.android.material.appbar.AppBarLayout>
// i tried to hide this recycler view with nested scroll but doesn't worked
<androidx.core.widget.NestedScrollView
android:id="@+id/snipet_list_Recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/appBarLayout"
android:overScrollMode="never">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
// the recyclerview i want to hide when the other scroll
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/postRecyclerView1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="horizontal"
android:overScrollMode="never"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:reverseLayout="true"
tools:ignore="MissingConstraints" />
<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmerEffect1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<include layout="@layout/snipet_list_top_10_home_shimmer" />
<include layout="@layout/snipet_list_top_10_home_shimmer" />
<include layout="@layout/snipet_list_top_10_home_shimmer" />
<include layout="@layout/snipet_list_top_10_home_shimmer" />
<include layout="@layout/snipet_list_top_10_home_shimmer" />
<include layout="@layout/snipet_list_top_10_home_shimmer" />
<include layout="@layout/snipet_list_top_10_home_shimmer" />
</LinearLayout>
</com.facebook.shimmer.ShimmerFrameLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
// vertical recyclerview
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerViewHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/snipet_list_Recyclerview"
android:layout_marginTop="0dp"
android:layout_marginBottom="10dp"
android:orientation="vertical"
android:overScrollMode="never"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmerEffect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/snipet_list_Recyclerview">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
<include layout="@layout/post_item_container_shimmer_home" />
</LinearLayout>
</com.facebook.shimmer.ShimmerFrameLayout>
</RelativeLayout>
爪哇
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(verticalRecyclerView);
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))
horizontalRecyclerView.setVisibility(View.GONE);
else {
if (!(horizontalRecyclerView.getVisibility() == View.VISIBLE)) {
horizontalRecyclerView.setVisibility(View.VISIBLE);
}
}
}
});
在这里,您将 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);
}
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句