Catch the changing of slide in a ViewPager - android

I have implemented a ViewPager in order to swipe between two Fragments like such:
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.statistics_general_vp, container, false);
mViewPager = (ViewPager) view.findViewById(R.id.pager);
mStatisticsPagerAdapter = new StatisticsPagerAdapter(getActivity().getSupportFragmentManager());
mViewPager.setAdapter(mStatisticsPagerAdapter);
return view;
}
public class StatisticsPagerAdapter extends FragmentStatePagerAdapter {
public StatisticsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return new GeneralStatistics();
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
But for now my two slides display the same Fragment.
How may I have slide 1 display Fragment 1 and slide 2 Fragment 2?

have u created fragment1 and fragment 2 seperately?? if yes then do this - in activity holding these two fragments.
private void createViewPager(ViewPager viewPager) {
Log.d("activity", "viewpager");
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new Fragment1(), "");
adapter.addFrag(new Fragment2(), "");
viewPager.setAdapter(adapter);
}
create fragment like this -
public class Fragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.transaction_layout, container, false);
return v;
}
}
create viewpager like this -
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);
}
}
or if you want to make tab with icons use this method -
private void createTabIcons() {
Log.d("activity", "createtabicons");
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.tab_item2, null);
tabOne.setText("QR Scan");
tabOne.setTypeface(MRR);
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image1, 0, 0);
tabOne.setCompoundDrawablePadding(5);
tabLayout.getTabAt(0).setCustomView(tabOne);
tabLayout.getTabAt(0).getCustomView().setSelected(true);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.tab_item2, null);
tabTwo.setText("Mobile No.");
tabTwo.setTypeface(MRR);
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.image3, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
tabTwo.setCompoundDrawablePadding(5);
}

Hello i show you what i doing, i got 2 fragments in mainActivity
private void addFragments() {
List<Fragment> fragments = new ArrayList<>();
chatFragment = new ChatFragment();
remoteFragment = new RemoteFragment();
fragments.add(remoteFragment);
fragments.add(chatFragment);
MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(TAB_REMOTE_POSITION)
.setIcon(getDrawable(R.drawable.ic_settings_remote_white_24dp));
tabLayout.getTabAt(TAB_SOCIAL_POSITION)
.setIcon(getDrawable(R.drawable.ic_group_white_24dp));
}
Here is my adapter:
class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
and on OnCreate u should
addFragments();
also i bind the views with ButterKnife
#Bind(R.id.view_pager)
ViewPager viewPager;
#Bind(R.id.tab_layout)
TabLayout tabLayout;
but u can static implement this with android
And remember that u have listeners:
private void pageChangeListener() {
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
View view = getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getSystemService
(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
});
}

Related

Interface in fragment in ViewPager.OnPageChangeListener

In my viewpager i set listener from which i call methods from my interface in tab fragments.
But variable in this tab fragments in my interfaces methods always return null.
Tell me why ?
p.s. sorry for my english)
Parent fragment:
public class MyFragment
extends Fragment {
protected Context mContext;
#Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}
#Override
public void onDetach() {
super.onDetach();
mContext = null;
} }
Container fragment with PagerAdapter:
public class FragmentContainer
extends MyFragment {
private ViewPager viewPager;
private ViewPager.OnPageChangeListener onPageChangeListener;
public interface IPageSelected {
void onPageSelected();
void onPageUnselected();
}
public static FragmentContainer newInstance() {
return new FragmentContainer();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(
R.layout.fragment_container, container, false);
viewPager = view.findViewById(R.id.pager);
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = view.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
onPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
for(int x = 0; x < pagerAdapter.getCount(); x++) {
((IPageSelected) pagerAdapter.getItem(x)).onPageUnselected();
}
((IPageSelected) pagerAdapter.getItem(position)).onPageSelected();
}
#Override
public void onPageScrollStateChanged(int state) {
}
};
viewPager.addOnPageChangeListener(onPageChangeListener);
return view;
}
#Override
public void onResume() {
super.onResume();
setCurrentItem();
}
private void setCurrentItem() {
viewPager.post(() -> {
viewPager.setCurrentItem(0);
onPageChangeListener.onPageSelected(0);
});
}
class PagerAdapter
extends FragmentStatePagerAdapter {
PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return TestFragment1.newInstance();
case 1:
return TestFragment2.newInstance();
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab1";
case 1:
return "Tab2";
default:
return null;
}
}
} }
Tab fragments.
Question: why variable mContext in interface methods always return null ?
public class TestFragment1
extends MyFragment
implements FragmentContainer.IPageSelected {
public static TestFragment1 newInstance() {
return new TestFragment1();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
#Override
public void onPageSelected() {
Log.d("myLog", "TestFragment1 onPageSelected: " + mContext); // WHY NULL ???
}
#Override
public void onPageUnselected() {
Log.d("myLog", "TestFragment1 onPageUnselected: " + mContext); // WHY NULL ???
} }
public class TestFragment2
extends MyFragment
implements FragmentContainer.IPageSelected {
public static TestFragment2 newInstance() {
return new TestFragment2();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
#Override
public void onPageSelected() {
Log.d("myLog", "TestFragment2 onPageSelected: " + mContext); // WHY NULL ???
}
#Override
public void onPageUnselected() {
Log.d("myLog", "TestFragment2 onPageUnselected: " + mContext); // WHY NULL ???
} }
In your fragment container :
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager()); here is the fault.
In order to attach your child fragments to your container fragment, consider passing child fragment manager getChildFragmentManager() to your viewpager adapter. (Right now your fragments are attached to same level as your container fragment).
I found solution.
Adapter class.
Old:
class PagerAdapter
extends FragmentStatePagerAdapter {
PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return TestFragment1.newInstance();
case 1:
return TestFragment2.newInstance();
}
return null;
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Tab1";
case 1:
return "Tab2";
default:
return null;
}
}
}
New:
class PagerAdapter
extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
Adapter initialization.
Old:
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
viewPager.setAdapter(pagerAdapter);
New:
PagerAdapter pagerAdapter = new PagerAdapter(getFragmentManager());
pagerAdapter.addFragment(TestFragment1.newInstance(), "tab1");
pagerAdapter.addFragment(TestFragment2.newInstance(), "tab2");
viewPager.setAdapter(pagerAdapter);
And now new question: what's the differense with the first option ?
Now i initializing adapter in container fragment, it's a cause ?
Why didn't work when initializing was in the adapter ?

ViewPager not showing content onResume

I'm using FragmentStatePagerAdapter for TabLayout with a ViewPager going to show a RecyclerView and when I click on a item of my list and i click back to go to previous page It's showing me a blank page with TabLayout and no RecyclerView.
Here is my Adapter code:
public class MyViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public MyViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
And here is my fragment handling ViewPager and TabLayout:
public class InternetFragment extends Fragment {
private View rootView;
private TabLayout tabLayout;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_internet_package, container, false);
initUI();
return rootView;
}
private void initUI() {
ViewPager viewPager = rootView.findViewById(R.id.viewpager);
tabLayout = rootView.findViewById(R.id.tabs);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TextView txtTabTitle = (TextView) LayoutInflater.from(GC.getInstance().getAppContext()).inflate(R.layout.custom_tablayout_cell, null);
tabLayout.getTabAt(i).setCustomView(txtTabTitle);
txtTabTitle.setTypeface(GC.font);
}
}
private void setupViewPager(ViewPager viewPager) {
String IMSI = GC.getInstance().getSimNumber();
MyViewPagerAdapter pageAdapter = new MyViewPagerAdapter(getActivity().getSupportFragmentManager());
if (IMSI.startsWith(ClientConstant.IMSI_RIGHTEL) || IMSI.startsWith(ClientConstant.IMSI_TALIA)) {
tabLayout.setVisibility(View.GONE);
pageAdapter.addFragment(new InternetOnlinePackageFragment(), "");
} else {
pageAdapter.addFragment(new InternetUSSDPackageFragment(), getString(R.string.package_ussd));
pageAdapter.addFragment(new InternetOnlinePackageFragment(), getString(R.string.package_online));
}
viewPager.setAdapter(pageAdapter);
if (!IMSI.startsWith(ClientConstant.IMSI_RIGHTEL) || !IMSI.startsWith(ClientConstant.IMSI_TALIA))
viewPager.setCurrentItem(1);
}
}

Access Fragment viewPager in Parent Activity

