I'm trying to change a fragment inside a ViewPager:
public void change() {
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.pager, mapa);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
mSectionsPagerAdapter.notifyDataSetChanged();
}
R.id.pager is my ViewPager id.
mapa is a inflated fragment.
If I call change(), the fragment I want to replace will disappear but the new fragment will not be load.
The view pager uses an Adapter to change the fragments it displays. Just like a ListView uses its adapter to get views to populate the list.
You have to extend FragmentPagerAdapter, create a new instance of it and set it to the pager with this line:
mViewPager.setAdapter(fragPagerAdapter);
Related
I have an activity with bottom tabs, which I use to switch fragments. One of the tabs have fragment with ViewPager setup (with 3 tabs). ViewPager has a RecyclerView and when I click on any item, the new fragment should replace fragment in which ViewPager exists.
But I receive an error No view found for id R.id.frame_layout_content for fragment IndexFragment when trying to replace fragment. How to properly replace fragment in this case?
Code flow:
Activity -> replace fragment in R.id.frame_layout_content with ViewPagerFragment -> Setup the ViewPager with fragment adapter (3 tabs) -> Click on RecyclerView item in IndexFragment to replace fragment in R.id.frame_layout_content with IndexDetailsFragment.
ViewPagerFragment:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
mViewPager = (ViewPager) view.findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new IndexFragment(), view.getResources().getString(R.string.title_index));
adapter.addFragment(new FaqFragment(), view.getResources().getString(R.string.title_faq));
adapter.addFragment(new QuotesFragment(), view.getResources().getString(R.string.title_cquotes));
mViewPager.setAdapter(adapter);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
super.onViewCreated(view, savedInstanceState);
}
Code to change fragment from IndexFragment:
IndexDetailsFragment newFragment = new IndexDetailsFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
transaction.replace(R.id.frame_layout_content, newFragment);
transaction.addToBackStack(null);
transaction.commit();
Try to change
IndexFragment newFragment = new IndexFragment();
to
Fragment newFragment = new IndexFragment();
Hope it helps.
R.id.frame_layout_content is inside MainActivity, so IndexFragment won't be able to access it.
You should make an interface, so IndexFragment can notify MainActivity and then MainActivity should change R.id.frame_layout_content content.
When I call FragmentTransaction transaction = getFragmentManager().beginTransaction() in IndexFragment (which replaced with FragmentManager attached to ViewPager) I receive an instance attached to ViewPager not to activity, which has R.id.frame_layout_content
So the solution is to get FragmentManager instance from parent Activity:
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
I am using three tabs and these tabs having list view, on these tabs i am adding new list items in tabs. After Creating a list the new list item is not added when i scroll the tabs then it shows the list item
when you add item in listview call one method (make it by own) in your adapter
public void refreshWishlistAdapter() {
itemList.clear();
itemList.addAll(dbHelper.getallWishlistItems());
itemsAdapter.notifyDataSetChanged();
}
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
Your_Fragment_TAG is the name you gave your fragment when you created it
This code is for support library.
please help me.
I have the following problem.
From the fragment, which was created from getSupportFragmentManager() create ViewPager.
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
MyViewPager viewPager = MyViewPager.newInstance();
fragmentTransaction.add(R.id.container, viewPager);
fragmentTransaction.commit();
Everything is ok. But if i rotate the screen.
Triggered onCreateActivity().
In the fragment from which I create ViewPager.
In ViewPager
In page(page in ViewPager is a fragment).
Questions:
That is how it should be? By this logic, I must retain ViewPager
position and recreate ViewPager.
How to make it so that when rotating the screen called only
onCreateActivity() from ViewPager and page. And not in
parentFragment?
When i try create ViewPager from:
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
I get the following error ..
java.lang.IllegalArgumentException: No view found for id 0x7f0f0054 (com.jd:id/container) for fragment MyViewPager{41dcf008 #0 id=0x7f0f0054 My_FRAGMENT}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
Thanks.
I've the following situation:
Linear Layout
ToolBar
SlidingTabLayout
ViewPager
FrameLayout
Initially, I only add a fragment to the FrameLayout, and all works. Next I remove this fragment, create new Adapter, and bind adapter to the SlidingTabLayout. All works again, but now I need to set tab visibility to false, and I do this with
slidingTabLayout.setVisibility(View.GONE);
but I also need to replace the current view with a new fragment.
Usually I do this with:
getFragmentManager().replace(parent, newFragment);
but now I can't do this because
getFragmentManager().replace(ViewPager, newFragment);
doesn't works. So how can I do this? And if is possible, can I add the ViewPager replacing in the backstack?
I find a solution. I made the following update to my layout:
Linear Layout
ToolBar
Linear Layout (id: parentScroll)
SlidingTabLayout
ViewPager
Frame Layout (id: parent)
So when I need to hide all the tabs and the ViewPager, in order to inflate a new Fragment in the Frame Layout, I do the following:
parentScroll.setVisibility(View.GONE);
MyFragment myFragment= new MyFragment ();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.parent, myFragment, "MYTAG");
transaction.commit();
In the onBackPressed of this new fragment:
MyFragment myFragment= (MyFragment) getSupportFragmentManager.findFragmentByTag("MYTAG");
if (myFragment!= null && myFragment.isVisible()) {
parentScroll.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction().remove(myFragment).commit();
}
I'm using a ViewPager which has 6 fragments(i.e) 6 pages, i want to replace a fragment with a new fragment dynamically.. i'm using FragmentStatePagerAdapter and overriding getItemPosition method
the following is the code inside the getItemPosition method.
Fragment myFragment = (Fragment)object;
FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
Fragment iviteFragment = new InviteFragment();
trans.detach(myFragment);
trans.add(mPager.getId(), iviteFragment);
trans.commit();
return POSITION_NONE;
this doesn't work.