Android 嵌套片段-Frag2的屏幕保持为空
我正在忍受嵌套碎片的折磨。我有一个mainactivity,它调用片段1,片段1通过按钮依次调用片段。片段frag2被很好地实例化,但屏幕是空白的。 我的代码有什么明显的地方吗 主要活动Android 嵌套片段-Frag2的屏幕保持为空,android,android-fragments,android-nested-fragment,Android,Android Fragments,Android Nested Fragment,我正在忍受嵌套碎片的折磨。我有一个mainactivity,它调用片段1,片段1通过按钮依次调用片段。片段frag2被很好地实例化,但屏幕是空白的。 我的代码有什么明显的地方吗 主要活动 import android.app.FragmentManager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatA
import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager;
Frag1 f1 = new Frag1();
fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame,
f1).commit();
}
}
主活动xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.narb.nestedfragments.MainActivity">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
在调用frag2之前,我需要使布局FRAG1不可见,这正常吗
最后是我的片段2和它的布局。我看到frag 2的日志,但屏幕是空的:
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Frag2 extends Fragment {
Context context;
private Button btn2;
public Frag2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview;
rootview = inflater.inflate(R.layout.fragment_frag2, container, false);
Log.i("frag2","createdview");
return rootview;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();
Log.i("frag2","onactivitycreated");
btn2 = (Button) getActivity().findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("frag2","onactivitycreated");
}
});
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.narb.nestedfragments.Frag2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Test 2" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="60dp"
android:text="back"
/>
</RelativeLayout>
导入android.app.Fragment;
导入android.content.Context;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
公共类Frag2扩展了片段{
语境;
专用按钮btn2;
公共框架2(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图;
rootview=充气机。充气(R.layout.fragment\u frag2,容器,假);
Log.i(“frag2”、“createdview”);
返回rootview;
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
context=getActivity().getApplicationContext();
Log.i(“frag2”、“onactivitycreated”);
btn2=(按钮)getActivity().findViewById(R.id.btn2);
btn2.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
Log.i(“frag2”、“onactivitycreated”);
}
});
}
}
如果片段1具有另一个替换片段2的框架布局
,则在片段1中使用getChildFragmentManager()
而不是getFragmentManager()
否则,您在replace()
methodR.id.fl1
inside fragment1中传递了错误的容器id。您应该在那里传递R.id.content\u frame
。您尚未发布片段1 XML代码。假设RelativeLayout
(rl1
)是您的父布局,并且在rl1
中有FrameLayout
(fl1
)。您正在使父布局不可见。所以碎片2是看不见的
如果您不想在显示片段的同时显示片段1,则不需要嵌套片段
getFragmentManager().beginTransaction()
.replace(R.id.content_frame ,new Frag2());
.addToBackStack(null);
.commit();
您应该这样调用以加载第二个片段
getFragmentManager().beginTransaction()
.replace(R.id.content_frame ,new Frag2());
.addToBackStack(null);
.commit();
在Frag1类中使用活动\u main的内容\u帧id
back1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("frag1", "createdview");
//getActivity().getFragmentManager().popBackStackImmediate();
rl1.setVisibility(View.INVISIBLE);
FragmentTransaction ft = getFragmentManager().beginTransaction();
Frag2 f2 = new Frag2();
ft.replace(R.id.content_frame, f2);
ft.addToBackStack(null);
ft.commit();
}
});
在调用frag2之前,我需要使布局FRAG1不可见,这正常吗?不嵌套的?那么为什么不使用getChildFragmentManager
?fragment2-文本视图高度匹配\u父级更改为包装\u contentfragment\u Fragma1 xml未附加..明白了。我想做的正是你描述的。现在可以了。在它上面,我添加了PopBackback,这也很有效。出于好奇,在什么情况下您会使用getChildFragmentManager?我开始拉扯我的头发。这些碎片不容易抓住。非常感谢!例如,如果您的片段具有ViewPager,其中每个页面都是片段,那么您需要使用getChildFragmentManager(),因为它们是片段中的片段。