2

I'm trying to place a card view above a recycler view. I Would like to scroll my card view and recycler view as one. The problem is, only the recycler view is scrolling and the contents of the recycler view are quite distorted in size. This is what I'm trying to achieve: Example Image

Here's my xml:

<android.support.constraint.ConstraintLayout 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:id="@+id/frameLayout3" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".HomeFragment"> <!-- TODO: Update blank fragment layout --> <android.support.v7.widget.CardView android:id="@+id/cardView" android:layout_width="match_parent" android:layout_height="100dp" android:isScrollContainer="false" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginEnd="9dp" android:src="@drawable/background" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.v7.widget.CardView> <android.support.v7.widget.RecyclerView android:id="@+id/blogListView" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintTop_toBottomOf="@+id/cardView" /> 

Would Appreciate any suggestions.

2
  • put your constraint layout inside a nested scrollview Commented Mar 2, 2019 at 18:54
  • you may use your cardView in a collapse view as appbar to collapse it while scrolling and show it back when it is necessary. Commented Mar 2, 2019 at 19:19

2 Answers 2

5

Put your recyclerview and cardView in a scrollView, and add android:nestedScrollingEnabled="true" in recycerlview

 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/frameLayout3" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- TODO: Update blank fragment layout --> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.CardView android:id="@+id/cardView" android:layout_width="match_parent" android:layout_height="100dp" android:isScrollContainer="false" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginEnd="9dp" android:src="@drawable/background" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.v7.widget.CardView> <android.support.v7.widget.RecyclerView android:id="@+id/blogListView" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:nestedScrollingEnabled="true" app:layout_constraintTop_toBottomOf="@+id/cardView" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> </android.support.constraint.ConstraintLayout> 
Sign up to request clarification or add additional context in comments.

3 Comments

Hi bipin. It's showing me this error android.view.InflateException: Binary XML file line #37: ScrollView can host only one direct child
Update: I put both of them in linear layout. Works fine..Thank you
this calls onBindViewHolder for all the items of recyclerview instead of ones which are can be visible
0
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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="#FFF"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="15dp" tools:layout_editor_absoluteY="50dp"> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbarSize="1sp"> <android.support.v4.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingBottom="50dp"> <TextView android:id="@+id/TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="5dp" android:text="TextView" app:fontFamily="@font/proximanova_regular" /> <EditText android:id="@+id/EditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="numberDecimal" app:fontFamily="@font/proximanova_regular" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:nestedScrollingEnabled="true" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> </ScrollView> </LinearLayout> </android.support.constraint.ConstraintLayout> 

1 Comment

Please add some info talking about what you've changed and why

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.