Fragment null when call function viewpager - android

My Fragment class:
onCreatedView:
parentActivity = (MainActivity) getActivity();
parentView = inflater.inflate(R.layout.fragmentla, container, false);
mFragmentAdapter = new myFragmentAdapter(parentActivity.getSupportFragmentManager(), parentView.getContext());
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) parentView.findViewById(R.id.viewpager);
viewPager.setAdapter(mFragmentAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) parentView.findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
Thats my fragment (Blue border). My fragment contains toolbar, tablayout, and viewpager.
My FragmentAdapter class:
public class myFragmentAdapter extends FragmentStatePagerAdapter {
final int PAGE_COUNT = 2;
private String tabTitles[] = new String[] { "ITEM1", "ITEM2" };
private Context context;
public myFragmentAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return 2;
}
#Override
public Fragment getItem(int position) {
if(position == 0) {
return FirstFragment.newInstance(position + 1);
} else {
return SecondFragment.newInstance(position + 1);
}
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
#Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
And this is my FirstFragment class:
public static FirstFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
FirstFragment fragment = new FirstFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
parentView = inflater.inflate(R.layout.fragment_first_newest, container, false);
sometext = (TextView) parentView.findViewById(R.id.wi_my_text);
return parentView;
}
So, my question is: Everytime that I press my menu button in toolbar the textview should change, but this return null cause "onCreatedView" is not called anymore. I tried with this in Fragment class:
public boolean onOptionsItemSelected(MenuItem item){
...
Fragment mFragment = mFragmentAdapter.getItem(0);
if(mFragment instanceof FirstFragment){
((FirstFragment) mFragment).OnButtonPressed();
}
}
Then on my FirstFragment but still getting null exception:
public void OnButtonPressed() {
someText.setText("its working");
createLocationRequest();
}
UPDATED: MY XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:id="#+id/wi_my_text"
android:layout_height="24dp"
android:layout_gravity="center"
android:gravity="center"
android:textStyle="bold"
android:paddingEnd="5dp"
android:text="texttest"/>
</LinearLayout>

Related

TabLayout always shows the last fragment

I am trying to fill Tablayout from 2 different fragments depending on the value (0 or 1) of the argument Radios.newInstance(int type)
The first fragment contains all radio stations and the second contains the favorite stations
But it shows me always the fragment containing the favorite stations
public class HomeFragment extends Fragment {
public static final String TAG = "HomeFragment";
private SectionsPagerAdapter mSectionsPagerAdapter;
private ProgressBar mProgressView;
private ViewGroup mContainer;
private BaseActivity activity;
public HomeFragment() {
}
public static HomeFragment newInstance() {
return new HomeFragment();
}
public void showProgress(final boolean show) {
mContainer.setVisibility(show ? View.GONE : View.VISIBLE);
mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
activity = (BaseActivity) getActivity();
activity.findViewById(R.id.tabs).setVisibility(View.VISIBLE);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
activity.getSupportActionBar().show();
mProgressView = (ProgressBar) activity.findViewById(R.id.progress);
mContainer = container;
showProgress(true);
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.container);
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(RadiosFragment.newInstance(0));
fragments.add(RadiosFragment.newInstance(1));
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager(), fragments);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) activity.findViewById(R.id.tabs);
tabLayout.setVisibility(View.VISIBLE);
tabLayout.setTabMode(TabLayout.GRAVITY_CENTER);
tabLayout.setupWithViewPager(mViewPager);
showProgress(false);
return view;
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
private List<Fragment> mFragments;
public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
mFragments = fragments;
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return 2;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "RADIOS";
case 1:
return "FAVORITES";
default:
return null;
}
}
}
}
This is the fragment that i want to show, if type == 0 it shows the all radio stations, and if type == 1 it shows the favorite stations
public class RadiosFragment extends Fragment {
private static String TYPE = "NONE";
private FirebaseRecyclerAdapter<FireBaseManager.Radio, ViewHolder> mAdapter = null;
private FirebaseRecyclerAdapter<FireBaseManager.Favorites, ViewHolder> mAdapter_fav = null;
private RecyclerView recyclerView;
public ArrayList<FireBaseManager.Favorites> listOrder;
public RadiosFragment() { }
public static RadiosFragment newInstance(int type) {
if (type == 1 )
TYPE = "favorite";
else
TYPE = "radios";
RadiosFragment fragment = new RadiosFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
final Context context = view.getContext();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
recyclerView = (RecyclerView) view.findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
Query query = FireBaseManager.Radio.Ref.orderByChild(FireBaseManager.Radio.Table.Name.text);
ProgressDialog progressDialog = new ProgressDialog(getContext());
if (TYPE == "favorite"){
....
#Override
protected void populateViewHolder(ViewHolder viewHolder, FireBaseManager.Favorites model, int position) {
...
viewHolder.Initialize(Data);
...
}
};
recyclerView.setAdapter(mAdapter_fav);
}
});
}
else if(TYPE == "radios") {
....
#Override
protected void populateViewHolder(ViewHolder viewHolder, FireBaseManager.Radio model, int position) {
...
viewHolder.Initialize(Data);
...
});
}
};
recyclerView.setAdapter(mAdapter);
});
}
return view;
}
}
I think the problem has to do with onCreateView / onCreate but i am no sure, can you help me please.
This might help you.
mViewPager.setCurrentItem(0);
Add this line after you setup tabLayout with viewpager
thank you very much for your answers, i have found a solution for my problem.
the solution is to add Listener on the Tablayout and intercept the events.
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == 0)
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.flContent, RadiosFragment.newInstance(0), FavoriteActivity.TAG).addToBackStack(null).commit();
if (tab.getPosition() == 1)
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.flContent, RadiosFragment.newInstance(1), FavoriteActivity.TAG).addToBackStack(null).commit();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});

