Fragment in TabLayout only load when user slide Android - android

Hi I'm making an app with A fragment and few child fragment inside it using tablayout and viewpager. The problem is all my child fragment (from Tablayout) always execute (load all the code inside whenever user click the parent fragment. How to get the child fragment only load whenever the the user slide to it (for example I have 2 tab, content in tab 2 only load when user slide to tab 2)
Here is my parent fragment code
public class ManageEventFragment extends Fragment {
public ManageEventFragment() {
// Required empty public constructor
}
TabLayout tabLayout;
ViewPager viewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View viewFragment = inflater.inflate(R.layout.fragment_manage_event, container, false);
viewPager = (ViewPager) viewFragment.findViewById(R.id.viewPager);
tabLayout = (TabLayout) viewFragment.findViewById(R.id.tabLayout);
viewPager.setAdapter(new CustomAdapter(getChildFragmentManager(), getContext()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
return viewFragment;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// tabLayout.setupWithViewPager(viewPager);
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
});
if (ViewCompat.isLaidOut(tabLayout)) {
tabLayout.setupWithViewPager(viewPager);
} else {
tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
tabLayout.setupWithViewPager(viewPager);
tabLayout.removeOnLayoutChangeListener(this);
}
});
}
super.onActivityCreated(savedInstanceState);
}
//TabLayout and ViewPager class
private class CustomAdapter extends FragmentPagerAdapter {
private String fragments[] = {"Edit Event", "Create Event"};
public CustomAdapter(FragmentManager fragmentManager, Context context) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new EditEventFragment();
case 1:
return new CreateEventFragment();
default:
return null;
}
}
#Override
public int getCount() {
return fragments.length;
}
#Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
}
And my child fragment code. I want these code only execute whenever the user slide to the child tab. Any help is much appreciate, Thanks
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Initializing our listEvents list
listEvents = new ArrayList();
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
adapter = new EventAdapterEdit(getContext(),listEvents);
recyclerView.setAdapter(adapter);
requestQueue = Volley.newRequestQueue(getContext());
adapter = new EventAdapterEdit(getContext(),listEvents);
recyclerView.setAdapter(adapter);
getEventDetailRespond(requestQueue);

