viewpager from fragment returns blank - android

I am working on an android app with viewpager and Tablayout. This is my view pager fragment which opens from a parent activity
public class HomeFragment extends Fragment{
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
public HomeActivity() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
mViewPager = (ViewPager) rootView.findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
return rootView;
}
}
this is my adapter for viewpager
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0 : return new Settingsfragment();
case 1 : return new Profilefragment();
case 2 : return new Categoriesfragment();
}
return null;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "settings";
case 1:
return "profile";
case 2:
return "Category";
}
return null;
}
}
when I open a fragment with the code below and press back viewpager become empty
public class Profilefragment extends Fragment {
public Profilefragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.profile_layout, container, false);
TextView VE = (TextView) rootView.findViewById(R.id.profile_view_enquiry);
VE.setOnClickListener(new View.OnClickListener(){
public void onClick (View view){
Fragment fragment = null;
fragment = new Enquiryfragment();
replaceFragment(fragment);
}
});
TextView CA = (TextView) rootView.findViewById(R.id.profile_change_address);
CA.setOnClickListener(new View.OnClickListener(){
public void onClick (View view){
Fragment fragment = null;
fragment = new ChangeAddressfragment();
replaceFragment(fragment);
}
});
return rootView;
}
public void replaceFragment(Fragment someFragment) {
android.support.v4.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.containerView, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}}
How can I fix this. Please Help.

public class HomeFragment extends Fragment{
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
public HomeFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());//Changed here
mViewPager = (ViewPager) rootView.findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
return rootView;
}
}
You should use getChildFragmentManager() instead of getFragmentManager() in Fragment

Related

LoadData() in OnCreateView of Fragment in TabView called Twice

Once my application is Login I start ActivityTwo. In ActivityTwo I am immediately calling TabView Fragment which hold my three Tabs.This is code in ActivityTwo to call TabView Fragment
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
fm = getSupportFragmentManager();
final Bundle bundle = new Bundle();
bundle.putInt("TARGET_FRAGMENT_ID", fragId);
ChooseTab chooseTab = new ChooseTab();
chooseTab.setArguments(bundle);
ft = fm.beginTransaction();
ft.replace(R.id.frame, chooseTab, add);
ft.commit();
});
This is code for ChooseTab Fragment which is responsible for setting TabView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.choose_tab, container, false);
//-----------------------------------------------------------------------------------
int position=getArguments().getInt("TARGET_FRAGMENT_ID");
tabLayout = (TabLayout) v.findViewById(R.id.simpleTabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab3"));
viewPager = (ViewPager) v.findViewById(R.id.simpleViewPager);
viewPager.setOffscreenPageLimit(3);
final PagerAdapter adapter = new PagerAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
viewPager.setCurrentItem(position);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
adapter.notifyDataSetChanged();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
adapter.notifyDataSetChanged();
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
adapter.notifyDataSetChanged();
}
});
return v;
}
This is code for PagerAdapter
public class PagerAdapter extends FragmentStatePagerAdapter {
private int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Tab1();
case 1:
return new Tab2();
case 2:
return new Tab3();
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
This is code for Tab1 onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.current_status, container, false);
fm = getFragmentManager();
LoadData();
});
return v;
}
But when I run the app and I print some Log when LoadData() is called it is seen loadData is called. How to resolve this ?

activity is blank in tab activity

I am creating ListView in my app in tab Activity.but when app runs emulator shows blank activity. I don't know what is the issue with this. Please help. I am not getting list in the tab. I am getting list in simple activity without tab but I can't see list in tab activity. Let me know if you want other information.
This my ArtistTab.java activity
public class ArtistsTab extends Fragment {
ListView listView1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.artist, container, false);
listView1 =(ListView)v.findViewById(R.id.listView);
return v;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// add your code here which executes after the execution of onCreateView() method.
ArrayList<String>my=new ArrayList<>();
my.add("devesh");
my.add("devesh");
my.add("devesh");
my.add("devesh");
ArrayAdapter arrayAdapter=new ArrayAdapter(getActivity(), layout.simple_list_item_1,my);
listView1.setAdapter(arrayAdapter);
}
}
This is my main Activity
public class MainActivity extends AppCompatActivity {
public ViewPagerAdapter pagerAdapter;
private ViewPager mViewPager;
private Toolbar toolbar;
private TabLayout tabLayout;
public static View rootView;
public static int tabNo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
mViewPager.setAdapter(pagerAdapter);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());//setting current selected item over viewpager
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// View rootView;
switch (getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1: {
rootView = inflater.inflate(R.layout.songs, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.album, container, false);
break;
}
case 3: {
rootView = inflater.inflate(R.layout.genres, container, false);
break;
}
case 4: {
rootView = inflater.inflate(R.layout.artist, container, false);
break;
}
}
return rootView;
}
}
}
artistTab xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView" />
</RelativeLayout>
Try like this to tabs with viewpager
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ArtistsTab(), "ArtistsTab");
adapter.addFragment(new SongsTab(), "Songs");
//Do your stuff here
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 addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}

How to start a fragment from an xml resource

