Replacing a fragment / tab in a view

I'm currently trying to create a framework for my future applications. I really like ActionBar and ViewPager, but skip the function. I need to replace fragment / tab at runtime.

Using an official example , I would like to see something like replaceTab (), but the fragment itself is not updated, no matter what I do.

+6
source share
4 answers

You should use the FragmentPagerAdapter to handle changes to your fragment. There are 3 important things:

  • delete the previous fragment if it exists.

  • call notifyDataSetChanged () to refresh the list of pages.

  • returns POSITION_NONE in getItemPosition if it requests an old fragment.

Here is the code that I use on the left and right pages. I am changing dynamically fragments depending on what the user is doing.

public class MyPager extends ViewPager { private MyPagerAdapter mMyPagerAdapter; public MyPager(Context context, FragmentActivity activity) { super(context); mMyPagerAdapter = new MyPagerAdapter(activity.getSupportFragmentManager()); setAdapter(mMyPagerAdapter); } public void replaceLeftFragment(Fragment fragment) { mMyPagerAdapter.replaceLeftFragment(fragment); } public void replaceRightFragment(Fragment fragment) { mMyPagerAdapter.replaceRightFragment(fragment); } } public class MyPagerAdapter extends FragmentPagerAdapter { private FragmentManager mFragmentManager; private Fragment mLeftFragment; private Fragment mRightFragment; public MyPagerAdapter(FragmentManager fm) { super(fm); mFragmentManager = fm; } @Override public Fragment getItem(int position) { if (position == 0) { return mLeftFragment; } else { return mRightFragment; } } @Override public int getCount() { final int nbLeft = (mLeftFragment == null) ? 0 : 1; final int nbRight = (mRightFragment == null) ? 0 : 1; return (nbLeft + nbRight); } public void replaceLeftFragment(Fragment fragment) { if (mLeftFragment != null) { mFragmentManager.beginTransaction().remove(mLeftFragment).commit(); } mLeftFragment = fragment; notifyDataSetChanged(); } public void replaceRightFragment(Fragment fragment) { if (mRightFragment != null) { mFragmentManager.beginTransaction().remove(mRightFragment).commit(); } mRightFragment = fragment; notifyDataSetChanged(); } @Override public int getItemPosition(Object object) { if ((object!=mLeftFragment) && (object!=mRightFragment)) { return POSITION_NONE; } return super.getItemPosition(object); } } 
+2
source

You are trying:

 fragmentTransaction.remove(yourframent); 

and then:

 fragmentTransaction.add(yournewfragment); 
+1
source

If your Fragment created from a layout file, it cannot be replaced at run time. You can successfully replace only fragments that you added programmatically. Watch Android: you cannot replace one fragment with another

0
source

Very close to what you want, I needed to add a fragment (at the end) in the ViewPager and do this:

  FragmentTransaction ft = fragmentManager.beginTransaction(); new_fragment = Fragment.instantiate(activity, class_name, args); // The below code removes old fragment and adds the new one at the end. ft.attach(new_fragment); // <- this adds the fragment at the end ft.detach(old_fragment); // <- this removes old fragment // This should be what you're looking for when adding to an Activity instead of a ViewPager ft.replace(view_id, new_fragment); // <- this is supposed to replace the fragment attached to view_id // Or if the replace does not work, this might work: ft.detach(old_fragment); ft.add(view_id, new_fragment); // Not to be forgotten: ;) ft.commit(); 

In the above code, some adjustments may be required, maybe you only need to efficiently execute a replacement call?

If the replacement call failed, you can detach all the fragments and reconnect them in the desired order.

0
source

Source: https://habr.com/ru/post/912249/


All Articles