#Override public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
}
you could use above method within your fragment that is within your ViewPager Adapter
if(isVisibleToUser){//dosomething when the fragment is visible}
else{//dosomething else.}
be aware to do not initialize views there or anything rather, init your views within onViewCreated and call the method that you wanna execute on setUserVisibleHint. another ugly way is to add a scroll listener to your ViewPager and get the current item position and trigger an action that is within the fragment. to get the fragment from the ViewPager Adapter you can do such :
MyFragment frag = (MyFragment) pager.getAdapter().instantiateItem(pager, position);
then you could call a method that is within MyFragment

The Viewpager by default loads the adjacent fragment to ensure make the app smooth, so that when the user swipe to the fragments (already loaded) it is there. To change is default behavior, use
viewpager.setOffscreenPageLimit(int limit) where limit is how many fragment next to the one you are on will be preloaded.
Hope this helps

Related

ViewPager with FragmentStatePagerAdapter keeps recreating deleted fragments

Need a help with dynamic ViewPager with EditText (straggling to do a multiple tabbed editor).
Here are problems I faced:
e. g. I create 3 pages and put some text, so I have
page0 - "a"
page1 - "b"
page2 - "c"
Adding works normally here, removing page0 leads not to "a" "c" but to "a" "b" left, removing page0 leads to "a" "b" instead of "b" "c" as well, removing page2 is ok. So it seems like pager remembers text of removed page n and moves it to page n+1, text from page n+1 to n+2 and so on
same pages with the same text, but now I remove page1 and page2 (the order doesn't matter) so text "a" left. Adding two new pages will recreate "b" and "c", next pages would be blank as they should be.
if I remove just page1 and add new page, text "c" will appear.
I've been here, here and here, no help.
Tried to follow getItem(int position) calls
Also searched ViewPager and FragmentStatePagerAdapter sources, it looks like it has smth to do with instantiateItem and/or destroyItem but I have no clue what exactly.
Here is my code:
Adapter
public class PagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<Fragment> fragmentsList = new ArrayList<>();
private ArrayList<String> titlesList = new ArrayList<>();
private TabLayout layout;
public PagerAdapter(FragmentManager manager, Context context, ViewPager pager, TabLayout layout) {
super(manager);
this.layout = layout;
}
#Override
public Fragment getItem(int position) {
return fragmentsList.get(position);
}
#Override
public int getItemPosition(#NonNull Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
return fragmentsList.size();
}
#Override
public CharSequence getPageTitle(int position) {
return titlesList.get(position);
}
public void addFrag(Fragment f, String s) {
fragmentsList.add(f);
titlesList.add(s);
}
public void removeFragment(int position) {
if (layout.getChildCount() > 0)
layout.removeTabAt(position);
Fragment fragment = fragmentsList.get(position);
fragmentsList.remove(fragment);
titlesList.remove(position);
FragmentManager manager = fragment.getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(fragment);
trans.commit();
notifyDataSetChanged();
}
}
RootFragment
public class RootFragment extends Fragment {
private TabLayout layout;
private ViewPager pager;
private PagerAdapter adapter;
private int currentTab;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.root_fragment, container, false);
pager = view.findViewById(R.id.viewpager);
layout = view.findViewById(R.id.sliding_tabs);
adapter = new PagerAdapter(getFragmentManager(), getActivity(), pager, layout);
pager.setAdapter(adapter);
layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
currentTab = tab.getPosition();
pager.setCurrentItem(currentTab);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
public void addTab(String title) {
Bundle bundle = new Bundle();
bundle.putString("data", title);
LeafFragment leafFragment = new LeafFragment();
leafFragment.setArguments(bundle);
adapter.addFrag(leafFragment, title);
adapter.notifyDataSetChanged();
layout.setupWithViewPager(pager);
currentTab = adapter.getCount() - 1;
pager.setCurrentItem(currentTab);
}
public void deleteTab(int position) {
adapter.removeFragment(position);
}
}
Thanks and sorry for my English.
FragmentStatePagerAdapter already stores a list of Fragments, you don't need (and potentially, you should not) implement your own list while using it. Simply:
public class MyAdapter extends FragmentStatePagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return 3;
}
#Override
public Fragment getItem(int position) {
//Simply create a fragment based on position here
}
}
Second, the unfortunate truth is, removing/reordering fragments in a FragmentStatePagerAdapter appears to have still open bugs as here:
https://issuetracker.google.com/issues/36956111
My suggestion is to simply re-create the adapter if you need to delete fragments from the adapter.

I want to load fragment when i click on tab of TabLayout or move by viewpager

I have multiple fragment and all are show some news
Problem is that when I run my app all fragment load and get all fragment data from server but I want to get data when I move on that particular fragment so please help me
My Code is below
MainActivity.Java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setToolbar();
initView();
}
private void initView() {
TabLayout tabLayout=(TabLayout)findViewById(R.id.tablayout);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
addTabs(tabLayout,viewPager);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
private void addTabs(TabLayout tabLayout, final ViewPager viewPager) {
final PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager(), 5);
tabLayout.addTab(tabLayout.newTab().setText(NewsConstant.HeadLines));
tabLayout.addTab(tabLayout.newTab().setText(NewsConstant.Sports));
tabLayout.addTab(tabLayout.newTab().setText(NewsConstant.Health));
tabLayout.addTab(tabLayout.newTab().setText(NewsConstant.India));
tabLayout.addTab(tabLayout.newTab().setText(NewsConstant.World));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#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) {
}
});
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setAdapter(pageAdapter);
viewPager.setOffscreenPageLimit(5);
}
PageAdapter.java
public class PageAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PageAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
return NewsFragment.newInstance(position,position+"");
}
#Override
public int getCount() {
return mNumOfTabs;
}
NewFragment.java
public class NewsFragment extends Fragment {
private RecyclerView recyclerView;
public static NewsFragment newInstance(int index,String title) {
NewsFragment fragment = new NewsFragment();
Bundle args = new Bundle();
args.putInt(NewsConstant.INDEX,index);
args.putString(NewsConstant.TITLE,title);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news_list, container, false);
initView(view);
getDataFromServer(); // Here i get data and display
return view;
}
For Ex.
I use TabLayout, ViewPager in Activity to load Fragments
I have 3 TAB like Headline | Sports | Hollywood
I use same fragment for all TAB
so when I click on Hollywood TAB then get data from server and display at that recyclerView
I faced same problem once
Try using
private boolean isViewShown = false;
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (getView() != null && isVisibleToUser) {
isViewShown = true; // fetchdata() contains logic to show data when page is selected mostly asynctask to fill the data
fetchData();
} else {
isViewShown = false;
} }
Call your Api or what ever you want to see when you reached the fragment here inside
View pager actually loads previous, current and next pages simultaneously. This is to have a seamless experience while swiping. This can be controlled using the approach mentioned by #Jius Lukose
or please refer to the answer
How can make my ViewPager load only one page at a time ie setOffscreenPageLimit(0);

