android: RecyclerView inside a ScrollView (or parallax)

假如想象 提交于 2020-01-01 05:21:11

问题


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:


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/28048603/android-recyclerview-inside-a-scrollview-or-parallax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!