8

I have ViewPager inside NestedScrollView and in order to make my ViewPager attach the right height and scroll attributes I added android:nestedScrollingEnabled="true" to my NestedScroll.., here is a snap for my 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">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent">

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nestedScroll"
        android:nestedScrollingEnabled="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/_7sdp"
            android:layout_marginRight="@dimen/_7sdp"/>
    </android.support.v4.widget.NestedScrollView>


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

The issue: Is when I set android:nestedScrollingEnabled="true" to true I got those exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{somePackege}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>

AND

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.NestedScrollingChildHelper.setNestedScrollingEnabled(boolean)' on a null object reference

I'm confused if android:nestedScrollingEnabled="@otherAtt" take another attribute (Not boolean).

And I'm was wondering if there a better approach to reach this behavior.

1 Answers1

6

Ok so the way I made your layout work is to not use the setNestedScrollingEnabled method in the xml and instead set it in runtime inside your activity or fragment like so:

NestedScrollView view = (NestedScrollView) findViewById(R.id.nestedScroll);
view.setNestedScrollingEnabled(true);

Doing it this way also reduces the API level needed, as to setNestedScrollingEnabled in xml requires API level 21 or higher.

Anthony Cannon
  • 1,245
  • 9
  • 20
  • 1
    ``=setNestedScrollingEnabled in xml requires API level 21 or higher` true> and setting `setNestedScrollingEnabled(true);` using java didn't crash application, but it's doesn't the most important thing.. it's dosen't set the right hegiht for viewPager, the viewPager compressed and didn't attached to the view. –  Feb 12 '18 at 15:47
  • 3 things made this layout crash for me, the two times you used `app:layout_behavior` and the time you used `android:nestedScrollingEnabled`, so i removed the `layout_behavior` and move the `nestedScrollingEnabled` – Anthony Cannon Feb 12 '18 at 15:53
  • Why do you use NestedScrollView, is there a specific reason? Because ViewPager is able to handle scrolling to transition to and from its fragments. – Anthony Cannon Feb 12 '18 at 16:08
  • Did you see the `CoordinatorLayout` ? I used it to use `app:layout_behavior` and that's a scrolling behavior, also viewPager contain recycleView that's another scroll. so it's required to use nestedscrollview. –  Feb 12 '18 at 16:11
  • Ok, so how about if we remove the `NestedScrollView` in this layout and instead, call `setNestedScrollingEnabled` for each of the `RecycleView's`? – Anthony Cannon Feb 12 '18 at 16:27
  • It's not solution for my case but I tried it with no success. –  Feb 12 '18 at 16:29
  • Ah fair enough, sorry I couldn't be any more help! – Anthony Cannon Feb 12 '18 at 16:32
  • No prob, Thanks for your help :) –  Feb 12 '18 at 16:33