0

Details:

Dependency: (latest)

implementation 'com.android.support:recyclerview-v7:27.0.2'

XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_gray">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/marginTwenty"
    android:layout_marginStart="@dimen/marginTwenty"
    android:layout_marginTop="@dimen/marginTen"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/contactLoadingLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="@dimen/marginThirty">

        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/waitText"
            android:textAlignment="center"
            android:textStyle="bold" />

    </LinearLayout>

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

        <LinearLayout
            android:id="@+id/mobileView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_mobile" />


                <TextView
                    android:id="@+id/txtMobileNumber"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/mobileNumber"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/mobileRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/txtNoMobileData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />

            <TextView
                android:id="@+id/btnAddMobile"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/emailView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">


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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="18dp"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_email" />


                <TextView
                    android:id="@+id/txtEmailNumber"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/email"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/emailRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/txtNoEmailData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />

            <TextView
                android:id="@+id/btnAddEmail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/facebookView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_facebook" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/facebookHint"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/facebookRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/txtNoFacebookData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />


            <TextView
                android:id="@+id/btnAddFacebook"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linkedInView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_linkedi" />

                <TextView
                    android:id="@+id/txtLinkedIn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/linkedInHint"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/linkedInRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/txtNoLinkedInData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />

            <TextView
                android:id="@+id/btnAddLinkedIn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/twitterView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_twitter" />

                <TextView
                    android:id="@+id/txtTwitter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/twitterHint"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/twitterRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />

            <TextView
                android:id="@+id/txtNoTwitterData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />

            <TextView
                android:id="@+id/btnAddTwitter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/youtubeView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_youtube" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/youtubeHint"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/youtubeRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:adjustViewBounds="true"
                android:scaleType="fitXY" />


            <TextView
                android:id="@+id/txtNoYoutubeData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />

            <TextView
                android:id="@+id/btnAddYoutube"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/gitHubView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/marginTwenty"
            android:layout_marginTop="@dimen/marginTen"
            android:background="@drawable/curved_background_white"
            android:orientation="vertical"
            android:padding="@dimen/marginTen">

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

                <ImageView
                    android:layout_width="@dimen/marginTwenty"
                    android:layout_height="@dimen/marginTwenty"
                    android:layout_gravity="center_vertical"
                    android:layout_marginEnd="5dp"
                    android:contentDescription="@string/dummyContent"
                    android:src="@drawable/ic_github" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                    android:text="@string/gitHubHint"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/textSize_18sp"
                    android:textStyle="bold" />
            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/githubRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp" />

            <TextView
                android:id="@+id/txtNoGitHubData"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="@dimen/marginTen"
                android:text="@string/no_contact_found"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold"
                android:visibility="gone" />


            <TextView
                android:id="@+id/btnAddGithub"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:text="@string/add"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/textSize_18sp"
                android:textStyle="bold" />
        </LinearLayout>

    </LinearLayout>

    <include
        android:id="@+id/noInternetLayoutContact"
        layout="@layout/no_internet_layout"
        android:visibility="gone" />

    </LinearLayout>

</ScrollView>

JAVA Code:

youtubeRecyclerView.setLayoutManager(new 
LinearLayoutManager(getActContext(), LinearLayoutManager.VERTICAL, false));
youtubeRecyclerView.setHasFixedSize(true);
youtubeRecyclerView.setItemAnimator(new DefaultItemAnimator());
youtubeRecyclerView.setAdapter(attributeAdapter);
youtubeRecyclerView.setVisibility(View.VISIBLE);

Note:

  • i have 6 recycler views in single activity and out of those 6 any one of the recycler view becomes scrollable and rest works fine (wrap_content) based on screen size.
  • My Parent View is ScrollView.

I have done enough research on internet and stackoverflow, couldn't find the solution, So asked question here. Please don't mark as duplicate.

KhanStan99
  • 414
  • 8
  • 20
  • `android:adjustViewBounds="true" android:scaleType="fitXY"` are properties of imageView – Manohar Mar 06 '18 at 12:32
  • Please post whole your xml file. It is hard to understand what would you like to get ;) – muminers Mar 06 '18 at 12:33
  • My recyclerview was without those properties, I tried that just experiment it didn't worked out. – KhanStan99 Mar 06 '18 at 12:33
  • @muminers, added whole XML file code. – KhanStan99 Mar 06 '18 at 12:36
  • try [this](https://stackoverflow.com/a/32390370/5148289) solution – V-rund Puro-hit Mar 06 '18 at 12:38
  • Why do you use a RecyclerView if you don't want it to scroll? It kind of defies its purpose. You could go on with static layouts instead. And having one scroll/recycler view inside another is usually a bad idea. – algrid Mar 06 '18 at 12:41
  • my list will have 5-6 items maximum, so i want user to see whole recyclerview instead of scrollview. and when the list decreases to 1 or 2 or 3 items it should automatically decrease. – KhanStan99 Mar 06 '18 at 12:45
  • How ever i can give a fixed height to my recyclerview and make it scrollable if the items get increased 5 (which might not happen) but if the list count is 2 or 3 recyclerview will have empty white space which will look bad – KhanStan99 Mar 06 '18 at 12:47
  • @KhanStan99 what you can do is use `NonScrollListView` instead `recyclerview`. they will automatically resized according to items in the list – V-rund Puro-hit Mar 06 '18 at 12:57
  • Yes i can do that. – KhanStan99 Mar 06 '18 at 13:02

3 Answers3

0

Try wrapping RecyclerView using fixed height parent

You can go for a fixed height or wrap_content for LinearLayout parent

          <LinearLayout
            android:id="@+id/llRecyclerContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/marginTen">

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

In your java file :

facebookRecyclerView.setHasFixedSize(true);
Deep Patel
  • 2,584
  • 2
  • 15
  • 28
  • In this case what if the size is small? like 2 items? in that case it will have empty white space, which will make the UI look bad – KhanStan99 Mar 06 '18 at 12:48
  • RecyclerView will get scrolled in this fixed area you are wrapping – Deep Patel Mar 06 '18 at 12:49
  • That was my question bro. I gave recyclerview wrapcontent height, but it is becoming scrollable when the list length increases 3 which i dont want. I want to set height of my all 6 recyclerview scrollable even if the list size increases 3. – KhanStan99 Mar 06 '18 at 12:52
  • Please check my question. i already gave youtubeRecyclerView.setHasFixedSize(true); – KhanStan99 Mar 06 '18 at 12:55
0

It's not good practice to use RecycylerView inside scrollView as result it won't support recycling if views.

Better use one vertical parent RecyclerView and based on view type add your child RecyclerView to parent.

Rahul Devanavar
  • 3,917
  • 4
  • 32
  • 61
0

I managed to resolve this issue by putting my recyclerview in a nestedscrollview and kept 'android:nestedScrollingEnabled' disabled/false.

KhanStan99
  • 414
  • 8
  • 20
  • 1
    Placing a recycler view inside a nested scroll view disables view recycling and therefore defeats the purpose of using a recyclerview. If you have a large list of data this will all be drawn at create time and would cause the main thread to lag. – AndroidDev123 Dec 26 '21 at 17:12