替换的FrameLayout布局片段 [英] Replace fragments in FrameLayout layout
问题描述
我已经试过关于Android的碎片第一款谷歌页面上的每个教程,但我不能得到任何工作。
所以我有一个导航栏的活动, MainActivity
。
现在我想改变在抽屉里点击片段。
在我的 content_main
(在 MainActivity
活动默认片段),我有我想提出一个的FrameLayout fragment_main
, fragment_one
和 fragment_two $:在我有以下片段的片段C $ C>。我希望展示这些当我点击在导航抽屉里的一个按钮。
我想用碎片的原因是,这样的资产净值抽屉就会留在上面。
在此先感谢!
编辑:这是我将使用更改片段,该功能:结果
它只是为了测试,没有完成。
公共无效setFragment(){
android.support.v4.app.FragmentTransaction交易;
交易= getSupportFragmentManager()调用BeginTransaction()。
transaction.replace(R.id.fragment_container,新LoginFragment());
器transaction.commit();
}
我解决了!
显然,我不得不使用 android.support.v4.app.Fragment;
而不是 android.app.Fragment;
。
这是code我得到了它的工作:
保护无效setFragment(片段片段){
android.support.v4.app.FragmentTransaction T = getSupportFragmentManager()调用BeginTransaction()。
t.replace(R.id.fragment_container,片段);
t.commit();
}
和设置它(从导航栏 onNavigationItemSelected()
,你这样做:
setFragment(新RoosterFragment());
我希望这可以帮助别人出与同令人沮丧的问题。
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();
}
I solved it!
Apparently, I had to use android.support.v4.app.Fragment;
instead of android.app.Fragment;
.
This is the code I got it to work with:
protected void setFragment(Fragment fragment) {
android.support.v4.app.FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.fragment_container, fragment);
t.commit();
}
And to set it (from the nav bar onNavigationItemSelected()
, you do this:
setFragment(new RoosterFragment());
I hope this helps others out with the same frustrating problem.
这篇关于替换的FrameLayout布局片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!