Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestedScrollView (NSV) in CoordinatorLayout (CL): NSV Not at Top When Loaded

I am using an NSV in a CL for the ability to have the toolbar compress when the NSV scrolls down. The problem that I am having is that my NSV is not scrolled to the top when it loads, instead, it is offset from the top of the NSV by quite a margin (I am not certain where this spacing is coming from, it's not in the layout).

Please take a look at the screen captures, the first one shows how the NSV loads and you can clearly see the NSV has scrolled down quite a bit from the top by comparing the second (when I scroll the NSV to the top manually):

When NSV loads, it's not at the top NSV scrolled to the top manually for comparison sake

I did some updates to this layout and it caused this to occur, previously, it loaded at the top without issue. However, I did not add any spacing that should have caused this.

Here is the layout I'm using for this:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/cl_goal_detail"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/abl_goal_detail"
        android:layout_width="match_parent"
        android:layout_height="144dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_goal_detail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingBottom="@dimen/content_space_double"
            app:collapsedTitleTextAppearance="@style/title.dark"
            app:expandedTitleTextAppearance="@style/display3.plus.dark"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_goal_detail"
                style="@style/toolbar"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nsv_goal_detail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/content_space_half"
        android:paddingLeft="@dimen/content_space_half"
        android:paddingRight="@dimen/content_space_half"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <FrameLayout
            android:id="@+id/container_goal_detail"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill_vertical"/>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

Any ideas would be appreciated!

like image 931
AutoM8R Avatar asked Jul 05 '15 20:07

AutoM8R


3 Answers

OK! After a solid DAY of debugging every single component of my layout and Fragment I identified what I believe is a bug.

First, the issue: Turns out that having elements in your NSV's child view that change visibility to View.GONE upon runtime are causing the list to scroll down. I noticed that the list scrolls to just above the element where the visibility was toggled (including any margins set on the view).

Second, the fix: I fixed this issue by setting all the views to have android:visibility="gone" in the xml layout, then, I toggle each view's visibility as needed. Previously, the views were visible by default and then I worked from there. I just needed to change my logic to start with them all GONE, not terribly difficult.

I assume this works because the views you are going to hide at runtime do not form a part of the overall height calculation when the NSV is created in onCreateView(). Once the fragment progresses past onCreateView() it's safe to dynamically change the views, however, if the views are calculated as part as the height in onCreateView() and THEN hidden with View.GONE, measurements go wonky and you end up with a list scrolled down significantly.

like image 123
AutoM8R Avatar answered Oct 23 '22 15:10

AutoM8R


Have you tried adding below line in your viewgroup i.e. FrameLayout in your case

android:descendantFocusability="blocksDescendants"

I think this will also work for you.

If not try it adding in NSV.

like image 41
nadafafif Avatar answered Oct 23 '22 14:10

nadafafif


In my case, there was an EditText near the bottom of my scrolling content that was grabbing focus. Since NestedScrollView does some weird layout stuff, the focused view didn't scroll to the top when the activity started, so the real cause was not readily apparent. Adding this to the NestedScrollView's child layout fixed it for me:

android:focusableInTouchMode="true"
like image 1
Jarett Millard Avatar answered Oct 23 '22 14:10

Jarett Millard