gpt4 book ai didi

android - 嵌套 fragment - Frag2 的屏幕保持空白

转载 作者:行者123 更新时间:2023-11-29 19:08:59 25 4
gpt4 key购买 nike

我正在经历嵌套的 fragment 。我有一个调用 fragment 1 的主 Activity , fragment 1 又通过按钮调用 fragment 。 fragment frag2 已很好地实例化,但屏幕是空白的。我的代码有什么明显的地方吗?

主要 Activity

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();
}
}

主 Activity 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>

fragment 1 及其 xml 布局:

import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;


/**
* A simple {@link Fragment} subclass.
*/
public class Frag1 extends android.app.Fragment {
Context context;
private Button back1;
RelativeLayout rl1;

public Frag1() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = null;
rootview = inflater.inflate(R.layout.fragment_frag1, container, false);
return rootview;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
context = getActivity().getApplicationContext();

initFindView();

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.fl1, f2);
ft.addToBackStack(null);
ft.commit();
}
});

}

private void initFindView(){
back1 = (Button) getActivity().findViewById(R.id.btn1);
rl1 = (RelativeLayout) getActivity().findViewById(R.id.rl1);
}
}

在调用 frag2 之前,我需要让布局 frag 1 不可见是否正常?

最后是我的 fragment 2 及其布局。我看到了 fragment 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>

最佳答案

如果您的 fragment 1 有另一个 FrameLayout,您要在其中替换 fragment 2,则不要使用 getFragmentManager(),而是使用 getChildFragmentManager()在 fragment 1

否则,您在 fragment1 内的 replace() 方法 R.id.fl1 中传递了错误的容器 ID。你应该在那里传递 R.id.content_frame

关于android - 嵌套 fragment - Frag2 的屏幕保持空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46078666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com