mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-09-12 23:39:45 +00:00
134 lines
4.4 KiB
Java
134 lines
4.4 KiB
Java
package org.schabi.newpipe.fragments;
|
|
|
|
import android.os.Bundle;
|
|
import android.preference.PreferenceManager;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.design.widget.TabLayout;
|
|
import android.support.v4.app.Fragment;
|
|
import android.support.v4.app.FragmentManager;
|
|
import android.support.v4.app.FragmentPagerAdapter;
|
|
import android.support.v4.view.ViewPager;
|
|
import android.support.v7.app.ActionBar;
|
|
import android.util.Log;
|
|
import android.view.LayoutInflater;
|
|
import android.view.Menu;
|
|
import android.view.MenuInflater;
|
|
import android.view.MenuItem;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
|
|
import org.schabi.newpipe.BaseFragment;
|
|
import org.schabi.newpipe.R;
|
|
import org.schabi.newpipe.fragments.subscription.SubscriptionFragment;
|
|
import org.schabi.newpipe.util.NavigationHelper;
|
|
|
|
public class MainFragment extends BaseFragment implements TabLayout.OnTabSelectedListener {
|
|
private ViewPager viewPager;
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
// Fragment's LifeCycle
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setHasOptionsMenu(true);
|
|
}
|
|
|
|
@Override
|
|
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
|
return inflater.inflate(R.layout.fragment_main, container, false);
|
|
}
|
|
|
|
@Override
|
|
protected void initViews(View rootView, Bundle savedInstanceState) {
|
|
super.initViews(rootView, savedInstanceState);
|
|
|
|
TabLayout tabLayout = rootView.findViewById(R.id.main_tab_layout);
|
|
viewPager = rootView.findViewById(R.id.pager);
|
|
|
|
/* Nested fragment, use child fragment here to maintain backstack in view pager. */
|
|
PagerAdapter adapter = new PagerAdapter(getChildFragmentManager());
|
|
viewPager.setAdapter(adapter);
|
|
viewPager.setOffscreenPageLimit(adapter.getCount());
|
|
|
|
tabLayout.setupWithViewPager(viewPager);
|
|
}
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
// Menu
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
@Override
|
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
|
super.onCreateOptionsMenu(menu, inflater);
|
|
if (DEBUG) Log.d(TAG, "onCreateOptionsMenu() called with: menu = [" + menu + "], inflater = [" + inflater + "]");
|
|
inflater.inflate(R.menu.main_fragment_menu, menu);
|
|
|
|
ActionBar supportActionBar = activity.getSupportActionBar();
|
|
if (supportActionBar != null) {
|
|
supportActionBar.setDisplayShowTitleEnabled(false);
|
|
supportActionBar.setDisplayHomeAsUpEnabled(false);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
switch (item.getItemId()) {
|
|
case R.id.action_search:
|
|
NavigationHelper.openSearchFragment(getFragmentManager(), Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("service", "0")), "");
|
|
return true;
|
|
}
|
|
return super.onOptionsItemSelected(item);
|
|
}
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
// Tabs
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
@Override
|
|
public void onTabSelected(TabLayout.Tab tab) {
|
|
viewPager.setCurrentItem(tab.getPosition());
|
|
}
|
|
|
|
@Override
|
|
public void onTabUnselected(TabLayout.Tab tab) {
|
|
}
|
|
|
|
@Override
|
|
public void onTabReselected(TabLayout.Tab tab) {
|
|
}
|
|
|
|
private class PagerAdapter extends FragmentPagerAdapter {
|
|
|
|
private int[] tabTitles = new int[]{
|
|
R.string.tab_main,
|
|
R.string.tab_subscriptions
|
|
};
|
|
|
|
PagerAdapter(FragmentManager fm) {
|
|
super(fm);
|
|
}
|
|
|
|
@Override
|
|
public Fragment getItem(int position) {
|
|
switch (position) {
|
|
case 1:
|
|
return new SubscriptionFragment();
|
|
default:
|
|
return new BlankFragment();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public CharSequence getPageTitle(int position) {
|
|
return getString(this.tabTitles[position]);
|
|
}
|
|
|
|
@Override
|
|
public int getCount() {
|
|
return this.tabTitles.length;
|
|
}
|
|
}
|
|
}
|