В моем приложении у меня есть FloatingActionButton внутри NestedScrollView с OnScrollChangeListener
, чтобы создать эффект параллакса для изображения (как в приложении Википедии) и перемещать FAB, чтобы он не выглядел странно. Он отлично работает, когда пользователь прокручивает и отпускает, но FAB переходит к нижней части экрана, когда пользователь прокручивает и удерживает палец. Вот подарок, показывающий, что происходит.
Вот фрагмент кода со слушателем прокрутки:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !media_type.equalsIgnoreCase("other")) {
NestedScrollView main_layout = findViewById(R.id.main_view);
main_layout.setOnScrollChangeListener((View.OnScrollChangeListener) (view, i, scrollY, i2, oldScrollY) -> {
if (scrollY > 10) {
shareButton.hide();
} else if (scrollY < 10) {
shareButton.show();
}
scroll = scrollY;
imageView.setTranslationY(scrollY * 0.5f);
shareButton.setTranslationY(scrollY * -0.5f);
findViewById(R.id.play_button).setTranslationY(scrollY * 0.5f);
});
}
А вот мой макет (я оставил самых важных детей):
<?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:context="....MainActivity"
android:background="@color/colorPrimary"
android:id="@+id/coordinator">
.
.
.
<android.support.v4.widget.NestedScrollView
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:id="@+id/frame">
<ImageView
android:id="@+id/image_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transitionName="img" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/video_play_icon"
android:layout_gravity="center"
android:id="@+id/play_icon"
android:visibility="gone"/>
</FrameLayout>
.
.
.
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/share_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@drawable/outline_share_white_24"
app:fabSize="normal"
app:layout_anchor="@+id/image_main"
app:layout_anchorGravity="bottom|right|end" />
.
.
.
</android.support.design.widget.CoordinatorLayout>
Почему код работает только для быстрого переключения?
РЕДАКТИРОВАТЬ: эта ошибка также происходит без строки shareButton.setTranslationY(scrollY * -0.5f);
РЕДАКТИРОВАТЬ 2: когда я устанавливаю layout_anchor
в main_view
(NestedScrollLayout), проблема не возникает, но, очевидно, FAB имеет плохую позицию, поэтому я не оставлю это так.