Android – how to pass from one fragment to another
•
Android
In my application, I have some fragments of navigation drawers. When I select in the drawer, please execute the following code:
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, f, tag);
ft.commit();
In mainactivity
Now, in a clip, I want to place a tablayout at the bottom of the screen, but I encounter the following difficulties:
>Can I replace one clip with another? Where can I place the fragmenttransaction? > If I want to call the third clip (for example, the send button in one of the tab clips), can I use the same function for that tab?
Thank you for your answer
resolvent:
You can
//when navigation item is selected by user
@Override
public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
public void displayView(int position) {
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new UserProjectListFragment();
break;
case 1:
fragment = new ContactUsFragment();
break;
case 2:
fragment = new HelpFragment();
break;
case 3:
//and so on
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment).addToBackStack("fragBack");
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}
/For your question, can you replace one fragment with another? Where can I place the fragmenttransaction? It's like this
Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment, videoFragment).commit();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码