From dc0a139392c33f4ebc98dec31dff898371641c7f Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 8 Apr 2024 07:45:44 +0200 Subject: [PATCH] collapse start chat button --- .../ui/ConversationsOverviewFragment.java | 1 + .../ui/ExtendedFabSizeChanger.java | 29 +++++++++++++++++++ .../siacs/conversations/ui/RecyclerViews.java | 29 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 src/main/java/eu/siacs/conversations/ui/ExtendedFabSizeChanger.java create mode 100644 src/main/java/eu/siacs/conversations/ui/RecyclerViews.java diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java b/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java index 820d3e5af..19121f225 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationsOverviewFragment.java @@ -299,6 +299,7 @@ public class ConversationsOverviewFragment extends XmppFragment { }); this.binding.list.setAdapter(this.conversationsAdapter); this.binding.list.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false)); + this.binding.list.addOnScrollListener(ExtendedFabSizeChanger.of(binding.fab)); this.touchHelper = new ItemTouchHelper(this.callback); this.touchHelper.attachToRecyclerView(this.binding.list); return binding.getRoot(); diff --git a/src/main/java/eu/siacs/conversations/ui/ExtendedFabSizeChanger.java b/src/main/java/eu/siacs/conversations/ui/ExtendedFabSizeChanger.java new file mode 100644 index 000000000..6c21f1a2d --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/ExtendedFabSizeChanger.java @@ -0,0 +1,29 @@ +package eu.siacs.conversations.ui; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; +import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton; + +public class ExtendedFabSizeChanger extends RecyclerView.OnScrollListener { + + private final ExtendedFloatingActionButton extendedFloatingActionButton; + + private ExtendedFabSizeChanger( + final ExtendedFloatingActionButton extendedFloatingActionButton) { + this.extendedFloatingActionButton = extendedFloatingActionButton; + } + + @Override + public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + if (RecyclerViews.findFirstVisibleItemPosition(recyclerView) > 0) { + extendedFloatingActionButton.shrink(); + } else { + extendedFloatingActionButton.extend(); + } + } + + public static RecyclerView.OnScrollListener of(final ExtendedFloatingActionButton fab) { + return new ExtendedFabSizeChanger(fab); + } +} diff --git a/src/main/java/eu/siacs/conversations/ui/RecyclerViews.java b/src/main/java/eu/siacs/conversations/ui/RecyclerViews.java new file mode 100644 index 000000000..cca9c36c0 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/RecyclerViews.java @@ -0,0 +1,29 @@ +package eu.siacs.conversations.ui; + +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +public final class RecyclerViews { + + private RecyclerViews() { + throw new IllegalStateException("Do not instantiate me"); + } + + public static boolean scrolledToTop(final RecyclerView recyclerView) { + final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); + if (layoutManager instanceof LinearLayoutManager linearLayoutManager) { + return linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0; + } else { + return false; + } + } + + public static int findFirstVisibleItemPosition(final RecyclerView recyclerView) { + final RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); + if (layoutManager instanceof LinearLayoutManager linearLayoutManager) { + return linearLayoutManager.findFirstVisibleItemPosition(); + } else { + return RecyclerView.NO_POSITION; + } + } +}