Reputation: 952
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 } }
Upvotes: 1
Views: 1382
Reputation: 20268
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:
However, the problem here is combining with layout heights, therefore I strongly suggest you to rethink your design.
Upvotes: 0