I have a TabLayout Activity, and for one of the tabs (case 3) i want to use a PreferenceFragment for a settings page. In case 3 below i've declared the fragment, but how do I start it?
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1: {
rootView = inflater.inflate(R.layout.fragment_profile, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_find, container, false);
break;
}
case 3: {
SettingsFragment fragment = new SettingsFragment();
// what goes here?
break;
}
}
return rootView;
}
full code:
public class Find extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
TabLayout.Tab tab1 = tabLayout.getTabAt(0);
tab1.setCustomView(R.layout.icon_view);
tab1.setIcon(R.drawable.profile);
TabLayout.Tab tab2 = tabLayout.getTabAt(1);
tab2.setCustomView(R.layout.icon_view);
tab2.setIcon(R.drawable.logo_a);
TabLayout.Tab tab3 = tabLayout.getTabAt(2);
tab3.setCustomView(R.layout.icon_view);
tab3.setIcon(R.drawable.settings);
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1: {
rootView = inflater.inflate(R.layout.fragment_profile, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_find, container, false);
break;
}
case 3: {
SettingsFragment fragment = new SettingsFragment();
// what goes here?
break;
}
}
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
Separate SettingsFragment from PlaceholderFragment
Update SectionsPagerAdapter's getItem() method to show SettingsFragment for 3rd TAB.
Remove case 3 from onCreateView().
Try this:
public class Find extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
TabLayout.Tab tab1 = tabLayout.getTabAt(0);
tab1.setCustomView(R.layout.icon_view);
tab1.setIcon(R.drawable.profile);
TabLayout.Tab tab2 = tabLayout.getTabAt(1);
tab2.setCustomView(R.layout.icon_view);
tab2.setIcon(R.drawable.logo_a);
TabLayout.Tab tab3 = tabLayout.getTabAt(2);
tab3.setCustomView(R.layout.icon_view);
tab3.setIcon(R.drawable.settings);
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1: {
rootView = inflater.inflate(R.layout.fragment_profile, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_find, container, false);
break;
}
}
return rootView;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0: {
return PlaceholderFragment.newInstance(1);
}
case 1: {
return PlaceholderFragment.newInstance(2);
}
case 2: {
SettingsFragment fragment = new SettingsFragment();
return fragment;
}
}
return PlaceholderFragment.newInstance(1);
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
}
UPDATE:
To resolve this error you have to use PreferenceFragmentCompat:
1. To use PreferenceFragmentCompat, add below line under dependencies in your build.gradle file:
dependencies {
.........
...............
compile 'com.android.support:preference-v7:25.1.0'
}
2. Extend your SettingsFragment from PreferenceFragmentCompat instead of PreferenceFragment.
import android.support.v7.preference.PreferenceFragmentCompat;
.........
.................
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
.........
.................
3. You must specify preferenceTheme in your theme:
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
.......
...........
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
</style>
You can also check tutorial to learn more about PreferenceFragmentCompat.
Hope this will help~

Log Error in Nested Fragments on View Pager : No view found for id

I have a fragment that Contains view pager.there are three fragments in that Main fragment they are Fragment A,Fragment B,Fragment C. Upto this code runs properly.
Then i added a new Fragment and called via Fragment A then i got the error in the Log
No view found for id 0x7f0d0083 (com.app.sanyog:id/frame) for fragment SearchPlaces{289bf4e #0 id=0x7f0d0083}
Main Fragment:
/**
* Created by Admin on 04-06-2015.
*/
public class ContentFragment_Flight_Bkg extends Fragment {
private View view;
public ViewPager pager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_fragment_flight,container,false);
TabsPagerAdapter adapter = new TabsPagerAdapter(getFragmentManager());
pager = (ViewPager)view.findViewById(R.id.pager);
pager.setAdapter(adapter);
return view;
}
}
Pager Adapter
public class TabsPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
// Tab Titles
private String tabtitles[] = new String[] { "One Way", "Round Trip", "Multicity" };
Context context;
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public Fragment getItem(int position) {
switch (position) {
// Open FragmentTab1.java
case 0:
OneWayTab fragmenttab1 = new OneWayTab();
return fragmenttab1;
// Open FragmentTab2.java
case 1:
RoundTripTab fragmenttab2 = new RoundTripTab();
return fragmenttab2;
// Open FragmentTab3.java
case 2:
MultiCityTab fragmenttab3 = new MultiCityTab();
return fragmenttab3;
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
return tabtitles[position];
}
}
Fragment A
public class OneWayTab extends Fragment {
private LinearLayout oneway_source,oneway_destiantion;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab3.xml
View view = inflater.inflate(R.layout.content_one_way_tab, container, false);
oneway_source = (LinearLayout)view.findViewById(R.id.oneway_source_layout);
oneway_destiantion = (LinearLayout)view.findViewById(R.id.oneway_destination_layout);
oneway_source.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SearchPlaces fragment_Flight= new SearchPlaces();
android.support.v4.app.FragmentTransaction fragmentTransaction_Flight = getChildFragmentManager().beginTransaction();
fragmentTransaction_Flight.replace(R.id.frame,fragment_Flight,"FLight_booking");
fragmentTransaction_Flight.commit();
}
});
oneway_destiantion.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Utils.showToast(getActivity(),"Destiantion");
}
});
return view;
}
}
New Fragment (Called from Fragment A)
public class SearchPlaces extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragmenttab1.xml
View view = inflater.inflate(R.layout.content_search_places, container, false);
return view;
}
}

set fragment tag when opening fragment using viewpager

here's a portion of my code:
public class MenuExpenses extends Fragment {
private ViewPager viewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.expenses,container, false);
viewPager = (ViewPager) root.findViewById(R.id.pager);
viewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) root.findViewById(R.id.tabs);
tabs.setViewPager(viewPager);
return root;
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
private final String[] TITLES = { "Daily", "Recurring" };
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return new MenuExpensesDaily();
case 1: return new IncomeList2();
default: return new MenuExpensesDaily();
}
}
}
}
MenuExpensesDaily & IncomeList2 are both fragments. So how can I set the fragment tag for both of them here?
You can override the instantiateItem method, the save the fragment to a SparseArray/HashMap, then create a method to return the fragment with the position. Now you will be able to access your selected your fragment and set the tag to it.

Categories

Resources