CollapsingToolbarLayout inside parent fragment and child fragment tablayout with recyclerView are not scrolling together

huangapple go评论62阅读模式
英文:

CollapsingToolbarLayout inside parent fragment and child fragment tablayout with recyclerView are not scrolling together

问题

我有一个具有CollapsingToolbarLayout父片段,其中包含一些布局。

然后在里面有一个带有两个子片段TabLayout,两个子片段都有RecyclerView,它们不会一起滚动。

当用户在子RecyclerView中向上滚动和向下滚动时,父片段TabLayout子片段textView会上下移动/隐藏和显示。

如何解决这个问题?

具有CollapsingToolbarLayout布局的父片段

<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">

    <androidx.appcompat.widget.AppCompatEditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="20dp"
        android:background="@drawable/white_speed_search_bar_rounded_rectangle"
        android:text="Search"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edit">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_scrollFlags="scroll|enterAlways">

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tab"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

            </com.google.android.material.appbar.CollapsingToolbarLayout>

        </com.google.android.material.appbar.AppBarLayout>

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/app_bar">
        </androidx.viewpager2.widget.ViewPager2>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

子片段1和2 布局:

<LinearLayout
    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:orientation="vertical">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="16dp" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
英文:

I have a parent fragment with CollapsingToolbarLayout with some layouts.

Then inside, there is a TabLayout with two child fragments having recycer-View both are not scrolling together.

when user scroll-up and scroll-down inside child recycler-View Parent fragment tabLayout and Child Fragment textView (move up and down)/hide and show.

How to solve this issue?

Parent Fragment with CollapsingToolbarLayout layout:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;androidx.appcompat.widget.AppCompatEditText
android:id=&quot;@+id/edit&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;50dp&quot;
android:layout_margin=&quot;20dp&quot;
android:background=&quot;@drawable/white_speed_search_bar_rounded_rectangle&quot;
android:text=&quot;Search&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout
android:id=&quot;@+id/main_content&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/edit&quot;&gt;
&lt;com.google.android.material.appbar.AppBarLayout
android:id=&quot;@+id/app_bar&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:theme=&quot;@style/ThemeOverlay.AppCompat.Dark&quot;&gt;
&lt;com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_scrollFlags=&quot;scroll|enterAlways&quot;&gt;
&lt;com.google.android.material.tabs.TabLayout
android:id=&quot;@+id/tab&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
/&gt;
&lt;/com.google.android.material.appbar.CollapsingToolbarLayout&gt;
&lt;/com.google.android.material.appbar.AppBarLayout&gt;
&lt;androidx.viewpager2.widget.ViewPager2
android:id=&quot;@+id/viewPager&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/app_bar&quot;&gt;
&lt;/androidx.viewpager2.widget.ViewPager2&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

Child Fragment 1&2 layout:

&lt;LinearLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;&gt;
&lt;TextView
android:id=&quot;@+id/tv&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;16dp&quot;/&gt;
&lt;androidx.recyclerview.widget.RecyclerView
android:id=&quot;@+id/recyclerview&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;/&gt;
&lt;/LinearLayout&gt;

答案1

得分: 2

你需要将滚动行为设置为 ViewPager,以使 CoordinatorLayout 能够同步 ViewPager 内容(即 RecyclerView)与 CollapsingToolbarLayout 的滚动。

<androidx.viewpager2.widget.ViewPager2
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    ...>
英文:

You need to set the scrolling behavior to the ViewPager so that the CoordinatorLayout is able to sync the scrolling of the ViewPager content (i.e., the RecyclerView) with the CollapsingToolbarLayout

&lt;androidx.viewpager2.widget.ViewPager2
app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot;
...&gt;

huangapple
  • 本文由 发表于 2023年2月16日 01:49:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463653.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定