make main page tabs scrollable
This commit is contained in:
parent
0afc8005d0
commit
33caad4690
3 changed files with 87 additions and 2 deletions
|
@ -30,6 +30,7 @@ import org.schabi.newpipe.settings.tabs.Tab;
|
||||||
import org.schabi.newpipe.settings.tabs.TabsManager;
|
import org.schabi.newpipe.settings.tabs.TabsManager;
|
||||||
import org.schabi.newpipe.util.NavigationHelper;
|
import org.schabi.newpipe.util.NavigationHelper;
|
||||||
import org.schabi.newpipe.util.ServiceHelper;
|
import org.schabi.newpipe.util.ServiceHelper;
|
||||||
|
import org.schabi.newpipe.views.ScrollableTabLayout;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -37,7 +38,7 @@ import java.util.List;
|
||||||
public class MainFragment extends BaseFragment implements TabLayout.OnTabSelectedListener {
|
public class MainFragment extends BaseFragment implements TabLayout.OnTabSelectedListener {
|
||||||
private ViewPager viewPager;
|
private ViewPager viewPager;
|
||||||
private SelectedTabsPagerAdapter pagerAdapter;
|
private SelectedTabsPagerAdapter pagerAdapter;
|
||||||
private TabLayout tabLayout;
|
private ScrollableTabLayout tabLayout;
|
||||||
|
|
||||||
private List<Tab> tabsList = new ArrayList<>();
|
private List<Tab> tabsList = new ArrayList<>();
|
||||||
private TabsManager tabsManager;
|
private TabsManager tabsManager;
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
package org.schabi.newpipe.views;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import com.google.android.material.tabs.TabLayout;
|
||||||
|
import com.google.android.material.tabs.TabLayout.Tab;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A TabLayout that is scrollable when tabs exceed its width.
|
||||||
|
*/
|
||||||
|
public class ScrollableTabLayout extends TabLayout {
|
||||||
|
private static final String TAG = ScrollableTabLayout.class.getSimpleName();
|
||||||
|
|
||||||
|
public ScrollableTabLayout(Context context) {
|
||||||
|
super(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScrollableTabLayout(Context context, AttributeSet attrs) {
|
||||||
|
super(context, attrs);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ScrollableTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||||
|
super(context, attrs, defStyleAttr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onAttachedToWindow() {
|
||||||
|
super.onAttachedToWindow();
|
||||||
|
|
||||||
|
setTabMode(TabLayout.MODE_FIXED);
|
||||||
|
resetMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||||
|
super.onSizeChanged(w, h, oldw, oldh);
|
||||||
|
|
||||||
|
resetMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addTab(@NonNull Tab tab, int position, boolean setSelected) {
|
||||||
|
super.addTab(tab, position, setSelected);
|
||||||
|
|
||||||
|
resetMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeTabAt(int position) {
|
||||||
|
super.removeTabAt(position);
|
||||||
|
|
||||||
|
resetMode();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resetMode() {
|
||||||
|
if (getTabCount() == 0 || getTabAt(0).view == null) return;
|
||||||
|
setTabMode(TabLayout.MODE_FIXED);
|
||||||
|
|
||||||
|
int layoutWidth = getWidth();
|
||||||
|
int minimumWidth = ((View) getTabAt(0).view).getMinimumWidth();
|
||||||
|
if (minimumWidth * getTabCount() > layoutWidth) {
|
||||||
|
setTabMode(TabLayout.MODE_SCROLLABLE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int actualWidth = 0;
|
||||||
|
for (int i = 0; i < getTabCount(); ++i) {
|
||||||
|
if (getTabAt(i).view == null) return;
|
||||||
|
actualWidth += ((View) getTabAt(i).view).getWidth();
|
||||||
|
if (actualWidth > layoutWidth) {
|
||||||
|
setTabMode(TabLayout.MODE_SCROLLABLE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,12 +6,13 @@
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
|
|
||||||
<com.google.android.material.tabs.TabLayout
|
<org.schabi.newpipe.views.ScrollableTabLayout
|
||||||
android:id="@+id/main_tab_layout"
|
android:id="@+id/main_tab_layout"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_alignParentTop="true"
|
android:layout_alignParentTop="true"
|
||||||
android:background="?attr/colorPrimary"
|
android:background="?attr/colorPrimary"
|
||||||
|
app:tabMinWidth="60dp"
|
||||||
app:tabGravity="fill"/>
|
app:tabGravity="fill"/>
|
||||||
|
|
||||||
<androidx.viewpager.widget.ViewPager
|
<androidx.viewpager.widget.ViewPager
|
||||||
|
|
Loading…
Reference in a new issue