0

I'm trying to act on Scroll Events inside(!) a (Nested)Scrollview.

I have an Item inside a Scrollview that i want to "peek" (translate to visible screen area) for a few sec if it's not initially visible in the scrollview/screen. But as soon as the container is scrolled, i want to hide it (translate back to origin).

Therefore i'm trying to use a CoordinatorLayout Behavior to not tightly couple the scrollView and my (custom) View.

Following the article, I tried to write a custom behavior, but figured out, that I cannot apply this Behavior on my view, as it's not a child of the CoordinatorLayout. If i have to set it on a direct child, how can i then act on the great-grand child?

How could this be achieved?

Layout looks sth like that:

<CoordinatorLayout>
   <NestedScrollView>
       <LinearLayout>
          ...
          <MyView/>
       <LinearLayout/>
   </NestedScrollView>
</CoordinatorLayout>

The behaviour is pretty straight forward:

public class MyBehaviour extends CoordinatorLayout.Behavior{

    ...

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int
            nestedScrollAxes) {
        return true;
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dxConsumed, int dyConsumed, int
            dxUnconsumed, int dyUnconsumed) {
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

        // act on MyView
    }
}
cwiesner
  • 732
  • 6
  • 21

1 Answers1

0

Following the article, I tried to write a custom behavior, but figured out, that I cannot apply this Behavior on my view, as it's not a child of the CoordinatorLayout.

This is not exactly true. The case where view reacts to CoordinatorLayout Behaviour changes can be emulated. Here is a quick example:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="1700px"
                    android:text="Hello World!"/>

                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|end"
                    android:layout_margin="@dimen/fab_margin"
                    android:src="@android:drawable/ic_dialog_email"/>

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

        </android.support.v4.widget.NestedScrollView>

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

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

As you can see the FloatingActionButton is inside

CoordinatorLayout -> CoordinatorLayout -> NestedScrollView -> CoordinatorLayout

In such layout FAB reacts to the Snackbar showing:

enter image description here enter image description here

However, the problem here is combining with layout heights, therefore I strongly suggest you to rethink your design.

R. Zagórski
  • 18,351
  • 5
  • 59
  • 86
  • i cannot confirm your answer. i uploaded a demo project here: https://github.com/chriswiesner/CoordinatorLayoutNestedScrolling if i put "MyView" as a child of the root coordinator layout it works fine. but as soon as it's in the nested coordinatorLayout it doesn't work in the meantime i solved my issue without coordinatorlayout and passing a scrollListener to MyView. this couples again my views, which i'd like to avoid. so i'm still curious how this could be achieved with CoordinatorLayout – cwiesner Sep 01 '16 at 11:41