嵌套片段-Frag2屏幕保持空白 [英] Nested fragments - Screen of Frag2 stays empty

查看:74
本文介绍了嵌套片段-Frag2屏幕保持空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正遭受嵌套碎片的困扰。我有一个mainactivity,它调用片段1,后者又通过按钮调用片段。片段frag2实例化良好,但屏幕空白。
我的代码有什么明显的地方吗?

I'm suffering through nested fragments. I have a mainactivity which calls a fragment 1 which in turns call a fragment via a button. The fragment frag2 is well instantiated but the screen is blank. Is there something obvious with my code?

MainActivity

MainActivity

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

mainactivity xml

mainactivity 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>

片段1及其xml布局:

Fragment 1 and its xml layout:

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

我需要使布局变脆是否正常? 1在调用frag2之前是不可见的?

Is it normal that I need to make my layout frag 1 invisible before calling frag2?

最后是片段2及其布局。我看到了碎片2的日志,但屏幕为空:

and finally my fragment 2 and its layout. I see the log for frag 2 but the screen is empty:

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>


推荐答案

如果片段1还有另一个 FrameLayout 替换片段2,然后代替 getFragmentManager(),使用 getChildFragmentManager()在片段1

if your fragment 1 is having another FrameLayout where you are replacing fragment 2, then instead of getFragmentManager(), use getChildFragmentManager() in fragment 1

否则,您在 replace()方法<$ c中传递了错误的容器ID $片段中的$ c> R.id.fl1 。您应该在那里传递 R.id.content_frame

otherwise you are passing wrong container id in replace() method R.id.fl1 inside fragment1. you should pass R.id.content_frame there.

这篇关于嵌套片段-Frag2屏幕保持空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