How to set Tabview inside the fragment in android

I am new to android.I want to design a page in android which contains a Tab view inside the fragment .
Fragment :
For that I have a Fragment named fragment_home.xml .
Layout Resource files for tabs:
And three layout resource file named
today.xml
this-week.xml
this_month.xml
and java classes for the resource file is
Today.java
ThisWeek.java
Thismonth.java
What is my doubt is how can I implement the tab view inside the fragment .I have referred some SO files but nothing is helped for me .
Home.java (Fragment):
public class Home extends Fragment{
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private TabLayout tabLayout;
private ViewPager viewPager;
private View rootView;
private OnFragmentInteractionListener mListener;
public Home() {
}
public static Home newInstance(String param1, String param2) {
Home fragment = new Home();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
public void setupViewPager(ViewPager viewPager) {
Pager adapter = new Pager(getChildFragmentManager());
adapter.addFragment(new Today(), "Today");
adapter.addFragment(new Reminders(), "Reminders");
adapter.addFragment(new Settings(), "Settings");
viewPager.setAdapter(adapter);
}
public static 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);
}
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
Here I want to implement something but I don't know ,How to ?
Please help me to get this.
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/home_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.Home">
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
PagerAdapter
public class Pager extends FragmentStatePagerAdapter {
int tabCount;
public Pager(FragmentManager fragmentManager, int tabCount) {
super(fragmentManager);
this.tabCount= tabCount;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Today todayObj = new Today();
return todayObj;
case 1:
ThisWeek thisWeekObj = new ThisWeek();
return thisWeekObj;
case 2:
ThisMonth thisMonthObj = new ThisMonth();
return thisMonthObj;
default:
return null;
}
}
#Override
public int getCount() {
return tabCount;
}
public void addFragment(Today today, String today1) {
}
public void addFragment(Reminders reminders, String reminders1) {
}
public void addFragment(Settings settings, String settings1) {
}
}
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;
public class Home extends Fragment{
private TabLayout tabLayout;
private ViewPager viewPager;
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_home, container, false);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabLayout);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
setupViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
public void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new FragmentOne(), "Fragment name one"); //Need to call first tab fragment
adapter.addFragment(new FragmentTwo(), "Fragment name two"); // Need second tab fragment and so on...
viewPager.setAdapter(adapter);
}
public static 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);
}
}
}
FragmentOne.class
public class FragmentOne extends Fragment {
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
return rootView;
}
}
fragment_one.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:background="#color/plainWhite"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment One Showing"
android:gravity="center"
android:textColor="#ffffff"
android:padding="4dp"
android:textSize="18sp"/>
</LinearLayout>
</FrameLayout>
FragmentTwo.class
public class FragmentTwo extends Fragment {
private View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_two, container, false);
return rootView;
}
}
fragment_two.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:clickable="true"
android:background="#color/plainWhite"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment Two Showing"
android:gravity="center"
android:textColor="#ffffff"
android:padding="4dp"
android:textSize="18sp"/>
</LinearLayout>
</FrameLayout>
use this code , this is the complete example of tablayout using view pager

