В MainActivity, при выборе определённого меню (из NavigationView), подгружаются определённые табы с фрагментами. (ManagerFragment и PlanFragment)
При переключении в меню на пункт 2 (tenders, см. Listener) Меняются только имена табов, без изменения фрагмента. Подскажите, в каком направлении копать.(Жизненный цикл фрагмента?) Изначально хотел реализовать всё это через дополнительные Activity, но хочется попробовать сделать всё в одном Activity.
MainActivity:
package ru.alexbykov.sailesstat;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import ru.alexbykov.sailesstat.statistic.adapter.TabsFragmentAdapter;
public class MainActivity extends AppCompatActivity {
private static final int LAYOUT = R.layout.activity_main;
DrawerLayout drawerLayout;
Toolbar toolbar;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppDefault);
super.onCreate(savedInstanceState);
setContentView(LAYOUT);
setupToolBar();
setupNavigationView();
setupPlanTabs();
}
private void setupToolBar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.app_name);
}
private void setupNavigationView() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle togle =
new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.view_navigation_open,
R.string.view_navigation_close);
// drawerLayout.setDrawerListener(togle);
togle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
drawerLayout.closeDrawers();
switch (item.getItemId()) {
case R.id.plan:
setupPlanTabs();
break;
case R.id.tenders:
setupTendersTabs();
break;
case R.id.shipments:
setupShipmentsTabs();
break;
}
return true;
}
});
}
private void setupPlanTabs() {
viewPager = (ViewPager) findViewById(R.id.viewPager);
TabsFragmentAdapter adapter = new TabsFragmentAdapter(this, getSupportFragmentManager(), true, false, false);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
private void setupTendersTabs() {
viewPager = (ViewPager) findViewById(R.id.viewPager);
TabsFragmentAdapter adapter = new TabsFragmentAdapter(this, getSupportFragmentManager(), false, true, false);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
private void setupShipmentsTabs() {
viewPager = (ViewPager) findViewById(R.id.viewPager);
TabsFragmentAdapter adapter = new TabsFragmentAdapter(this, getSupportFragmentManager(), false, false, true);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
}
TabFragmentAdapter:
package ru.alexbykov.sailesstat.statistic.adapter;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.HashMap;
import java.util.Map;
import ru.alexbykov.sailesstat.statistic.fragments.AbstractTabFragment;
import ru.alexbykov.sailesstat.statistic.fragments.fragmentsTenders.InWorkTendersFragment;
import ru.alexbykov.sailesstat.statistic.fragments.fragmentsTenders.StatTendersFragment;
import ru.alexbykov.sailesstat.statistic.fragments.fragmentsTheSale.ManagersFragment;
import ru.alexbykov.sailesstat.statistic.fragments.fragmentsTheSale.PlanFragment;
public class TabsFragmentAdapter extends FragmentPagerAdapter {
//for use strings
private Context context;
private Map<Integer, AbstractTabFragment> tabs;
private boolean isTheSale;
private boolean isTenders;
private boolean isShipments;
public TabsFragmentAdapter(Context context, FragmentManager fm, boolean isTheSale, boolean isTenders, boolean isShipments) {
super(fm);
this.isTheSale = isTheSale;
this.isTenders = isTenders;
this.isShipments = isShipments;
this.context = context;
initTabs();
}
public TabsFragmentAdapter(Context context, FragmentManager fm) {
super(fm);
this.context = context;
initTabs();
}
@Override
public Fragment getItem(int position) {
return tabs.get(position);
}
@Override
public int getCount() {
return tabs.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabs.get(position).getTitle();
}
private void initTabs() {
tabs = new HashMap<>();
if (isTheSale) {
tabs.put(0, PlanFragment.getInstance(context));
tabs.put(1, ManagersFragment.getInstance(context));
}
else if (isTenders) {
tabs.put(0, InWorkTendersFragment.getInstance(context));
tabs.put(1, StatTendersFragment.getInstance(context));
}
else if (isShipments){
}
}
}
AbstractTabFragment
package ru.alexbykov.sailesstat.statistic.fragments;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;
public class AbstractTabFragment extends Fragment {
private String title;
protected Context context;
protected View view;
public void setTitle(String title) {
this.title = title;
}
public void setContext(Context context) {
this.context=context;
}
public String getTitle() {
return title;
}
}
ManagerFragment
package ru.alexbykov.sailesstat.statistic.fragments.fragmentsTheSale;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import ru.alexbykov.sailesstat.R;
import ru.alexbykov.sailesstat.statistic.fragments.AbstractTabFragment;
/**
* A simple {@link Fragment} subclass.
*/
public class ManagersFragment extends AbstractTabFragment {
private static final int LAYOUT = R.layout.fragment_managers;
public static ManagersFragment getInstance(Context context){
ManagersFragment fragment = new ManagersFragment();
fragment.setContext(context);
fragment.setTitle(context.getString(R.string.tab_managers_fragment));
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(LAYOUT, container, false);
return view;
}
}
PlanFragment
package ru.alexbykov.sailesstat.statistic.fragments.fragmentsTheSale;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import ru.alexbykov.sailesstat.R;
import ru.alexbykov.sailesstat.statistic.fragments.AbstractTabFragment;
/**
* A simple {@link Fragment} subclass.
*/
public class PlanFragment extends AbstractTabFragment {
private static final int LAYOUT = R.layout.fragment_plan;
public static PlanFragment getInstance(Context context) {
/* Bundle bundle = new Bundle();
fragment.setArguments(bundle);*/
PlanFragment fragment = new PlanFragment();
fragment.setContext(context);
fragment.setTitle(context.getString(R.string.tab_plan_stat_fragment));
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(LAYOUT, container, false);
return view;
}
}
InWorkTendersFragment
package ru.alexbykov.sailesstat.statistic.fragments.fragmentsTenders;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import ru.alexbykov.sailesstat.R;
import ru.alexbykov.sailesstat.statistic.fragments.AbstractTabFragment;
/**
* A simple {@link Fragment} subclass.
*/
public class InWorkTendersFragment extends AbstractTabFragment {
private static final int LAYOUT = R.layout.fragment_in_work_tenders;
public static InWorkTendersFragment getInstance(Context context) {
InWorkTendersFragment fragment = new InWorkTendersFragment();
fragment.setContext(context);
fragment.setTitle(context.getString(R.string.tab_tenders_in_work_fragment));
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(LAYOUT, container, false);
return view;
}
StatTendersFragment
package ru.alexbykov.sailesstat.statistic.fragments.fragmentsTenders;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import ru.alexbykov.sailesstat.R;
import ru.alexbykov.sailesstat.statistic.fragments.AbstractTabFragment;
/**
* A simple {@link Fragment} subclass.
*/
public class StatTendersFragment extends AbstractTabFragment {
private static final int LAYOUT = R.layout.fragment_stat_tenders;
public static StatTendersFragment getInstance(Context context) {
StatTendersFragment fragment = new StatTendersFragment();
fragment.setContext(context);
fragment.setTitle(context.getString(R.string.tab_tenders_stat_fragment));
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(LAYOUT, container, false);
return view;
}
}