0

I want a layout in which I need recyclerView in between two views and for scrolling all, i need scrollView. I have created a sample program in which i have used below xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
tools:context="com.example.vatishs.recyclerviewunderscrollview.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom|start"
        android:hint="Hint" />

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

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom|start"
        android:hint="Hint" />
</LinearLayout>
</ScrollView>

Now, I don't understand why recyclerView is not visible when i run this program?

It is visible if I am not use scrollView.

Vatish Sharma
  • 1,536
  • 3
  • 16
  • 35
  • 1
    I hope that this post to help you:http://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-working – David Rauca Feb 04 '16 at 06:23
  • 2
    If you expect the EditTexts to be scrolled together with RecyclerView elements, it would be easier to handle different view types within the RecyclerView. A RecyclerView should not be put inside a ScrollView, as reported by other users too. – andrea.petreri Feb 04 '16 at 06:44
  • Thanks, I got your point :) – Vatish Sharma Feb 04 '16 at 06:52

4 Answers4

1

Replace your xml with xml below.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    tools:context="com.example.vatishs.recyclerviewunderscrollview.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin">


        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom|start"
            android:hint="Hint" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/mRecycler"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="0dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom|start"
            android:hint="Hint" />
    </LinearLayout>
</ScrollView>
Chintan Bawa
  • 1,376
  • 11
  • 15
  • thanks, my recyclerView is visible now, but i want that recyclerView displays all items as a child of scrollview means only scroll view will scroll all items and also scroll view's scroll takes all scrolling effect from recyclerView – Vatish Sharma Feb 04 '16 at 06:30
  • I want the same effect as if we make above and below views of recycler view as its header and footer in its adapter, Any way to do it with scrollView ???? – Vatish Sharma Feb 04 '16 at 06:33
  • Your welcome. This will be an another question, so please ask a new question. So that more People can help you. @VatishSharma – Chintan Bawa Feb 04 '16 at 06:37
1

you have given height of you main layout to wrap_content, change it to match_parent

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context="com.example.vatishs.recyclerviewunderscrollview.MainActivity">
Ravi
  • 34,851
  • 21
  • 122
  • 183
0

Here is the correct xml for my problem:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context="com.example.vatishs.recyclerviewunderscrollview.MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom|start"
        android:hint="Hint" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/mRecycler"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom|start"
        android:hint="Hint" />
</LinearLayout>
</ScrollView>

and for recycler view to allow scroll with other items of scrollView , we have to give recyclerView height at runtime by setting its LayoutParams. Like for 100 TextView of 50dp each, the recyclerView Height will be:

mRecyclerView = (RecyclerView) findViewById(R.id.mRecycler);

    float density = getResources().getDisplayMetrics().density;
    mRecyclerView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ((int) (density * 50)) * 100));

It will work fine :)

Vatish Sharma
  • 1,536
  • 3
  • 16
  • 35
-1

Technically you should not place a scrollable view inside a scrollable view. This means, you cannot put RecyclerView inside ScrollView.

Tafveez Mehdi
  • 456
  • 3
  • 12