简体   繁体   中英

How can I use an OnClickListener inside of a fragment to swap between the other fragments

I have an Activity that displays 2 fragments. One acts as a list of options and includes a few textviews and the other is the corresponding fragment. What is the best way use OnClick for those text views to swap between the corresponding fragments?

Thank you in advanced, I really appreciate it.

this is the basic code for swapping fragments:

import android.support.v4.app.Fragment;

//If not using "support" fragment, use getFragmentManager()
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

//you can omit this, Android.R. has some built in anmations.
ft.setCustomAnimations(android.R.anim.slide_in_left, R.animator.fade_out);

//"replace" empties the container of all existing fragments.
ft.replace(container, myFrag, str_SomeID);          

ft.commit();

Alternatively you can hide a fragment in place. The basic code for hiding a fragment:

FragmentManager manager = getSupportFragmentManager();

fragment = manager.findFragmentByTag(str_SomeID);

if (fragment != null && !fragment.isHidden()){

manager.beginTransaction().hide(fragment).commit();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM