Qazi Hassan
Qazi Hassan

Reputation: 35

Two recycleviews in one activity scroll problem

I have created 2 recycleviews in one activity. One scrolls horizontally while other scrolls vertically. I can scroll correctly inside each RecyclerView but the page as a whole won't scroll i.e. top RecyclerView stays at the top always and bottom one stays at the bottom like both are fixed in position.

<ScrollView 
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"
tools:context="com.shakeelnawaz.recipes.AllRecipesFragmet">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:text="@string/trending_recipes"
        android:textSize="18sp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/horizontaltrendingrecycleview"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_marginStart="15dp"
        android:orientation="horizontal"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="20dp"
        android:text="@string/all_recipes"
        android:textSize="18sp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v7.widget.RecyclerView>
    </LinearLayout>
</ScrollView>

I read this post "Scolling with multiple RecyclerViews in Layout" and set vertical recycleview's height programmatically. like this

LinearLayout.LayoutParams params = new     
 LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
 ViewGroup.LayoutParams.WRAP_CONTENT);
// calculate height of RecyclerView based on child count
params.height=1150;
// set height of RecyclerView
recyclerView.setLayoutParams(params);

But the problem is that how I can calculate the height of RecyclerView based on child count?

Upvotes: 0

Views: 284

Answers (2)

Rakesh C
Rakesh C

Reputation: 41

Replace ScrollView with NestedScrollView

Then add: horizontaltrendingrecycleview.isNestedScrollingEnabled = false

Your XML should look like below:

<android.support.v4.widget.NestedScrollView 
  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:fillViewport="true"
  tools:context="com.shakeelnawaz.recipes.AllRecipesFragmet">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginBottom="10dp"
      android:layout_marginStart="20dp"
      android:layout_marginTop="20dp"
      android:text="@string/trending_recipes"
      android:textSize="18sp" />

    <android.support.v7.widget.RecyclerView
      android:id="@+id/horizontaltrendingrecycleview"
      android:layout_width="match_parent"
      android:layout_height="240dp"
      android:layout_marginStart="15dp"
      android:orientation="horizontal"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="20dp"
      android:text="@string/all_recipes"
      android:textSize="18sp" />

    <android.support.v7.widget.RecyclerView
      android:id="@+id/recycleView"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="20dp"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

  </LinearLayout>
</android.support.v4.widget.NestedScrollView>

Upvotes: 1

sushildlh
sushildlh

Reputation: 9056

Use NestedScrollView instead of ScrollView and use this below line in your Fragment in Activity.

ViewCompat.setNestedScrollingEnabled(mYourRecycleView, false);

This will be work on all your Android API level.

Upvotes: 1

Related Questions