Replacing one fragment with another fragment in viewpager

I am having trouble showing my new fragment . Basically what i doing is i have a viewpager having 3 pages , in the second page i m hiding the current fragment and adding a new one. Everything goes perfectly except that i can't see my new fragment.
My Mainactivity.java
public class MainActivity extends AppCompatActivity {
ViewPager viewPager = null;
NavigationDrawerFragement drawerFragement;
Bitmap image = null;
int notificationExtra = 0;
int nid=0;
Toolbar toolbar;
private void startGetLocationService() {
Intent i = new Intent(this, GetUserLocationService.class);
startService(i);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_register);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
drawerFragement = (NavigationDrawerFragement) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragement.setUp((DrawerLayout) findViewById(R.id.drawer_layout),toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setHomeButtonEnabled(false);
//getSupportActionBar().setIcon(R.drawable.account);
getSupportActionBar().setTitle("Eventyze");
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(viewPager);
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment = null;
if (i == 0)
fragment = new ReminderFragment();
if (i == 1)
fragment = new EventFragment();
if (i == 2)
fragment = new InvitationFragment();
return fragment;
}
#Override
public CharSequence getPageTitle(int position) {
if (position == 0)
return "Reminders";
if (position == 1)
return "Events";
if (position == 2)
return "Invitations";
return null;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
#Override
protected void onResume() {
super.onResume();
if(notificationExtra==1){
viewPager.setCurrentItem(2);
}
}
}
the onclick in my 2nd page .
FragmentManager fragmentManager2 = getFragmentManager();
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
InvitedEventsFragment fragment2 = new InvitedEventsFragment();
fragmentTransaction2.hide(EventFragment.this);
fragmentTransaction2.add(R.id.viewpager, fragment2);
fragmentTransaction2.commit();
My InvitedEventsFragment.java
public class InvitedEventsFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_invited_events_fragment, container, false);
}
return view;
} }
My activity_invited_events_fragment.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/black">
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/appbarcol2"
android:text="New Fragment"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold" /> </LinearLayout>
Can someone tell me why it doesn't show anything i the new fragment.
If you're using a ViewPager, the adapter should manage the Fragments.
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import java.util.List;
public class ExampleAdapter extends FragmentPagerAdapter
{
List<Fragment> fragmentList;
public ExampleAdapter(FragmentManager fm, List<Fragment> fragmentList)
{
super(fm);
this.fragmentList = fragmentList;
}
#Override public Fragment getItem(int position)
{
return fragmentList.get(position);
}
#Override public int getCount()
{
return fragmentList.size();
}
public void removeItem(int position, ViewPager pager)
{
pager.setAdapter(null);
this.fragmentList.remove(position);
pager.setAdapter(this);
}
}

ViewPager inside a

