mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-11-05 09:36:22 +00:00
Simplify code to set tab layout position
This commit is contained in:
parent
26b29ca78d
commit
c8ffe65acf
@ -1,7 +1,15 @@
|
|||||||
package org.schabi.newpipe.fragments;
|
package org.schabi.newpipe.fragments;
|
||||||
|
|
||||||
|
import static android.widget.RelativeLayout.ABOVE;
|
||||||
|
import static android.widget.RelativeLayout.ALIGN_PARENT_BOTTOM;
|
||||||
|
import static android.widget.RelativeLayout.ALIGN_PARENT_TOP;
|
||||||
|
import static android.widget.RelativeLayout.BELOW;
|
||||||
|
import static com.google.android.material.tabs.TabLayout.INDICATOR_GRAVITY_BOTTOM;
|
||||||
|
import static com.google.android.material.tabs.TabLayout.INDICATOR_GRAVITY_TOP;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.ColorStateList;
|
import android.content.res.ColorStateList;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@ -12,10 +20,10 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.RelativeLayout;
|
import android.widget.RelativeLayout;
|
||||||
|
|
||||||
|
import androidx.annotation.ColorInt;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
import androidx.fragment.app.FragmentManager;
|
import androidx.fragment.app.FragmentManager;
|
||||||
import androidx.fragment.app.FragmentStatePagerAdapterMenuWorkaround;
|
import androidx.fragment.app.FragmentStatePagerAdapterMenuWorkaround;
|
||||||
@ -93,8 +101,6 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
|
|||||||
|
|
||||||
binding.mainTabLayout.setupWithViewPager(binding.pager);
|
binding.mainTabLayout.setupWithViewPager(binding.pager);
|
||||||
binding.mainTabLayout.addOnTabSelectedListener(this);
|
binding.mainTabLayout.addOnTabSelectedListener(this);
|
||||||
binding.mainTabLayout.setTabRippleColor(binding.mainTabLayout.getTabRippleColor()
|
|
||||||
.withAlpha(32));
|
|
||||||
|
|
||||||
setupTabs();
|
setupTabs();
|
||||||
}
|
}
|
||||||
@ -112,7 +118,8 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
|
|||||||
} else if (hasTabsChanged) {
|
} else if (hasTabsChanged) {
|
||||||
setupTabs();
|
setupTabs();
|
||||||
}
|
}
|
||||||
updateTabsPosition();
|
|
||||||
|
updateTabLayoutPosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -196,44 +203,38 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
|
|||||||
private void updateTitleForTab(final int tabPosition) {
|
private void updateTitleForTab(final int tabPosition) {
|
||||||
setTitle(tabsList.get(tabPosition).getTabName(requireContext()));
|
setTitle(tabsList.get(tabPosition).getTabName(requireContext()));
|
||||||
}
|
}
|
||||||
private void updateTabsPosition() {
|
|
||||||
|
private void updateTabLayoutPosition() {
|
||||||
final ScrollableTabLayout tabLayout = binding.mainTabLayout;
|
final ScrollableTabLayout tabLayout = binding.mainTabLayout;
|
||||||
final ViewPager viewPager = binding.pager;
|
final ViewPager viewPager = binding.pager;
|
||||||
final RelativeLayout.LayoutParams tabParams = (RelativeLayout.LayoutParams)
|
final boolean bottom = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||||
tabLayout.getLayoutParams();
|
.getBoolean(getString(R.string.main_tabs_position_key), false);
|
||||||
final RelativeLayout.LayoutParams pagerParams = (RelativeLayout.LayoutParams)
|
|
||||||
viewPager.getLayoutParams();
|
// change layout params to make the tab layout appear either at the top or at the bottom
|
||||||
if (PreferenceManager.getDefaultSharedPreferences(requireContext())
|
final var tabParams = (RelativeLayout.LayoutParams) tabLayout.getLayoutParams();
|
||||||
.getBoolean(getString(R.string.main_tabs_position_key), false)) {
|
final var pagerParams = (RelativeLayout.LayoutParams) viewPager.getLayoutParams();
|
||||||
tabParams.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
|
|
||||||
tabParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
|
tabParams.removeRule(bottom ? ALIGN_PARENT_TOP : ALIGN_PARENT_BOTTOM);
|
||||||
pagerParams.removeRule(RelativeLayout.BELOW);
|
tabParams.addRule(bottom ? ALIGN_PARENT_BOTTOM : ALIGN_PARENT_TOP);
|
||||||
pagerParams.addRule(RelativeLayout.ABOVE, R.id.main_tab_layout);
|
pagerParams.removeRule(bottom ? BELOW : ABOVE);
|
||||||
tabLayout.setSelectedTabIndicatorGravity(TabLayout.INDICATOR_GRAVITY_TOP);
|
pagerParams.addRule(bottom ? ABOVE : BELOW, R.id.main_tab_layout);
|
||||||
tabLayout.setBackgroundColor(ThemeHelper
|
tabLayout.setSelectedTabIndicatorGravity(
|
||||||
.resolveColorFromAttr(requireContext(), R.attr.colorSecondary));
|
bottom ? INDICATOR_GRAVITY_TOP : INDICATOR_GRAVITY_BOTTOM);
|
||||||
final int colorAccent = ThemeHelper.resolveColorFromAttr(
|
|
||||||
requireContext(), R.attr.colorAccent);
|
|
||||||
tabLayout.setTabRippleColor(ColorStateList.valueOf(colorAccent).withAlpha(32));
|
|
||||||
tabLayout.setTabIconTint(ColorStateList.valueOf(colorAccent));
|
|
||||||
tabLayout.setSelectedTabIndicatorColor(colorAccent);
|
|
||||||
} else {
|
|
||||||
tabParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
|
|
||||||
tabParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
|
|
||||||
pagerParams.removeRule(RelativeLayout.ABOVE);
|
|
||||||
pagerParams.addRule(RelativeLayout.BELOW, R.id.main_tab_layout);
|
|
||||||
tabLayout.setSelectedTabIndicatorGravity(TabLayout.INDICATOR_GRAVITY_BOTTOM);
|
|
||||||
tabLayout.setBackgroundColor(ThemeHelper
|
|
||||||
.resolveColorFromAttr(requireContext(), R.attr.colorPrimary));
|
|
||||||
tabLayout.setTabRippleColor(ColorStateList.valueOf(
|
|
||||||
getResources().getColor(R.color.white)).withAlpha(32));
|
|
||||||
tabLayout.setTabIconTint(ColorStateList.valueOf(
|
|
||||||
getResources().getColor(R.color.white)));
|
|
||||||
tabLayout.setSelectedTabIndicatorColor(ContextCompat
|
|
||||||
.getColor(requireContext(), R.color.white));
|
|
||||||
}
|
|
||||||
tabLayout.setLayoutParams(tabParams);
|
tabLayout.setLayoutParams(tabParams);
|
||||||
viewPager.setLayoutParams(pagerParams);
|
viewPager.setLayoutParams(pagerParams);
|
||||||
|
|
||||||
|
// change the background and icon color of the tab layout:
|
||||||
|
// service-colored at the top, app-background-colored at the bottom
|
||||||
|
tabLayout.setBackgroundColor(ThemeHelper.resolveColorFromAttr(requireContext(),
|
||||||
|
bottom ? R.attr.colorSecondary : R.attr.colorPrimary));
|
||||||
|
|
||||||
|
@ColorInt final int iconColor = bottom
|
||||||
|
? ThemeHelper.resolveColorFromAttr(requireContext(), R.attr.colorAccent)
|
||||||
|
: Color.WHITE;
|
||||||
|
tabLayout.setTabRippleColor(ColorStateList.valueOf(iconColor).withAlpha(32));
|
||||||
|
tabLayout.setTabIconTint(ColorStateList.valueOf(iconColor));
|
||||||
|
tabLayout.setSelectedTabIndicatorColor(iconColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user