【发布时间】:2021-06-14 08:03:44
【问题描述】:
你好,我试图在 CoordinatorLayout 中使用下面的布局,但它不像 relativelayout 那样工作,我试过 this solution 但对我没有用
在我的布局中,我有三个主要元素,一个是工具栏,然后是水平回收器视图,然后是垂直回收器视图,水平的在垂直滚动时应该隐藏在相对布局中存在滚动不平滑的问题但是在实现 CoordinatorLayout 之后,滚动是平滑的,但是现在水平显示在垂直和顶部的后面
编辑
我已经尝试了一个有效的答案,但仍然存在一个问题 垂直 recyclerview 的滚动并不顺利,这里是 Here is the screen recording for refrence
编辑 2
好的,我问的问题是关于 CoordinatorLayout 但我面临很多 与它有关的问题,因为将来我可能会在其中包含更多观点 布局,这对我来说可能很麻烦,所以现在我更换了 CoordinatorLayout 与 relativelayout 但有一个小问题 几乎相同的垂直recyclerview的滚动动画 我在视频中显示的微小变化现在有一个文本视图 在水平的上方
这里是代码
<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"
android:background="@color/black"
tools:ignore="ExtraText">
<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>
<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: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" />
<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmerEffect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/appBarLayout">
<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>
recyclerview 的 Java 代码
// this is for one item per scroll Vertical RecyclerView
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(verticalRecyclerView);
// TextView
view_all.setOnClickListener(v -> requireActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, Search_Fragment.class, null)
.addToBackStack(String.valueOf(new Home_Fragment()))
.commit());
// Method For Hiding Horizontal RecyclerView When Vertical RecyclerView Scrolls
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);
}
}
}
});
【问题讨论】:
-
请不要在您的问题标题中添加
[Android],这就是标签的用途
标签: android android-recyclerview android-coordinatorlayout