seba123neo
seba123neo

Reputation: 4748

android: RecyclerView inside a ScrollView (or parallax)

I have a fragment with 2 cardviews with multiple controls within.

below the second cardview I have a recyclerview, this works perfect.

the problem is that the recyclerview starts very bottom of the screen, and scroll the recylcerview is very small.

previously used a listview, and this kept me fit your content and thus make scroll across the screen at once, but with recylclerview can not.

How to make when I scroll in the recyclerview, controls go up like parallax effect?

EDIT: more clear, imagine 2 cardviews in a fragment, these occupy 80% of the screen, above of these, a recyclerview with a list of 100 items, but the scroll is so tiny...the listview let me adapt to the "content" and scroll the entire screen at once.

this is my XML layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/svScroll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <include
            android:id="@+id/cvCampos1"
            layout="@layout/ctlcabeceralibreta" />

        <include
            android:id="@+id/cvCampos2"
            layout="@layout/ctlcabeceralibreta2" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvRegistros"
            android:layout_width="fill_parent"
            android:layout_height="1000dp"
            android:layout_below="@id/cvCampos2"
            android:scrollbars="vertical" />
    </RelativeLayout>

</ScrollView>

this is the screenshoot:

enter image description here

Upvotes: 6

Views: 10901

Answers (2)

Alexander Sidikov Pfeif
Alexander Sidikov Pfeif

Reputation: 2435

putting a fixed height works !!! but I need to calculate the height of the number of items in the list

this works for listviews (i dont tested it with recyclerview)

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            if (listItem instanceof ViewGroup) {
                listItem.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            }
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

Src:

Full example

Upvotes: 0

Deepak Baliga
Deepak Baliga

Reputation: 245

In the above case what you need to do is to use multiple ViewHolders in the same recyclerview.

I see that you have three type of layouts there. The first two cards and other layouts in your recyclerview.

RecyclerView with multiple View Types gives you an example how to use multiple viewtypes in a simple recyclerview.

Upvotes: 2

Related Questions