[英]Replace fragments in FrameLayout layout
I have tried every tutorial on the first google page about android fragments, but I can't get anything to work. 我已经在Google第一页上尝试过有关android片段的所有教程,但是我什么都无法工作。
So I have one navigation bar activity, MainActivity
. 因此,我有一个导航栏活动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
. 在我的content_main
( MainActivity
活动中的默认片段)中,我有一个框架布局,希望将这些片段放入其中。我有以下片段: fragment_main
, fragment_one
和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;
显然,我不得不使用android.support.v4.app.Fragment;
instead of android.app.Fragment;
而不是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: 并进行设置(从导航栏onNavigationItemSelected()
,您可以执行以下操作:
setFragment(new RoosterFragment());
I hope this helps others out with the same frustrating problem. 我希望这可以帮助其他人解决同样令人沮丧的问题。
Did you try this? 你有尝试过吗? Click here 点击这里
This link will Help you, to create fragment
. 该链接将帮助您创建fragment
。 and this is what you want for navigation drawer Click here 这就是您想要的导航抽屉 单击此处
This is FrameLayout 这是FrameLayout
<FrameLayout
android:id="@+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/view"
android:layout_marginTop="@dimen/medium_margin"></FrameLayout>
Now , call displayview method onCreate() of your activity, 现在,调用您活动的displayview方法onCreate(),
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//rest of your code here
displayView(0); // fragment at 0 position
}
displayView method , which have three fragements displayView方法 ,具有三个部分
public void displayView(int position) {
switch (position) {
case 0:
tvTitle.setText(getResources().getString(R.string.signin_tile));
showFragment(new LoginFragment(), position);
break;
case 1:
tvTitle.setText(getResources().getString(R.string.forgot_password_tile));
showFragment(new ForgotPasswordFragment(), position);
break;
case 2:
tvTitle.setText(getResources().getString(R.string.change_password_tile));
showFragment(new ChangePasswordFragment(), position);
break;
}
}
showFragment method called from displayView method, showFragment方法从displayView方法调用,
public void showFragment(Fragment fragment, int position) {
FragmentTransaction mTransactiont = getSupportFragmentManager().beginTransaction();
mTransactiont.replace(R.id.main_container, fragment, fragment.getClass().getName());
mTransactiont.commit();
}
Try this one. 试试这个。 it is my newbie code easier to understand :) 这是我的新手代码,更容易理解:)
FragmentTransaction transaction;
switch (id){
case R.id.button_fragment_one:
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, new FragmentOne());
transaction.addToBackStack(null);
transaction.commit();
break;
case R.id.button_fragment_two:
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, new FragmentTwo());
transaction.commit();
break;
default:
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.