6

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

seba123neo
  • 4,688
  • 10
  • 35
  • 53
  • please be a little clearer because it is not clear what the actual problem is – tyczj Jan 20 '15 at 15:06
  • imagine 2 cardviews in a fragment, these occupy 80% of the screen, below 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. – seba123neo Jan 20 '15 at 15:10
  • 1
    set a min height to the recylerview ? – Alexander Sidikov Pfeif Jan 20 '15 at 15:14
  • 1
    or put a scrollview around all set recyclerView height to wrap content. and diable scrolling of the recyclerview with: .setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); – Alexander Sidikov Pfeif Jan 20 '15 at 15:17
  • I tried what you said, but the scroll only works for both cardview and does nothing when I want to scroll the recyclerview – seba123neo Jan 20 '15 at 15:30
  • putting a fixed height works !!! but I need to calculate the height of the number of items in the list... – seba123neo Jan 20 '15 at 15:36
  • Dont' use a scrollable view inside a ScrollView. – Gabriele Mariotti Jan 20 '15 at 19:03
  • Try this. http://stackoverflow.com/questions/26530685/is-there-an-addheaderview-equivalent-for-recyclerview/26573338#26573338 – Sultan Feb 28 '15 at 19:48
  • Did you get a solution for this issue? I am stuck at a similar point. I am able to scroll from the recyclerview, but cant scroll the entire screen with fields before the recyclerview – Rashmi.B Jul 29 '15 at 13:14
  • 2
    yes, the solution for now I have used and works on all devices that have tried, is to put the property on the recyclerview android: layout_height = "wrap_content" (inside a ScrollView) and then use this LinearLayoutManager for recyclerview http://stackoverflow.com/a/27616854/518657, which calculates the height of the items, the only problem is that all items must be of the same height. if anyone has a more elegant solution to this problem, please post it, it can not be that there is NO solution to this very basic problem. – seba123neo Aug 03 '15 at 13:31
  • try this. http://stackoverflow.com/questions/33058496/set-starting-height-of-collapsingtoolbarlayout/33194147#33194147. in this sample you can replace recyclerview with nestedscrollview – Mohammad Hossein Gerami Oct 20 '15 at 08:03
  • Please check the following answer [which uses custom layout manager to calculate the height](http://stackoverflow.com/a/33542789/3257178) – Arun Antoney Nov 05 '15 at 10:59
  • You have to give 0dp height to recyclerView and give height at runtime according to total no. of item * height of each item in dp. Like if we have 10 textview of 40dp each, then vertical recyclerView height will be 10*40 dp = 400dp and 10dp for horizontal. Then it will works fine :) – Vatish Sharma Feb 04 '16 at 10:38

2 Answers2

2

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.

Deepak Baliga
  • 245
  • 2
  • 7
0

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

Community
  • 1
  • 1
Alexander Sidikov Pfeif
  • 2,418
  • 1
  • 20
  • 35