summaryrefslogtreecommitdiff
path: root/ui/common/src
diff options
context:
space:
mode:
Diffstat (limited to 'ui/common/src')
-rw-r--r--ui/common/src/main/java/de/danoeh/antennapod/ui/common/PagedToolbarFragment.java48
-rw-r--r--ui/common/src/main/res/layout/pager_fragment.xml31
2 files changed, 79 insertions, 0 deletions
diff --git a/ui/common/src/main/java/de/danoeh/antennapod/ui/common/PagedToolbarFragment.java b/ui/common/src/main/java/de/danoeh/antennapod/ui/common/PagedToolbarFragment.java
new file mode 100644
index 000000000..118d2ffb2
--- /dev/null
+++ b/ui/common/src/main/java/de/danoeh/antennapod/ui/common/PagedToolbarFragment.java
@@ -0,0 +1,48 @@
+package de.danoeh.antennapod.ui.common;
+
+import androidx.annotation.NonNull;
+import androidx.appcompat.widget.Toolbar;
+import androidx.fragment.app.Fragment;
+import androidx.viewpager2.widget.ViewPager2;
+
+/**
+ * Fragment with a ViewPager where the displayed items influence the top toolbar's menu.
+ * All items share the same general menu items and are just allowed to show/hide them.
+ */
+public abstract class PagedToolbarFragment extends Fragment {
+ private Toolbar toolbar;
+ private ViewPager2 viewPager;
+
+ /**
+ * Invalidate the toolbar menu if the current child fragment is visible.
+ * @param child The fragment to invalidate
+ */
+ public void invalidateOptionsMenuIfActive(@NonNull Fragment child) {
+ Fragment visibleChild = getChildFragmentManager().findFragmentByTag("f" + viewPager.getCurrentItem());
+ if (visibleChild == child) {
+ visibleChild.onPrepareOptionsMenu(toolbar.getMenu());
+ }
+ }
+
+ protected void setupPagedToolbar(Toolbar toolbar, ViewPager2 viewPager) {
+ this.toolbar = toolbar;
+ this.viewPager = viewPager;
+
+ toolbar.setOnMenuItemClickListener(item -> {
+ Fragment child = getChildFragmentManager().findFragmentByTag("f" + viewPager.getCurrentItem());
+ if (child != null) {
+ return child.onOptionsItemSelected(item);
+ }
+ return false;
+ });
+ viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
+ @Override
+ public void onPageSelected(int position) {
+ Fragment child = getChildFragmentManager().findFragmentByTag("f" + position);
+ if (child != null) {
+ child.onPrepareOptionsMenu(toolbar.getMenu());
+ }
+ }
+ });
+ }
+}
diff --git a/ui/common/src/main/res/layout/pager_fragment.xml b/ui/common/src/main/res/layout/pager_fragment.xml
new file mode 100644
index 000000000..ea007892a
--- /dev/null
+++ b/ui/common/src/main/res/layout/pager_fragment.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout
+ 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="match_parent"
+ android:orientation="vertical">
+
+ <androidx.appcompat.widget.Toolbar
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:minHeight="?attr/actionBarSize"
+ android:theme="?attr/actionBarTheme"
+ app:navigationIcon="?homeAsUpIndicator"
+ android:id="@+id/toolbar"/>
+
+ <com.google.android.material.tabs.TabLayout
+ android:id="@+id/sliding_tabs"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:background="?android:attr/windowBackground"
+ app:tabBackground="?attr/selectableItemBackground"
+ app:tabMode="fixed"
+ app:tabGravity="fill"/>
+
+ <androidx.viewpager2.widget.ViewPager2
+ android:id="@+id/viewpager"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"/>
+
+</LinearLayout>