diff options
author | ByteHamster <info@bytehamster.com> | 2022-02-23 00:22:51 +0100 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2022-02-26 19:44:17 +0100 |
commit | 7451da112145f96ecddc314eea7b90fcb03737dd (patch) | |
tree | 437ad8df931d422eb0c5deebf19d6a9e93c0310c /ui/common | |
parent | 0d7555da8c291457cd8fe7b97036fd05c515bbd2 (diff) | |
download | AntennaPod-7451da112145f96ecddc314eea7b90fcb03737dd.zip |
Move statistics screens to new module
Diffstat (limited to 'ui/common')
-rw-r--r-- | ui/common/build.gradle | 2 | ||||
-rw-r--r-- | ui/common/src/main/java/de/danoeh/antennapod/ui/common/PagedToolbarFragment.java | 48 | ||||
-rw-r--r-- | ui/common/src/main/res/layout/pager_fragment.xml | 31 |
3 files changed, 81 insertions, 0 deletions
diff --git a/ui/common/build.gradle b/ui/common/build.gradle index bacb9f84c..ff4bf83cf 100644 --- a/ui/common/build.gradle +++ b/ui/common/build.gradle @@ -6,4 +6,6 @@ apply from: "../../common.gradle" dependencies { annotationProcessor "androidx.annotation:annotation:$annotationVersion" implementation "androidx.appcompat:appcompat:$appcompatVersion" + implementation "androidx.viewpager2:viewpager2:$viewPager2Version" + implementation "com.google.android.material:material:$googleMaterialVersion" } 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> |