mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-12-24 00:50:32 +00:00
Use view binding in MainFragment.
This commit is contained in:
parent
979102a2d9
commit
910d22daa6
@ -19,12 +19,12 @@ 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;
|
||||||
import androidx.preference.PreferenceManager;
|
import androidx.preference.PreferenceManager;
|
||||||
import androidx.viewpager.widget.ViewPager;
|
|
||||||
|
|
||||||
import com.google.android.material.tabs.TabLayout;
|
import com.google.android.material.tabs.TabLayout;
|
||||||
|
|
||||||
import org.schabi.newpipe.BaseFragment;
|
import org.schabi.newpipe.BaseFragment;
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
|
import org.schabi.newpipe.databinding.FragmentMainBinding;
|
||||||
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
||||||
import org.schabi.newpipe.report.ErrorActivity;
|
import org.schabi.newpipe.report.ErrorActivity;
|
||||||
import org.schabi.newpipe.report.ErrorInfo;
|
import org.schabi.newpipe.report.ErrorInfo;
|
||||||
@ -34,15 +34,13 @@ 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.util.ThemeHelper;
|
import org.schabi.newpipe.util.ThemeHelper;
|
||||||
import org.schabi.newpipe.views.ScrollableTabLayout;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MainFragment extends BaseFragment implements TabLayout.OnTabSelectedListener {
|
public class MainFragment extends BaseFragment implements TabLayout.OnTabSelectedListener {
|
||||||
private ViewPager viewPager;
|
private FragmentMainBinding binding;
|
||||||
private SelectedTabsPagerAdapter pagerAdapter;
|
private SelectedTabsPagerAdapter pagerAdapter;
|
||||||
private ScrollableTabLayout tabLayout;
|
|
||||||
|
|
||||||
private final List<Tab> tabsList = new ArrayList<>();
|
private final List<Tab> tabsList = new ArrayList<>();
|
||||||
private TabsManager tabsManager;
|
private TabsManager tabsManager;
|
||||||
@ -90,13 +88,12 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
|
|||||||
protected void initViews(final View rootView, final Bundle savedInstanceState) {
|
protected void initViews(final View rootView, final Bundle savedInstanceState) {
|
||||||
super.initViews(rootView, savedInstanceState);
|
super.initViews(rootView, savedInstanceState);
|
||||||
|
|
||||||
tabLayout = rootView.findViewById(R.id.main_tab_layout);
|
binding = FragmentMainBinding.bind(rootView);
|
||||||
viewPager = rootView.findViewById(R.id.pager);
|
|
||||||
|
|
||||||
tabLayout.setTabIconTint(ColorStateList.valueOf(
|
binding.mainTabLayout.setTabIconTint(ColorStateList.valueOf(
|
||||||
ThemeHelper.resolveColorFromAttr(requireContext(), R.attr.colorAccent)));
|
ThemeHelper.resolveColorFromAttr(requireContext(), R.attr.colorAccent)));
|
||||||
tabLayout.setupWithViewPager(viewPager);
|
binding.mainTabLayout.setupWithViewPager(binding.pager);
|
||||||
tabLayout.addOnTabSelectedListener(this);
|
binding.mainTabLayout.addOnTabSelectedListener(this);
|
||||||
|
|
||||||
setupTabs();
|
setupTabs();
|
||||||
}
|
}
|
||||||
@ -120,8 +117,14 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
|
|||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
tabsManager.unsetSavedTabsListener();
|
tabsManager.unsetSavedTabsListener();
|
||||||
if (viewPager != null) {
|
}
|
||||||
viewPager.setAdapter(null);
|
|
||||||
|
@Override
|
||||||
|
public void onDestroyView() {
|
||||||
|
super.onDestroyView();
|
||||||
|
if (binding != null) {
|
||||||
|
binding.pager.setAdapter(null);
|
||||||
|
binding = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,19 +175,19 @@ public class MainFragment extends BaseFragment implements TabLayout.OnTabSelecte
|
|||||||
getChildFragmentManager(), tabsList);
|
getChildFragmentManager(), tabsList);
|
||||||
}
|
}
|
||||||
|
|
||||||
viewPager.setAdapter(null);
|
binding.pager.setAdapter(null);
|
||||||
viewPager.setOffscreenPageLimit(tabsList.size());
|
binding.pager.setOffscreenPageLimit(tabsList.size());
|
||||||
viewPager.setAdapter(pagerAdapter);
|
binding.pager.setAdapter(pagerAdapter);
|
||||||
|
|
||||||
updateTabsIconAndDescription();
|
updateTabsIconAndDescription();
|
||||||
updateTitleForTab(viewPager.getCurrentItem());
|
updateTitleForTab(binding.pager.getCurrentItem());
|
||||||
|
|
||||||
hasTabsChanged = false;
|
hasTabsChanged = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateTabsIconAndDescription() {
|
private void updateTabsIconAndDescription() {
|
||||||
for (int i = 0; i < tabsList.size(); i++) {
|
for (int i = 0; i < tabsList.size(); i++) {
|
||||||
final TabLayout.Tab tabToSet = tabLayout.getTabAt(i);
|
final TabLayout.Tab tabToSet = binding.mainTabLayout.getTabAt(i);
|
||||||
if (tabToSet != null) {
|
if (tabToSet != null) {
|
||||||
final Tab tab = tabsList.get(i);
|
final Tab tab = tabsList.get(i);
|
||||||
tabToSet.setIcon(tab.getTabIconRes(requireContext()));
|
tabToSet.setIcon(tab.getTabIconRes(requireContext()));
|
||||||
|
Loading…
Reference in New Issue
Block a user