I have an activity that has a viewpager fragment in it
one of the pages has another viewpager fragment in it
the problem is, when i reach the page of the inner viewpager fragment, the "onCreateView" of the inner viewpager fragments doesnt get called, and so the page is blank.
Why is that ?
here is my code :
fragment_viewpager.xml
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
fragment_viewpager_with_logo.xml
<include layout="#layout/logo" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
public class ViewPagerFragment extends Fragment {
private ViewPager fragmentPager;
private PagerAdapter fragmentAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container instanceof ViewPager) {
return inflater.inflate(R.layout.fragment_viewpager_with_logo, container, false);
}
return inflater.inflate(R.layout.fragment_viewpager, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
fragmentPager = (ViewPager)getView().findViewById(R.id.pager);
fragmentPager.setAdapter(fragmentAdapter);
}
public void setAdapter(PurchaseFragmentStatePagerAdapter fragmentAdapter) {
this.fragmentAdapter = fragmentAdapter;
}
public void setCurrentItem(int i) {
fragmentPager.setCurrentItem(i);
}
public int getCurrentItem() {
return fragmentPager.getCurrentItem();
}
public void setOnPageChangeListener(OnPageChangeListener l) {
if (fragmentPager == null) {
fragmentPager = (ViewPager) getView().findViewById(R.id.pager);
}
fragmentPager.setOnPageChangeListener(l);
}
}
in some activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_container);
MyFragmentStatePagerAdapter fragmentAdapter = new MyStatePagerAdapter(getSupportFragmentManager());
List<Fragment> fragments = new ArrayList<Fragment>();
fragments.add(settingsFragment);// settings
ViewPagerFragment vpFragment = new ViewPagerFragment();
fragments.add(vpFragment);
fragmentAdapter.setFragments(fragments);
fragmentPager.setAdapter(fragmentAdapter);
MyFragmentStatePagerAdapter innerFragmentAdapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager());
List<Fragment> innerFragments = new ArrayList<Fragment>();
innerFragments.add(settingsFragment);
innerFragmentAdapter.setFragments(innerFragments);
vpFragment.setAdapter(innerFragmentAdapter);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.content_fragment, fragmentPager).commit();
}
MyFragmentStatePagerAdapter
public class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
private List<BaseFragment> fragments;
#Inject
public MyFragmentStatePagerAdapter(FragmentManager fm) {
super(fm);
}
public void setFragments(List<Fragment> fragments) {
this.fragments = fragments;
}
#Override
public BaseFragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments == null ? 0 : fragments.size();
}
}
In the Fragment use fragment.getChildFragmentManager() in FragmentPagerAdapter's constructor instead of getActivity().getFragmentManager()

ViewPagerIndicator tabs loses content

I am using Holoeverywhere and ViewPagerIndicator; using Holoeverywhere1 I implemented the slider and tabs in the Fragment using ViewPagerIndicator. Everything is working fine, Slider working, the menu in it, the tabs; the tabs works fine only the first time, when I move to other Fragment using the slider and come back to the tabs Fragment, the contents are no more available.
Code for the Fragment where tabs are created
public class SeconFrag extends Fragment {
ViewPager mViewPager;
FragmentAdapter mTabsAdapter;
PageIndicator mIndicator;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabsAdapter = new FragmentAdapter(getSupportFragmentManager());
View view = inflater.inflate(R.layout.second_frag);
mViewPager = (ViewPager) view.findViewById(R.id.pager);
mViewPager.setAdapter(mTabsAdapter);
TitlePageIndicator indicator = (TitlePageIndicator) view
.findViewById(R.id.indicator);
indicator.setViewPager(mViewPager);
indicator.setFooterIndicatorStyle(IndicatorStyle.Triangle);
mIndicator = indicator;
return view;
}
#Override
public void onResume() {
super.onResume();
getSupportActionBar().setSubtitle("Create");
}
}
FragmentAdapter code
public class FragmentAdapter extends FragmentPagerAdapter{
protected static final String[] CONTENT = new String[] { "Choose", "Customise" };
private int mCount = CONTENT.length;
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0) {
return TabOne.newInstance();
} else if (position == 1) {
return TabTwo.newInstance();
}else if (position == 3 ) {
return TabOne.newInstance();
}else
return null;
}
#Override
public int getCount() {
return mCount;
}
#Override
public CharSequence getPageTitle(int position) {
return FragmentAdapter.CONTENT[position % CONTENT.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}
on of the tab Fragment
public final class TabOne extends Fragment {
public static TabOne newInstance() {
TabOne fragment = new TabOne();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.tab_one, container,
false);
return myFragmentView;
}
}
in the SeconFrag OnPause() I removed the Fragment no luck, hide() it in the OnPause() and tried to show() in the OnResume() still not showing content after the Fragment is changed.
EDIT

Categories

Resources