Hide tab layout in detail fragment when there is no space

This commit is contained in:
Stypox 2021-01-14 13:18:34 +01:00
parent e840d42fb9
commit 71d92c8d1b
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
1 changed files with 51 additions and 4 deletions

View File

@ -10,6 +10,8 @@ import android.content.SharedPreferences;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.database.ContentObserver; import android.database.ContentObserver;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
@ -19,6 +21,7 @@ import android.os.Looper;
import android.provider.Settings; import android.provider.Settings;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
@ -157,6 +160,7 @@ public final class VideoDetailFragment
@AttrRes @NonNull final List<Integer> tabIcons = new ArrayList<>(); @AttrRes @NonNull final List<Integer> tabIcons = new ArrayList<>();
@StringRes @NonNull final List<Integer> tabContentDescriptions = new ArrayList<>(); @StringRes @NonNull final List<Integer> tabContentDescriptions = new ArrayList<>();
private boolean tabSettingsChanged = false; private boolean tabSettingsChanged = false;
private int lastAppBarVerticalOffset = Integer.MAX_VALUE; // prevents useless updates
@State @State
protected int serviceId = Constants.NO_SERVICE_ID; protected int serviceId = Constants.NO_SERVICE_ID;
@ -575,6 +579,7 @@ public final class VideoDetailFragment
ThemeHelper.resolveResourceIdFromAttr(requireContext(), R.attr.ic_expand_more)); ThemeHelper.resolveResourceIdFromAttr(requireContext(), R.attr.ic_expand_more));
binding.detailSecondaryControlPanel.setVisibility(View.GONE); binding.detailSecondaryControlPanel.setVisibility(View.GONE);
} }
updateTabLayoutVisibility();
} }
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////
@ -638,6 +643,14 @@ public final class VideoDetailFragment
binding.detailControlsBackground.setOnTouchListener(getOnControlsTouchListener()); binding.detailControlsBackground.setOnTouchListener(getOnControlsTouchListener());
binding.detailControlsPopup.setOnTouchListener(getOnControlsTouchListener()); binding.detailControlsPopup.setOnTouchListener(getOnControlsTouchListener());
binding.appBarLayout.addOnOffsetChangedListener((layout, verticalOffset) -> {
// prevent useless updates to tab layout visibility if nothing changed
if (verticalOffset != lastAppBarVerticalOffset) {
lastAppBarVerticalOffset = verticalOffset;
updateTabLayoutVisibility();
}
});
setupBottomPlayer(); setupBottomPlayer();
if (!PlayerHolder.bound) { if (!PlayerHolder.bound) {
setHeightThumbnail(); setHeightThumbnail();
@ -901,6 +914,10 @@ public final class VideoDetailFragment
}); });
} }
/*//////////////////////////////////////////////////////////////////////////
// Tabs
//////////////////////////////////////////////////////////////////////////*/
private void initTabs() { private void initTabs() {
if (pageAdapter.getCount() != 0) { if (pageAdapter.getCount() != 0) {
selectedTabTag = pageAdapter.getItemTitle(binding.viewPager.getCurrentItem()); selectedTabTag = pageAdapter.getItemTitle(binding.viewPager.getCurrentItem());
@ -931,16 +948,14 @@ public final class VideoDetailFragment
} }
pageAdapter.notifyDataSetUpdate(); pageAdapter.notifyDataSetUpdate();
if (pageAdapter.getCount() < 2) { if (pageAdapter.getCount() >= 2) {
binding.tabLayout.setVisibility(View.GONE);
} else {
final int position = pageAdapter.getItemPositionByTitle(selectedTabTag); final int position = pageAdapter.getItemPositionByTitle(selectedTabTag);
if (position != -1) { if (position != -1) {
binding.viewPager.setCurrentItem(position); binding.viewPager.setCurrentItem(position);
} }
binding.tabLayout.setVisibility(View.VISIBLE);
updateTabIconsAndContentDescriptions(); updateTabIconsAndContentDescriptions();
} }
updateTabLayoutVisibility();
} }
/** /**
@ -994,8 +1009,40 @@ public final class VideoDetailFragment
} }
} }
public void updateTabLayoutVisibility() {
if (pageAdapter.getCount() < 2) {
binding.tabLayout.setVisibility(View.GONE);
} else {
binding.tabLayout.post(() -> {
if (getContext() != null) {
final Rect pagerHitRect = new Rect();
binding.viewPager.getHitRect(pagerHitRect);
final Point displaySize = new Point();
Objects.requireNonNull(ContextCompat.getSystemService(getContext(),
WindowManager.class)).getDefaultDisplay().getSize(displaySize);
final int viewPagerVisibleHeight = displaySize.y - pagerHitRect.top;
// see TabLayout.DEFAULT_HEIGHT
final float tabLayoutHeight = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics());
if (viewPagerVisibleHeight > tabLayoutHeight * 2) {
// no translation at all when viewPagerVisibleHeight > tabLayout.height * 3
binding.tabLayout.setTranslationY(
Math.max(0, tabLayoutHeight * 3 - viewPagerVisibleHeight));
binding.tabLayout.setVisibility(View.VISIBLE);
} else {
binding.tabLayout.setVisibility(View.GONE);
}
}
});
}
}
public void scrollToTop() { public void scrollToTop() {
binding.appBarLayout.setExpanded(true, true); binding.appBarLayout.setExpanded(true, true);
updateTabLayoutVisibility();
} }
/*////////////////////////////////////////////////////////////////////////// /*//////////////////////////////////////////////////////////////////////////