Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView is not trully wrap_content at the ScrollView

I have a ScrollView, which contains a vertical LinearLayout. This is a place, where I add some amount of Views called "Section". "Section" is a LinearLayout, which contains a TextView and `RecyclerView.

<ScrollView
    android:id="@+id/main_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/sections_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />
</ScrollView>

section:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:textSize="@dimen/activity_starred_title_textsize" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

The problem is that RecyclerView is not truly wrap_content sometimes. Therefore it creates 2 types of problems (depending on solution type I'm trying to use).

  1. It can be unscrollable (so it matches screen height and I can't scroll it down to see rest of items inside).

  2. It can scroll nested.

Originally problem is that it has nested scrolling. So I want RecyclerViews to be as simple vertical LinearLayouts and the only thing that has to have scrolling effect is root ScrollView.

What have I tried to do?

  1. extend the GridLayoutManager and override canScrollVertically(). method:

    public boolean canScrollVertically(){ return false; }

  2. extend RecyclerView class and override

    @Override public boolean onInterceptTouchEvent(MotionEvent event){ return false; }

    @Override public boolean onTouchEvent(MotionEvent event){ return false; }

  3. Disable NestedScrolling via xml everywhere, where it is possible.

  4. Override GridLayoutManager using this solution: SOLUTION

  5. Combine 1-4
like image 253
M. Maximovich Avatar asked May 25 '17 23:05

M. Maximovich


People also ask

Can I use RecyclerView inside ScrollView Android?

card view inside the ScrollView (now ScrollView contains RecyclerView ) - can see the card up until the RecyclerView. initial thought was to implement this viewGroup using RecyclerView instead of ScrollView where one of it's views type is the CardView but i got the exact same results as with the ScrollView.

What is the difference between ScrollView and RecyclerView?

In the practical on scrolling views, you use ScrollView to scroll a View or ViewGroup . ScrollView is easy to use, but it's not recommended for long, scrollable lists. RecyclerView is a subclass of ViewGroup and is a more resource-efficient way to display scrollable lists.

Is RecyclerView scrollable?

To help you build apps with lists, Android provides the RecyclerView . RecyclerView is designed to be very efficient, even with large lists, by reusing, or recycling, the views that have scrolled off the screen.

What is Wrap_content?

WRAP_CONTENT means that the view wants to be just large enough to fit its own internal content, taking its own padding into account.


1 Answers

Don't use RecyclerView or ListView inside ScrollView. For nested scrolling you should use NestedScrollView.

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

SOLUTION:

1. Instead of using ScrollView, use NestedScrollView as container of your Section part(RecyclerView and other Views).

<android.support.v4.widget.NestedScrollView
    android:id="@+id/main_scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/sections_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- Your Section:: RecyclerView and other Views -->

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

2. For scrolling issue use setNestedScrollingEnabled(false) to your RecyclerView.

like image 81
Ferdous Ahamed Avatar answered Oct 17 '22 05:10

Ferdous Ahamed