【问题标题】:How to use layout_below in CoordinatorLayout如何在 CoordinatorLayout 中使用 layout_below
【发布时间】: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


【解决方案1】:

Coordinator 布局类似于 FrameLayout。 RelativeLayout 的属性对它不起作用。

将此行添加到您的 AppBarLayout

app:layout_behavior="com.google.android.material.appbar.AppBarLayout$Behavior"

添加如下所示的 RelativeLayout 并将视图放入其中并使用 RelativeLayout 的属性

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
    
    </RelativeLayout>

【讨论】:

  • 好的,但同样的问题出现了,当我使用相对布局时,滚动并不平滑
【解决方案2】:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="@android:color/black">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

    <!-- NESTED SCROLLVIEW -->
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/appBarLayout">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/view_all_text"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="TOP TEXTVIEW"
                android:textColor="@color/white"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <!--    Horizontal RecyclerView-->
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/postRecyclerView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:overScrollMode="never"
                app:layout_constraintTop_toBottomOf="@id/view_all_text"
                app:reverseLayout="true" />

            <!--    Vertical RecyclerView-->
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewHome"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:orientation="vertical"
                android:overScrollMode="never"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintTop_toBottomOf="@id/postRecyclerView1" />

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

                <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>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 可以正常工作,但是在滚动中存在一些小问题,垂直回收视图的滚动并不平滑,如果可能的话,你能帮我解决一下吗
  • 请检查我用您提供的代码更新的问题以及我正在谈论的问题的屏幕录像
  • @VasantRaval,我看过屏幕录像,但无法理解您的问题。也许你在课堂上提到了一些东西。你能再描述一下吗?
  • @VasantRaval,如果我已经在CollapsingToolbar 中添加了它,你为什么要将水平RecyclerViewvisibility 设置在垂直addOnScrollListenerRecyclerView?如果您这样做,则没有必要使用CollapsingToolbar。我认为你不应该使用addOnScrollListener
  • 好吧,抱歉回复晚了,我已经尝试添加 CoordinatorLayout,但我遇到了很多问题,因为将来我可能会在布局中包含更多视图,这对我来说可能很麻烦,所以现在我用 relativelayout 替换了 CoordinatorLayout,但是垂直 recyclerview 的滚动动画有一个小问题,这与我在视频中显示的几乎相同,微小的变化是现在水平视图上方有一个文本视图,我已经更新了代码xml的你会更好理解,再次抱歉回复晚了
猜你喜欢
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2015-12-30
  • 1970-01-01
相关资源
最近更新 更多