I have a problem in my Application, I load a fragment in my activity and in the fragment i used the ViewPager(for TabLayout) so i want to handle the BackPressed of ViewPager.
Because there is no OnKeyDown override method is available in fragment so i want the refrence of my Fragment ViewPager in my Parent Activty so i can handle this situation in onBackPressed() method Activity.
Activity Code :
fragment= new TabFragment();
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.containerView, fragment);
ft.commitAllowingStateLoss();
// ft.commit();
}
#Override
public void onBackPressed() {
if (isMainShown) {
// want the refrence of view pager so i can handle ViewPager on backPress
}
else {
//display tab fragment
}
//super.onBackPressed();
}
TabFragment :
public class TabFragment extends Fragment {
public TabFragment() {
// Required empty public constructor
}
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 2 ; View x;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
x=inflater.inflate(R.layout.content_main_menu,null);
tabLayout=(TabLayout) x.findViewById(R.id.tabs);
viewPager =(ViewPager) x.findViewById(R.id.viewpager);
setupViewPager(viewPager);
//viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
viewPager.setOffscreenPageLimit(1);
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
private void setupViewPager(ViewPager viewPager) {
MyAdapter adapter = new MyAdapter(getChildFragmentManager());
adapter.addFragment(new TabOne(), "TAb1");
adapter.addFragment(new TabTwo(), "Tab2");
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
final InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(x.getWindowToken(), 0);
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private class MyAdapter extends FragmentPagerAdapter{
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
}
}......
add this code in your onBackPressed()
fragobjTab = (TabFragment) getSupportFragmentManager().findFragmentById(R.id.containerView);
try {
fragobjTab.onBackPressed();
}catch (Exception e){
e.printStackTrace();
}
And create a onBackPressed method in your tabFragment and setCurrentItem to the ViewPager:
public void onBackPressed()
{
// set page to your viewPager
// 0 is your first viewpager fragment
viewPager.setCurrentItem(0,true);
}

How to update only current fragment's view in viewpager?

I used a ViewPager with FragmentPagerAdapter in my app. I created 4 fragments. Also I used EventBus library to refresh current fragment's background color. When I changed the page, onEvent method was triggered, then all 4 fragments' background color was changed. But I want to change only current fragment's background color. How can I fix this bug? Sorry for my bad English.
ViewPager as below :
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
MyPageViewActivity as below :
public class PageViewActivity extends FragmentActivity {
MyPagerAdapter pageAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
List<Fragment> fragments = getFragments();
pageAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
pager.setAdapter(pageAdapter);
pager.setOffscreenPageLimit(4);
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
EventBus.getDefault.post(new TriggerEvent(position));
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
}
private List<Fragment> getFragments(){
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(MyFragment.newInstance("1"));
fList.add(MyFragment.newInstance("2"));
fList.add(MyFragment.newInstance("3"));
fList.add(MyFragment.newInstance("4"));
return fList;
}
}
finally MyFragment.class
public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
RelativeLayout relativeLayout;
String msg;
public static final MyFragment newInstance(String message) {
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
msg = getArguments().getString(EXTRA_MESSAGE);
relativeLayout = (RelativeLayout) v.findViewById(R.id.relativelayout);
return v;
}
public void onEvent(TriggerEvent trigger) {
relativeLayout.setBackgroundColor(randomColor());
}
}
try this,
public void onEvent(TriggerEvent trigger) {
if(isCurrentFragment){
relativeLayout.setBackgroundColor(randomColor());
}
}

TabLayout with ViewPager not working inside fragment Android

Hi I have a TabLayout and ViewPager inside a fragment. But I can not slide beetween tab to come to child fragment (Working normally when in Activity but not work in fragment).
Here is my code for Tablayout inside fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflateView = inflater.inflate(R.layout.fragment_assign_beacon_to_event, container, false);
//Viewpager and TabLayout
viewPager = (ViewPager)inflateView.findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapter(getFragmentManager(), getContext()));
tabLayout = (TabLayout)inflateView.findViewById(R.id.tabLayout);
// Inflate the layout for this fragment
return inflateView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
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());
}
});
Any here is my Custom Adapter class
private class CustomAdapter extends FragmentPagerAdapter {
private String fragments[] = {"Assign Event", "Manage Event"};
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
// return new AssignEventToBeacon();
return new Test1();
case 1:
return new ManageEventAssigned();
default:
return null;
}
}
#Override
public int getCount() {
return fragments.length;
}
#Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
I doubt i have problem with this line:
viewPager.setAdapter(new CustomAdapter(getFragmentManager(), getContext()));
Anyhelp is much appreciate, thanks
I have done a similar project and here is my code for the fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.outer_fragment, container, false);
setHasOptionsMenu(true);
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
Log.v("Layout","Tabs");
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setSelectedTabIndicatorColor(Color.parseColor("#FFFFFF"));
tabLayout.setTabTextColors(Color.parseColor("#707070"), Color.parseColor("#FFFFFF"));
assert viewPager != null;
tabLayout.setupWithViewPager(viewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getChildFragmentManager());
adapter.addFragment(new Tab1Fragment(), "PHOTOS");
adapter.addFragment(new Tab2Fragment(), "HI-FIVES");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public Adapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
You need to use getChildFragmentManager() in Fragment instead of getFragmentManager().
From getChildFragmentManager() documentation
Return a private FragmentManager for placing and managing Fragments
inside of this Fragment.
From getFragmentManager() documentation
Return the FragmentManager for interacting with fragments associated
with this fragment's activity.
In simple words,
Use getChildFragmentManager() for Child Fragments
Use getFragmentManager() for Fragments inside Activity
One can see #this answer for complete code.

Categories

Resources