I have tried every tutorial on the first google page about android fragments, but I can't get anything to work.
So I have one navigation bar activity, MainActivity
.
Now I'd like to change fragments on a click in the drawer.
In my content_main
(default fragment in the MainActivity
activity), I have a framelayout that I wish to put the fragments in. I have the following fragments: fragment_main
, fragment_one
and fragment_two
. And I wish to show these when I click on a button in the nav drawer.
The reason I want to use fragments is so that the nav drawer will stay on top.
Thanks in advance!
Edit: Here is the function I'll use to change fragments:
It's just to test, not finished.
public void setFragment() {
android.support.v4.app.FragmentTransaction transaction;
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, new LoginFragment());
transaction.commit();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…