[英]How to change fragment from another fragment an apply it on the main_content layout?
I'm struggling with the changing of a fragment from another fragment. 我正在努力从另一个片段中更改一个片段。 In my main_activity i have a layout-file with a bottomnavigationview and a framelayout for the content on the page.
在我的main_activity中,我有一个具有bottomnavigationview的布局文件和页面内容的框架布局。 The changing from fragment to fragment on the navigationview workes fine, but if i access a new "subfragment" from a fragment, the new fragment is transparent.
在NavigationView上从片段到片段的更改工作正常,但是如果我从片段访问新的“子片段”,则新片段是透明的。
Any suggestions? 有什么建议么? code below from my fragment and my mainactivity.
下面的代码来自我的片段和我的mainactivity。
MainActivity parentActivity = (MainActivity) getActivity();
parentActivity.switchContent(new VisArrangementInfo(), data, true);
switchContent inside MainActivity: MainActivity中的switchContent:
public void switchContent(final Fragment fragment, Bundle data, final Boolean addToBackStackOption){
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
}
The id content_frame is the id for the layoutfile for my MainActivity. id content_frame是MainActivity的布局文件的ID。 the bottomnavigationview and the framelayout is in this file.
bottomnavigationview和framelayout在此文件中。 the ID for the framelayout is "main_frame".
框架布局的ID为“ main_frame”。
After my understanding i would want to change the "main_frame" and not the "content_fram" since it's here the main-content is supposed to be. 在我理解之后,我想更改“ main_frame”而不是“ content_fram”,因为这里应该是主要内容。 When i do that i can't find the layout for the fragment i try to open at all.
当我这样做时,我找不到片段的布局,我尝试完全打开。 Is it because i'm trying to replace the framelayout instead of the whole layout?
是因为我要替换框架布局而不是整个布局吗?
In your Fragment A
define an interface. 在
Fragment A
定义一个接口。
public interface FragmentEventListener {
void onFragmentChangeEvent(Fragment newFragment);
}
private FragmentEventListener listener;
public void setEventListener(FragmentEventListener listener) {
this.listener = listener;
}
In your fragment, when you want to change the fragment, call the method. 在片段中,当您想更改片段时,请调用方法。
if (listener != null) {
listener.onFragmentChangeEvent(FragmentB);
}
In your main activity, implement FragmentEventListener
, so you can switch to Fragment B
from MainActivity. 在您的主要活动中,实现
FragmentEventListener
,以便您可以从MainActivity切换到Fragment B
。
public class MainActivity implements FragmentA.FragmentEventListener {
...
...
fragmentB.setEventListener(this); // here identify the listener.
....
@Override
public void onFragmentChangeEvent(Fragment newFragment) {
switchContent // Switch to Fragment B here
}
In the first time use add
instead of replace
: 第一次使用
add
而不是replace
:
public void switchContent(final Fragment fragment, Bundle data, final Boolean addToBackStackOption){
getSupportFragmentManager()
.beginTransaction()
.add(R.id.content_frame, fragment)
.addToBackStack(null)
.commit();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.