How to add ViewPager with fragment on fragment? [duplicate]

This question already has an answer here:
How to implement a viewpager inside of a fragment?
(1 answer)
Closed 4 years ago.
I am building an android application where in a fragment I need to add viewPager. The ViewPager will be holding fragment and will be swipe left and right.
Can some one help me to complete above task I am not able to find a tutorial for it. I am facing a lot issue adding viewPager fragment in fragment rather activity
Check this: https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
Remove TabLayout if not needed
Starting in Android 4.2, there are nested fragments.http://developer.android.com/about/versions/android-4.2.html#NestedFragments The support library now also includes support for this for older Android versions.
So you can do something like this:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
mViewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_lineup_team_b, container, false);
unbinder = ButterKnife.bind(this, view);
viewpager.setOffscreenPageLimit(3); //5 is number of tabs
tabsFrag = (TabLayout) view.findViewById(R.id.tabs_frag);
toolbar.setVisibility(View.GONE);
return view;
}
#Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupViewPager(viewpager);
if(ViewCompat.isLaidOut(tabsFrag)) {
setViewPagerListener();
} else {
tabsFrag.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
setViewPagerListener();
tabsFrag.removeOnLayoutChangeListener(this);
}
});
}
}
private void setViewPagerListener() {
tabsFrag.setupWithViewPager(viewpager);
// use class TabLayout.ViewPagerOnTabSelectedListener
// note that it's a class not an interface as OnTabSelectedListener, so you can't implement it in your activity/fragment
// methods are optional, so if you don't use them, you can not override them (e.g. onTabUnselected)
tabsFrag.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewpager) {
#Override
public void onTabReselected(TabLayout.Tab tab) {
super.onTabReselected(tab);
}
#Override
public void onTabSelected(TabLayout.Tab tab) {
super.onTabSelected(tab);
}
});
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFrag(new LineUpFragment_A(), "LineUp");
adapter.addFrag(new SubstitutionFragment_A(), "Substitution");
adapter.addFrag(new CoachFragment_A(), "Coach");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}

Android ViewPager inside Fragment loaded only once v2

My question is similtar to this:
Android ViewPager Inside Fragment Loaded Only Once
The difference is that its answer doesn't work for me.
I have 4 fragments. Inside one of these fragments there is a Fragment X with ViewPager that switch betwen two other fragments. The first time I load the FragmentX the ViewPager works fine. The problem is if I switch to one of the other 4 fragments, when I go back to the FragmentX the ViewPager has nothing inside.
Fragment X
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_campus, container, false);
TabLayout tabLayout = (TabLayout)view.findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Mapa"));
tabLayout.addTab(tabLayout.newTab().setText("Eventos"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
vpPager = (ViewPager)view.findViewById(R.id.vpPager);
adapterViewPager = new PagerAdapter(getChildFragmentManager());
vpPager.setAdapter(adapterViewPager);
vpPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
vpPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return view;
}
PageAdapter
public class PagerAdapter extends FragmentPagerAdapter {
private int NUM_ITEMS = 2;
public PagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
#Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return MapFragment.newInstance(0);
case 1:
return EventosFragment.newInstance(1);
default:
return null;
}
}
// Returns the page title for the top indicator
#Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
MainActivity
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
...
if (...) {
if (campusFragment == null) // the problem is not solved by commenting this condition
campusFragment = new CampusFragment(); // Fragment X
fragmentManager.beginTransaction().replace(R.id.content_frame, campusFragment).commit();
}
}

