Reputation: 658
I have a Cordinatorlayout that contains a viewpager , a recyclerview, a tablayout and some other views,, i want my screen to scroll properly, but it does not.
(the problem is shown in the gif)
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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/white"
tools:context=".home.ui.views.HomeFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|snap"
app:toolbarId="@id/toolbar">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/white"
app:contentInsetStart="0dp"
app:layout_collapseMode="pin">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:fontFamily="@font/worksans_regular"
android:text="Discover"
android:textColor="@color/purple_S600"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="26dp"
android:layout_height="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:backgroundTint="@color/white_grey_s400"
android:drawableLeft="@drawable/account_icon"
android:padding="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.MaterialToolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/search_textInputLayout"
android:layout_width="match_parent"
android:layout_height="38dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="4dp"
android:layout_marginTop="8dp"
android:background="@drawable/search_edittext_shape"
android:textColorHint="@color/purple_grey_S600"
app:boxStrokeWidth="0dp"
app:boxStrokeWidthFocused="0dp"
app:endIconDrawable="@drawable/close_icon"
app:endIconMode="clear_text"
app:endIconTint="@color/purple_S600"
app:hintEnabled="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:startIconDrawable="@drawable/search_icon"
app:startIconTint="@color/purple_S600">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/home_search_tf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search"
android:inputType="text"
android:padding="0dp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/categories_tabs"
style="@style/CategoriesTabLayourStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:visibility="visible"
/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/home_constraint"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<androidx.core.widget.NestedScrollView
android:id="@+id/nested_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_gravity="start"
android:background="@color/yellow_S600"
android:fontFamily="@font/worksans_semibold"
android:text="Deals "
android:textColor="@color/purple_S600"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-28dp"
android:src="@drawable/deals_arrow"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="@+id/textView2" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="@font/inter_regular"
android:text="UP to 75% off"
android:textColor="@color/purple_S600"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@+id/textView2"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toTopOf="@+id/textView2" />
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="2dp"
android:src="@drawable/flame_icon"
app:layout_constraintBottom_toBottomOf="@+id/textView5"
app:layout_constraintStart_toEndOf="@+id/textView5"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/deals_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginStart="8dp"
android:background="@color/white"
android:clipToPadding="false"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"
tools:listitem="@layout/deal_product" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nested_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I have tried so many tweaks but it just didn't seem to work. As a last resort I have tried to put the deals section which is a constraintlayout that contains a couple of views with a horizontal recyclerview. it try to put it inside a NestedScrollView but this does not work either.
Upvotes: 0
Views: 52
Reputation: 658
I should have wrapped the constraint layout with the nestedscrollview. I did it the other way around.
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/home_constraint"
app:layout_anchor="@id/app_bar_layout"
app:layout_anchorGravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">
<TextView
.... />
<ImageView
.... />
<TextView
.... />
<ImageView
..../>
<androidx.recyclerview.widget.RecyclerView
.... />
<androidx.viewpager2.widget.ViewPager2
.... />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
This the solution, Thanks everyone
Upvotes: 0
Reputation: 767
Avoid nesting the RecyclerView
inside a NestedScrollView
Instead of using a NestedScrollView
, you can directly use the RecyclerView
with a LinearLayoutManager
set to a horizontal orientation. This way, you won't need the extra layer of nesting and can achieve the desired scrolling behavior within the RecyclerView itself. In a RecyclerView
, it is more easy to handle different types of view types. https://developer.android.com/develop/ui/views/layout/recyclerview
The drawbacks when you use a RecyclerView
inside a NestedScrollView
are described here. Why am I experiencing ANRs with RecyclerView inside NestedScrollView and what are the recommended alternatives?
In your current code you can fix the scroll issue by adding below flag in RecyclerView
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:nestedScrollingEnabled="false"
Upvotes: 0
Reputation: 376
This is not answer that you want, but it is suggestion. You can solve this issue with concat adapter and recyclerview, instead of nested scrollviews.
https://developer.android.com/reference/androidx/recyclerview/widget/ConcatAdapter
Upvotes: 0