Search code examples
androidandroid-recyclerviewvertical-scrollingandroid-nestedscrollview

Recyclerview inside ViewPager which is inside nested Scroll View


I have used the following code inside coordinator layout and i am using custom collapsing toolbar design.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="RtlHardcoded">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:background="@drawable/backg">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <FrameLayout
                android:id="@+id/main.framelayout.title"
                android:layout_width="match_parent"
                android:layout_height="130dp"
                android:layout_gravity="bottom"
                android:background="#00000000"
                android:orientation="vertical"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.3">

                <LinearLayout
                    android:id="@+id/main.linearlayout.title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="30dp"
                    android:layout_gravity="left"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="bottom"
                        android:text="User Name"
                        android:id="@+id/username"
                        android:textColor="@android:color/white"
                        android:textSize="30sp"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="4dp"
                        android:id="@+id/business"
                        android:text="Users Business"
                        android:textColor="@android:color/white"/>
                </LinearLayout>

            </FrameLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/main.toolbar"
                android:layout_width="match_parent"
                android:layout_height="110dp"
                android:background="#00000000"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:title=""
                app:layout_collapseMode="pin">

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

                    <ImageView
                        android:layout_width="32dp"
                        android:layout_height="32dp"
                        android:id="@+id/back"
                        android:layout_marginTop="10dp"
                        android:background="@drawable/ic_arrow_back"/>

                    <TextView
                        android:id="@+id/main.textview.title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="12dp"
                        android:text="User Name"
                        android:textColor="@android:color/white"
                        android:textSize="24sp"
                        />

                </LinearLayout>
            </android.support.v7.widget.Toolbar>
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#00000000"
                android:layout_gravity="bottom"
                app:tabMode="fixed"
                app:tabGravity="fill"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_gravity="fill_vertical"
        android:fillViewport="true"
        android:layout_marginBottom="110dp">

             <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                 android:layout_gravity="fill_vertical"
                android:layout_height="match_parent"
                android:background="@android:color/white">
            </android.support.v4.view.ViewPager>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

Now what problem i am facing is, when i run this app on Lower OS versions, it is all okay while scrolling in all viewpager fragments Recyclerviews, but when i use OS above Android.M, recyclerview's height is out of display screen, and it is not visible enough to see the item on screen. Please help me resolve my problem. I have designed my custom collapsing bar layout, but it is all okay with it in lower Android OS.

[EDITED]
Here are some snapshots when i add margin_bottom of my actionbar size.
enter image description here <--- Snapshot End.
You can see the viewpagers height is less then screen size as we are using marginBottom.
Now
For Higher varients, its all okay as below while using marginBottom.
enter image description here


Solution

  • Thanks for your reviews and answers to my questions, But after digging much i didnt found anything useful, and though to set height programmatically. so i tried snippet below, and that works like a charm. well for heigher varients, set marginBottom with actionbar size in your layout xml in NestedScrollView and for lower varients, Use this Hack below.

     NestedScrollingView nsv = findViewByID(R.id.nsv);
    
     if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.M){
            android.support.design.widget.CoordinatorLayout.LayoutParams lpm = new android.support.design.widget.CoordinatorLayout.LayoutParams(
                    android.support.design.widget.CoordinatorLayout.LayoutParams.MATCH_PARENT, android.support.design.widget.CoordinatorLayout.LayoutParams.MATCH_PARENT);
            lpm.setMargins(0,0,0,0);
            lpm.setBehavior(new android.support.design.widget.AppBarLayout.ScrollingViewBehavior());
            nsv.setLayoutParams(lpm);
            nsv.requestLayout();
        }
    

    Again, Thanks for your help.