Unexpected behavior using ViewPager with TabLayout

Hello everybody
I have a Fragment with a TabLayout with to tabs, I implement the PagerAdapter and everything work fine, the problem is when I go to other option and after that I tap in to the option again the TabLayout appears but empty, I will share with you the code that I wrote for this functionality:
Adapter:
public class DevicePagerAdapter extends FragmentPagerAdapter {
public DevicePagerAdapter(FragmentManager fm){
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:{
return new fragmentDeviceCategory();
}
case 1:{
return new fragmentDeviceRoom();
}
default:{
return null;
}
}
}
#Override
public int getCount() {
return 2;
}
}
Code for one of the Fragments in the TabLayout:
public class fragmentDeviceList extends Fragment {
protected ViewPager viewPager;
public fragmentDeviceList() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(getResources().getString(R.string.title_devices_list));
}
#Override
public void onResume() {
super.onResume();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_device_list, container, false);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_category)).setIcon(getResources().getDrawable(R.drawable.ic_category_black_36dp, getActivity().getTheme())));
} else {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_category)).setIcon(getResources().getDrawable(R.drawable.ic_category_black_36dp)));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_room)).setIcon(getResources().getDrawable(R.drawable.ic_room_black_36dp, getActivity().getTheme())));
} else {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_room)).setIcon(getResources().getDrawable(R.drawable.ic_room_black_36dp)));
}
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) view.findViewById(R.id.pager);
DevicePagerAdapter adapter = new DevicePagerAdapter(getFragmentManager());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#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) {
}
});
return view;
}
}
An this is the code that I'm using in the main Fragment who is hosting the TabLayout:
public class fragmentDeviceList extends Fragment {
protected ViewPager viewPager;
public fragmentDeviceList() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(getResources().getString(R.string.title_devices_list));
}
#Override
public void onResume() {
super.onResume();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_device_list, container, false);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_category)).setIcon(getResources().getDrawable(R.drawable.ic_category_black_36dp, getActivity().getTheme())));
} else {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_category)).setIcon(getResources().getDrawable(R.drawable.ic_category_black_36dp)));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_room)).setIcon(getResources().getDrawable(R.drawable.ic_room_black_36dp, getActivity().getTheme())));
} else {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.title_tab_by_room)).setIcon(getResources().getDrawable(R.drawable.ic_room_black_36dp)));
}
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) view.findViewById(R.id.pager);
DevicePagerAdapter adapter = new DevicePagerAdapter(getFragmentManager());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#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) {
}
});
return view;
}
}
Note: When I debugged the code I note that the method `onCreateView` in the `fragmentDeviceList` (The fragment that it is hosted the TabLayout) it is executed, but the method `onCreateView` of the fragment `fragmentDeviceCategory` is not fired.
To use the tabs inside a Fragment you should use ChildFragmentManager.
The line:
DevicePagerAdapter adapter = new DevicePagerAdapter(getFragmentManager());
should be:
DevicePagerAdapter adapter = new DevicePagerAdapter(getChildFragmentManager());
FragmentPagerAdapter is a valid use for just two fragments because it keeps them in memory. FragmentStatePagerAdapter is intended for a larger number of fragments.
Also TabLayout has a method setupWithViewPager that will:
This method will:
Add a ViewPager.OnPageChangeListener that will forward events to this TabLayout.
Populate the TabLayout's tabs from the ViewPager's PagerAdapter.
Set our TabLayout.OnTabSelectedListener which will forward selected events to the ViewPager
You can use it instead of manually adding tabs.
I got the solution for the problem, the solutions is very simple, I thought that I can use as Adapter for the ViewPage FragmentPagerAdapter because i want to show just fragments, but this class remain the state of the fragment so the nested fragment don't is initialized again, so I changed my adapter to FragmentStatePagerAdapter and now is working properly.
Thank you all

Categories

Resources