1

I want to Scroll ReyclerView inside a Fixed height/region.

Sample XML to demonstrate the use case

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ele_parent_ll"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent">


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


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/sample_id"
            android:background="@color/white"
            android:fitsSystemWindows="true">

            <ImageView
                android:contentDescription="@string/btn_youtube"
                android:layout_width="match_parent"
                android:layout_height="115dp"
                android:id="@+id/election_banner"
                android:background="@drawable/some_drawable"
                android:adjustViewBounds="true"/>

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



                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/mid_parent"
                    android:layout_marginLeft="@dimen/view_margin_10"
                    android:layout_marginRight="@dimen/view_margin_10"
                    android:layout_marginTop="@dimen/view_margin_10"
                    android:orientation="horizontal"
                    android:weightSum="1">

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight=".5"
                        android:orientation="vertical">

                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@drawable/shaded_purple_new_local"
                            android:text="Layout heading"
                            android:textColor="@color/white" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:text="Choose one"
                            android:textColor="@color/icon_yellow"
                            android:textSize="13sp" />


                        <android.support.v7.widget.RecyclerView
                            android:id="@+id/candidate_recycler"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:nestedScrollingEnabled="false">
                        </android.support.v7.widget.RecyclerView>
                    </LinearLayout>
                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/view_margin_10"
                        android:layout_marginLeft="@dimen/view_margin_10"
                        android:layout_weight=".5"
                        android:orientation="vertical">
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

            <!--Sample Layout to make the screen scrollable for the example-->
            <RelativeLayout
                android:id="@+id/samp_RL"
                android:layout_width="match_parent"
                android:layout_height="200dp">
            </RelativeLayout>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>

In this scenario, the RecyclerView is taking it's required space and list is pretty much long so the total length of the scrollable area has gone long so probability of seeing elements below RCV has decreased.

To solve this I want to scroll RecyclerView inside a fixed area so that If the user is not interested in that he can easily look up to the other Elements present below the RecyclerView

  1. When user scrolls within the region of RCV the RCV should scroll.
  2. When the user scrolls outside the RCV the whole Layout should scroll

enter image description here

Image made at draw.io

What I have tried:

  1. Making the height of RecyclerView constant: It is Jammed and is not scrolling at all.
  2. Making Recyclerview Fixed Height and Scrollable : It's also the same. It is jammed and is not scrolling at all.
  3. Scrollable NestedScrollViews inside RecyclerView: Tried this but could not establish a connection to my current situation.

Please tell what I am missing here.

nimi0112
  • 2,065
  • 1
  • 18
  • 32

3 Answers3

2

you do not need to do anything. by default it works like this only ! Try the below code -

recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
        recyclerView.setHasFixedSize(false);
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        mAdapter = new MyAdapterClass(getContext(), lst);
        recyclerView.setAdapter(mAdapter);

XML

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.iosdteachingapp.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="100sp"
        android:text="hello"/>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="200sp"
        android:layout_height="300sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="end"
        android:padding="20sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="end"
        android:padding="20sp"/> <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="end"
    android:padding="20sp"/> <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="end"
    android:padding="20sp"/>
</LinearLayout>
</ScrollView>
Amir Dora.
  • 2,831
  • 4
  • 40
  • 61
  • Hello deepanshi, I tried your code but it does not make `recyclerView` scroll independently. It only displays the content within it's height and does not scroll i.e it's jammed. I have given my XML in the question above, please use that and see if you can help me out. Thank you. – nimi0112 May 24 '18 at 17:45
  • i have tried it and made the same ui as you showed and its working – Deepanshi Bansal May 26 '18 at 05:39
1

Add android:nestedScrollingEnabled="false" to your RecyclerView.

Tin Nguyen
  • 420
  • 5
  • 9
0

Set Recycler view height dynamically thr java using param. You can even set the height to show only particular no of elements by setting height = size of each element of recycler view * no of elements in the list. Try this :

ViewGroup.LayoutParams 
params=recyclerview.getLayoutParams();
params.height=100;
recyclerview.setLayoutParams(params);
  • Hello Sparsh, I tried your code but it does not make `recyclerView` scroll independently. It only displays the content within its height and does not scroll i.e it's jammed. I have given my XML in the question above, please use that and see if you can help me out. Thank you. – nimi0112 May 24 '18 at 17:46